whole lotta shit clion made me do
This commit is contained in:
44
src/sys.cpp
44
src/sys.cpp
@@ -1,5 +1,6 @@
|
||||
#include <sys/statvfs.h>
|
||||
|
||||
#include <charconv>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <log.hpp>
|
||||
@@ -7,36 +8,37 @@
|
||||
#include <sys.hpp>
|
||||
#include <utils.hpp>
|
||||
|
||||
int getVal(std::string_view target, std::string_view content) {
|
||||
int getVal(const std::string_view target, std::string_view content) {
|
||||
int result = -1;
|
||||
std::size_t start = content.find(target);
|
||||
if (start != std::string::npos) {
|
||||
int begin = start + target.length();
|
||||
std::size_t end = content.find("kB", start);
|
||||
result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10);
|
||||
if (const std::size_t start = content.find(target); start != std::string::npos) {
|
||||
content.remove_prefix(start + target.length());
|
||||
const std::size_t end = content.find(" kB");
|
||||
while (std::isspace(content.front()))
|
||||
content.remove_prefix(1);
|
||||
std::from_chars(content.data(), content.data() + end, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void sys::getTimeDate(TimeDate* td) {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto local = std::chrono::current_zone()->to_local(now);
|
||||
std::chrono::year_month_day ymd{floor<std::chrono::days>(local)};
|
||||
std::chrono::weekday wd{floor<std::chrono::days>(local)};
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
const auto local = std::chrono::current_zone()->to_local(now);
|
||||
const std::chrono::year_month_day ymd{floor<std::chrono::days>(local)};
|
||||
const std::chrono::weekday wd{floor<std::chrono::days>(local)};
|
||||
td->time = std::format("{:%H:%M:%S}", floor<std::chrono::seconds>(local));
|
||||
td->date = std::format("{}, {} {}", wdays[wd.c_encoding()], ymd.day(), months[(unsigned)ymd.month() - 1]);
|
||||
td->date = std::format("{}, {} {}", wdays[wd.c_encoding()], ymd.day(), months[static_cast<unsigned>(ymd.month()) - 1]);
|
||||
}
|
||||
|
||||
void sys::getRam(Memory* mem) {
|
||||
static char buf[128]; // file is longer but we only need first 128 chars
|
||||
utils::readFile("/proc/meminfo", buf, sizeof(buf));
|
||||
mem->free = getVal("MemAvailable:", std::string_view(buf));
|
||||
mem->total = getVal("MemTotal:", std::string_view(buf));
|
||||
mem->total = getVal("MemTotal:", std::string_view(buf)); // shouldn't really update this on each loop
|
||||
mem->used = mem->total - mem->free;
|
||||
}
|
||||
|
||||
void sys::getDisk(Disk* disk) {
|
||||
struct statvfs diskData;
|
||||
struct statvfs diskData{};
|
||||
statvfs(disk->mountpoint.c_str(), &diskData);
|
||||
disk->usage.free = diskData.f_bfree * diskData.f_frsize;
|
||||
disk->usage.used = (diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize;
|
||||
@@ -50,14 +52,14 @@ void sys::getNetwork(Network* net, const std::string& device) {
|
||||
uint64_t tx = 0;
|
||||
};
|
||||
|
||||
static char buf[24];
|
||||
static char buf[32];
|
||||
static _net prev;
|
||||
_net current;
|
||||
|
||||
current.ts = std::chrono::steady_clock::now();
|
||||
current.rx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf);
|
||||
current.tx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf);
|
||||
float ts_diff = std::chrono::duration_cast<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
|
||||
utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf, current.rx);
|
||||
utils::fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf, current.tx);
|
||||
const double ts_diff = std::chrono::duration_cast<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
|
||||
net->download = (current.rx - prev.rx) / ts_diff;
|
||||
net->upload = (current.tx - prev.tx) / ts_diff;
|
||||
prev = current;
|
||||
@@ -74,10 +76,10 @@ void sys::detectHwmon(sysSensors* s, const std::string& platform) {
|
||||
}
|
||||
|
||||
void sys::updateSensors(sysSensors* s) {
|
||||
if (s->hwmon.length() < 1)
|
||||
if (s->hwmon.empty())
|
||||
return;
|
||||
static char buf[8];
|
||||
s->fan_speed[0] = utils::fastReadNum(s->hwmon + "/fan2_input", buf);
|
||||
s->fan_speed[1] = utils::fastReadNum(s->hwmon + "/fan7_input", buf);
|
||||
s->t_sensor = utils::fastReadNum(s->hwmon + "/temp5_input", buf) / 1000;
|
||||
utils::fastReadNum(s->hwmon + "/fan2_input", buf, s->fan_speed[0]);
|
||||
utils::fastReadNum(s->hwmon + "/fan7_input", buf, s->fan_speed[1]);
|
||||
utils::fastReadNumDiv<1000>(s->hwmon + "/temp5_input", buf, s->t_sensor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user