it was colliding with math log, renamed to applog
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#define LOG_HPP
|
||||
|
||||
// comment out to disable logging in general
|
||||
#define log(level, fmt, ...) _log(level, std::source_location::current(), fmt, ##__VA_ARGS__)
|
||||
#define applog(level, fmt, ...) _log(level, std::source_location::current(), fmt, ##__VA_ARGS__)
|
||||
// comment out to disable logging to file
|
||||
// #define LOG_FILE_LOCATION "linuxSmartie.log.gz"
|
||||
|
||||
#ifdef log
|
||||
#ifdef applog
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
@@ -83,7 +83,7 @@ inline void _log(LOG level, const std::source_location& loc, std::format_string<
|
||||
|
||||
#else
|
||||
|
||||
#define log(...)
|
||||
#define applog(...)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
10
main.cpp
10
main.cpp
@@ -43,13 +43,13 @@ struct Sensors {
|
||||
};
|
||||
|
||||
void signalHandler(int) {
|
||||
log(LOG::WRN, "got SIGINT/SIGTERM, exiting...");
|
||||
applog(LOG::WRN, "got SIGINT/SIGTERM, exiting...");
|
||||
sem::main.release();
|
||||
sem::display.release();
|
||||
}
|
||||
|
||||
void hassThread(std::stop_token st, Sensors* s) {
|
||||
log(LOG::INF, "Thread started");
|
||||
applog(LOG::INF, "Thread started");
|
||||
HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc");
|
||||
|
||||
pc_power.connect();
|
||||
@@ -66,7 +66,7 @@ void hassThread(std::stop_token st, Sensors* s) {
|
||||
}
|
||||
|
||||
void sensorsThread(std::stop_token st, Sensors* s) {
|
||||
log(LOG::INF, "Thread started, poll interval {} ms", LCD_INTERVAL_MS);
|
||||
applog(LOG::INF, "Thread started, poll interval {} ms", LCD_INTERVAL_MS);
|
||||
cpu::detectHwmon(&s->cpu, "0000:00:18.3");
|
||||
gpu::detectHwmon(&s->gpu, "0000:0b:00.0");
|
||||
sys::detectHwmon(&s->sys, "nct6775.656");
|
||||
@@ -94,7 +94,7 @@ void sensorsThread(std::stop_token st, Sensors* s) {
|
||||
}
|
||||
|
||||
void displayThread(std::stop_token st, Sensors* s) {
|
||||
log(LOG::INF, "Thread started");
|
||||
applog(LOG::INF, "Thread started");
|
||||
// init internal variables of LCDDisplay class
|
||||
LCDDisplay lcd(LCD_PORT);
|
||||
|
||||
@@ -165,7 +165,7 @@ void displayThread(std::stop_token st, Sensors* s) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
log(LOG::START, "============================= PROGRAM START ============================="); // to make logs clearer
|
||||
applog(LOG::START, "============================= PROGRAM START ============================="); // to make logs clearer
|
||||
std::signal(SIGINT, signalHandler);
|
||||
std::signal(SIGTERM, signalHandler);
|
||||
|
||||
|
||||
@@ -40,11 +40,11 @@ LCDDisplay::~LCDDisplay() {
|
||||
}
|
||||
|
||||
void LCDDisplay::open(bool reopen) {
|
||||
log(LOG::INF, "Opening serial port at \"{}\"", _dev);
|
||||
applog(LOG::INF, "Opening serial port at \"{}\"", _dev);
|
||||
if (_lcd < 0 || reopen)
|
||||
_lcd = ::open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); // ::open calls the global open() instead of LCDDisplay::open()
|
||||
if (_lcd < 0) {
|
||||
log(LOG::ERR, "Error opening serial port \"{}\"", _dev); // err
|
||||
applog(LOG::ERR, "Error opening serial port \"{}\"", _dev); // err
|
||||
exit(_lcd);
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ void LCDDisplay::open(bool reopen) {
|
||||
void LCDDisplay::close() {
|
||||
if (_lcd >= 0) {
|
||||
::close(_lcd);
|
||||
log(LOG::INF, "Closed LCD port");
|
||||
applog(LOG::INF, "Closed LCD port");
|
||||
}
|
||||
_lcd = -1;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ void LCDDisplay::close() {
|
||||
void LCDDisplay::write(const uint8_t* cmdbuf, uint8_t len) {
|
||||
int rc = ::write(_lcd, cmdbuf, len);
|
||||
if(rc < 0) {
|
||||
log(LOG::ERR, "failed to execute ::write, lost access to port?");
|
||||
applog(LOG::ERR, "failed to execute ::write, lost access to port?");
|
||||
this->open(true);
|
||||
}
|
||||
}
|
||||
@@ -71,20 +71,20 @@ void LCDDisplay::clear() {
|
||||
}
|
||||
|
||||
void LCDDisplay::setContrast(uint8_t contrast) {
|
||||
log(LOG::INF, "Set contrast to {}", contrast);
|
||||
applog(LOG::INF, "Set contrast to {}", contrast);
|
||||
const uint8_t cmdbuf[] = {254, 80, contrast};
|
||||
this->write(cmdbuf, sizeof(cmdbuf));
|
||||
}
|
||||
|
||||
void LCDDisplay::setBacklight(bool enabled) {
|
||||
log(LOG::INF, "Set backlight to {}", enabled);
|
||||
applog(LOG::INF, "Set backlight to {}", enabled);
|
||||
const uint8_t cmdbuf[] = {254, (uint8_t)(enabled ? 66 : 70), 255};
|
||||
this->write(cmdbuf, enabled ? 3 : 2);
|
||||
}
|
||||
|
||||
/* still doesnt work? */
|
||||
void LCDDisplay::setBacklight(uint8_t brightness) {
|
||||
log(LOG::INF, "Set backlight brightness to {}", brightness);
|
||||
applog(LOG::INF, "Set backlight brightness to {}", brightness);
|
||||
const uint8_t cmdbuf[] = {254, (uint8_t)(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness
|
||||
this->write(cmdbuf, brightness ? 4 : 2);
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ void detectHwmon(cpuSensors* target, const std::string& pci) {
|
||||
try {
|
||||
target->hwmon = getFirstDir(path);
|
||||
} catch (...) {
|
||||
log(LOG::ERR, "NO CPU HWMON KERNEL MODULE DETECTED");
|
||||
applog(LOG::ERR, "NO CPU HWMON KERNEL MODULE DETECTED");
|
||||
return;
|
||||
}
|
||||
|
||||
target->zenpower = false; // we were using readFileToStr to figure this out, we dont f/w it anymore, find another way (see if hwmon/in1_input exists?)
|
||||
log(LOG::INF, "Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon);
|
||||
applog(LOG::INF, "Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon);
|
||||
}
|
||||
|
||||
void update(cpuSensors* c) {
|
||||
|
||||
@@ -12,11 +12,11 @@ namespace gpu {
|
||||
/* takes pointer to a target gpu::sensors struct and updates its hwmon path and detects gpu_metrics */
|
||||
void detectHwmon(gpuSensors* target, const std::string& pci) {
|
||||
target->hwmon = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
|
||||
log(LOG::INF, "Detected path \"{}\"", target->hwmon);
|
||||
applog(LOG::INF, "Detected path \"{}\"", target->hwmon);
|
||||
|
||||
if (FILE* file = fopen((target->hwmon + REL_METRICS_PATH).c_str(), "r")) {
|
||||
fclose(file);
|
||||
log(LOG::INF, "found gpu_metrics");
|
||||
applog(LOG::INF, "found gpu_metrics");
|
||||
target->hasGpuMetrics = true;
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ void updateFromMetrics(gpuSensors* g) {
|
||||
|
||||
/* werid hardware i probably dont like, maybe fallback to reading from fs? */
|
||||
default: {
|
||||
log(LOG::WRN, "Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
|
||||
applog(LOG::WRN, "Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
23
src/hass.cpp
23
src/hass.cpp
@@ -1,8 +1,7 @@
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <hass.hpp>
|
||||
#include <log.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
HomeAssistant::HomeAssistant(std::string addr, std::string ent) {
|
||||
@@ -17,7 +16,7 @@ HomeAssistant::~HomeAssistant() {
|
||||
void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons reason, char* in) {
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: {
|
||||
log(LOG::ERR, "websocket conn error: {}", in ? in : "(null)");
|
||||
applog(LOG::ERR, "websocket conn error: {}", in ? in : "(null)");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -34,7 +33,7 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
}
|
||||
|
||||
if (ha_type == "auth_ok") {
|
||||
log(LOG::INF, "subscribing to entity \"{}\"", entity);
|
||||
applog(LOG::INF, "subscribing to entity \"{}\"", entity);
|
||||
json.clear();
|
||||
json["id"] = id++;
|
||||
json["type"] = "subscribe_entities";
|
||||
@@ -45,30 +44,30 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
|
||||
if (ha_type == "result") {
|
||||
if (json["success"].get<bool>() != true)
|
||||
log(LOG::ERR, "api said success was \"false\" on message id {}", id - 1);
|
||||
applog(LOG::ERR, "api said success was \"false\" on message id {}", id - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ha_type == "event") {
|
||||
json = json["event"];
|
||||
if (json.contains("a"))
|
||||
if(json.contains("a"))
|
||||
state = std::stof(json["a"][entity]["s"].get<std::string>());
|
||||
if (json.contains("c"))
|
||||
if(json.contains("c"))
|
||||
state = std::stof(json["c"][entity]["+"]["s"].get<std::string>());
|
||||
break;
|
||||
}
|
||||
|
||||
log(LOG::WRN, "unhandled event: {}", (char*)in);
|
||||
applog(LOG::WRN, "unhandled event: {}", (char*)in);
|
||||
break;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED: {
|
||||
log(LOG::INF, "Websocket connected");
|
||||
applog(LOG::INF, "Websocket connected");
|
||||
break;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_CLIENT_CLOSED: {
|
||||
log(LOG::INF, "Websocket closed");
|
||||
applog(LOG::INF, "Websocket closed");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -127,7 +126,7 @@ int HomeAssistant::connect() {
|
||||
|
||||
context = lws_create_context(&info);
|
||||
if (!context) {
|
||||
log(LOG::ERR, "error creating context");
|
||||
applog(LOG::ERR, "error creating context");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -144,7 +143,7 @@ int HomeAssistant::connect() {
|
||||
|
||||
websock = lws_client_connect_via_info(&i);
|
||||
if (!websock) {
|
||||
log(LOG::ERR, "error creating lws client");
|
||||
applog(LOG::ERR, "error creating lws client");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -68,7 +68,7 @@ void sys::detectHwmon(sysSensors* s, const std::string& platform) {
|
||||
try {
|
||||
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
|
||||
} catch (...) {
|
||||
log(LOG::ERR, "Can't open sys hwmon. Module loaded? Kernel params added?");
|
||||
applog(LOG::ERR, "Can't open sys hwmon. Module loaded? Kernel params added?");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ std::string pBytes(double bytes, bool isKbytes) {
|
||||
ssize_t readFile(const char* path, char* buffer, size_t maxlen) {
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
log(LOG::ERR, "Error reading {}", path);
|
||||
applog(LOG::ERR, "Error reading {}", path);
|
||||
return 0;
|
||||
}
|
||||
ssize_t len = read(fd, buffer, maxlen - 1);
|
||||
|
||||
Reference in New Issue
Block a user