#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include "log_entry.h"
const auto delta = std::chrono::seconds(5);
std::filesystem::path FindHwmonByName(const std::string& mon_name) {
std::optional<std::filesystem::path> mon_path;
for (auto& p : std::filesystem::directory_iterator("/sys/class/hwmon")) {
std::string path = p.path() / "name";
std::ifstream ifs(path);
if (!ifs.is_open()) {
std::ostringstream ss;
ss << "Failed to open " << path << " for read";
throw std::runtime_error(ss.str());
}
std::string name;
ifs >> name;
if (name == mon_name) {
mon_path = p.path();
break;
}
}
if (!mon_path) {
std::ostringstream ss;
ss << "No hwmon with name " << mon_name;
throw std::runtime_error(ss.str());
}
return mon_path.value();
}
int main(int argc, char** argv) {
try {
if (argc != 4) {
std::ostringstream ss;
ss << "Usage: " << argv[0] << " [OUTPUT FILE] [HWMON NAME] [INPUT NAME]";
throw std::runtime_error(ss.str());
}
auto mon_path = FindHwmonByName(argv[2]);
std::string input_path = mon_path / argv[3];
std::cerr << input_path << std::endl;
while (true) {
std::ifstream ifs(input_path);
if (!ifs.is_open()) {
std::ostringstream ss;
ss << "Failed to open " << input_path;
throw std::runtime_error(ss.str());
}
int input_value;
if (!ifs >> input_value) {
std::ostringstream ss;
ss << "Failed to read value from " << input_path;
throw std::runtime_error(ss.str());
}
std::cout << input_value << std::endl; // !!!!!!!!!!!!
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
$ ./hwmon_logger /dev/stdout amdgpu temp1_input | head -10
/sys/class/hwmon/hwmon1/temp1_input
0
0
0
0
0
0
0
0
0
0
$ cat /sys/class/hwmon/hwmon1/temp1_input
44000
Иногда просто какой-то мусор выводит, типа 32766.
Что такого делает cat, чего не умеет ifstream?