RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
system.cpp

System information, environment variables, files and a timer.

System information, environment variables, files and a timer.

#include <filesystem>
#include <iostream>
int main() {
namespace Sys = RST::System;
// Machine
std::cout << Sys::getOSName() << " (" << Sys::getArchitecture() << "), " << Sys::getCpuName() << '\n';
std::cout << Sys::getCpuCores() << " cores, " << Sys::getAvailableRAM() / (1024 * 1024) << " MiB of RAM available\n";
// Environment and paths
std::cout << "home: " << Sys::getHomeDirectory().string() << '\n';
std::cout << "shell: " << Sys::getEnv("SHELL").value_or("unknown") << '\n';
// Files, without exceptions
const std::filesystem::path note = std::filesystem::temp_directory_path() / "rstools-note.txt";
if (Sys::writeFile(note, "written at " + RST::Time::formatNow()))
std::cout << Sys::readFile(note).value_or("") << '\n';
std::cout << "done in " << timer.getElapsedMs() << " ms\n";
return 0;
}
Stopwatch that can be stopped and resumed, based on a steady clock.
Definition Timer.hpp:9
static Timer startNew() noexcept
Returns a running timer.
Definition Timer.hpp:17
T getElapsedMs() const noexcept
Definition Timer.hpp:81
Portable access to system information, environment variables, files and paths.
Definition Environment.hpp:7
std::string formatNow(std::string_view format="%Y-%m-%d %H:%M:%S", TimeZone zone=TimeZone::Local)
Formats the current time; see formatTime().