logging lib

This commit is contained in:
Drath
2025-10-03 16:57:38 +02:00
parent 31f2c8bf7c
commit 7e7d8d2aa1
12 changed files with 80 additions and 48 deletions

View File

@@ -1,22 +1,13 @@
#include "LCDDisplay.hpp"
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <print>
#include <string>
/* lcd size hardcoded to 4x40 idgaf */
LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) {
std::println("[LCDDisplay] Opening LCD at \"{}\"", dev);
log(LOG::INF, "LCDDisplay", "Opening LCD at \"{}\"", dev);
struct termios term;
int lcd = open(dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (lcd < 0) {
std::println("[LCDDisplay] Error opening serial port \"{}\"", dev); // err
log(LOG::ERR, "LCDDisplay", "Error opening serial port \"{}\"", dev); // err
exit(lcd);
}
@@ -42,7 +33,7 @@ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) {
LCDDisplay::~LCDDisplay() {
close(_lcd);
std::println("[LCDDisplay] LCD port closed");
log(LOG::INF, "LCDDisplay", "LCD port closed");
}
void LCDDisplay::clear() {
@@ -51,21 +42,21 @@ void LCDDisplay::clear() {
}
void LCDDisplay::setContrast(uint8_t contrast) {
std::println("[LCDDisplay::setContrast] Set contrast to {}", contrast);
log(LOG::INF, "LCDDisplay::setContrast", "Set contrast to {}", contrast);
uint8_t cmdbuf[] = {254, 80, contrast};
write(_lcd, &cmdbuf, 3);
}
void LCDDisplay::setBacklight(bool enabled) {
std::println("[LCDDisplay::setBacklight] Set backlight to {}", enabled);
log(LOG::INF, "LCDDisplay::setBacklight", "Set backlight to {}", enabled);
uint8_t cmdbuf[] = {254, (uint8_t)(enabled ? 66 : 70), 255};
write(_lcd, &cmdbuf, enabled ? 3 : 2);
}
/* still doesnt work? */
void LCDDisplay::setBacklight(uint8_t brightness) {
std::println("[LCDDisplay::setBacklight] Set backlight brightness to {}", brightness);
uint8_t cmdbuf[] = {254, (uint8_t)(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness
log(LOG::INF, "LCDDisplay::setBacklight", "Set backlight brightness to {}", brightness);
uint8_t cmdbuf[] = {254, (uint8_t)(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness
write(_lcd, &cmdbuf, brightness ? 4 : 2);
}
@@ -95,7 +86,7 @@ void LCDDisplay::writeStr(uint8_t row, uint8_t col, std::string str) {
void LCDDisplay::writeLines(std::array<std::string, 4> lines) {
for (auto& line : lines) {
if (line.size() > LCD_COLS) {
std::println("[LCDDisplay::writeLines] Line {} too long! ({} chars)", line, line.size());
log(LOG::WRN, "LCDDisplay::writeLines", "Line {} too long! ({} chars)", line, line.size());
continue;
}
line.insert(line.end(), LCD_COLS - line.size(), ' ');

View File

@@ -9,11 +9,11 @@ void detectHwmon(cpuSensors* target, std::string pci) {
for (const auto& entry : std::filesystem::directory_iterator(path))
target->hwmon = getFirstDir(path);
} catch (...) {
std::println("[cpu::detectHwmon] NO CPU HWMON KERNEL MODULE DETECTED");
log(LOG::ERR, "cpu::detectHwmon", "NO CPU HWMON KERNEL MODULE DETECTED");
return;
}
target->zenpower = readFile((target->hwmon + "/name"), 128) == "zenpower";
std::println("[cpu::detectHwmon] Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon);
log(LOG::INF, "cpu::detectHwmon", "Detected module \"{}\" in path \"{}\"", target->zenpower ? "zenpower" : "k10temp", target->hwmon);
}
void update(cpuSensors* c) {
@@ -29,7 +29,7 @@ void update(cpuSensors* c) {
c->vcore = (double)(std::stoul(readFile(c->hwmon + "/in1_input", 4)) / 1000.0);
// cpu.vsoc = (double)(std::stoul(readFile(hw.path + "/in2_input")) / 1000.0);
} else {
c->tctl = std::stoi(readFile(c->hwmon+ "/temp1_input", 2));
c->tctl = std::stoi(readFile(c->hwmon + "/temp1_input", 2));
c->tccd1 = std::stoi(readFile(c->hwmon + "/temp3_input", 2));
}

View File

@@ -8,11 +8,11 @@ namespace gpu {
/* takes pointer to a target gpu::sensors struct and updates its hwmon path and detects gpu_metrics */
void detectHwmon(gpuSensors* target, std::string pci) {
target->hwmon = getFirstDir("/sys/bus/pci/devices/" + pci + "/hwmon");
std::println("[gpu::detectHwmon] Detected path \"{}\"", target->hwmon);
log(LOG::INF, "gpu::detectHwmon", "Detected path \"{}\"", target->hwmon);
if (FILE* file = fopen((target->hwmon + REL_METRICS_PATH).c_str(), "r")) {
fclose(file);
std::println("[gpu::detectHwmon] found metrics file");
log(LOG::INF, "gpu::detectHwmon", "found metrics file");
target->hasGpuMetrics = true;
}
}
@@ -80,7 +80,7 @@ void updateFromMetrics(gpuSensors* g) {
/* werid hardware i probably dont like, maybe fallback to reading from fs? */
default: {
std::println("[gpu::updateFromMetrics] Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
log(LOG::WRN, "gpu::updateFromMetrics", "Unsupported gpu_metrics_v{}_{}", header->format_revision, header->content_revision);
break;
}
}

View File

@@ -13,14 +13,13 @@ void HomeAssistant::event_handler(struct lws *wsi, enum lws_callback_reasons rea
ArduinoJson::JsonDocument json;
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: {
std::println("[HomeAssistant] websocket conn error: {}", in ? in : "(null)");
log(LOG::ERR, "HomeAssistant::event_handler", "websocket conn error: {}", in ? in : "(null)");
return;
}
case LWS_CALLBACK_CLIENT_RECEIVE: {
ArduinoJson::deserializeJson(json, in);
std::string ha_type = json["type"].as<std::string>();
// std::println("[HomeAssistant] got event type: {} ", ha_type);
if (ha_type == "auth_required") {
json.clear();
@@ -31,7 +30,7 @@ void HomeAssistant::event_handler(struct lws *wsi, enum lws_callback_reasons rea
}
if (ha_type == "auth_ok") {
std::println("[HomeAssistant] subscribing to entity \"{}\"", entity);
log(LOG::INF, "HomeAssistant::event_handler", "subscribing to entity \"{}\"", entity);
json.clear();
json["id"] = id++;
json["type"] = "subscribe_entities";
@@ -42,7 +41,7 @@ void HomeAssistant::event_handler(struct lws *wsi, enum lws_callback_reasons rea
if (ha_type == "result") {
if (json["success"].as<std::string>() == "false")
std::println("[HomeAssistant] api said success was \"false\" on message id {}", id - 1);
log(LOG::ERR, "HomeAssistant::event_handler", "api said success was \"false\" on message id {}", id - 1);
break;
}
@@ -51,24 +50,23 @@ void HomeAssistant::event_handler(struct lws *wsi, enum lws_callback_reasons rea
break;
}
std::println("[HomeAssistant] unhandled event: {}", (char *)in);
log(LOG::WRN, "HomeAssistant::event_handler", "unhandled event: {}", (char *)in);
break;
}
case LWS_CALLBACK_CLIENT_ESTABLISHED: {
std::println("[HomeAssistant] Websocket connected");
log(LOG::INF, "HomeAssistant::event_handler", "Websocket connected");
break;
}
case LWS_CALLBACK_CLIENT_CLOSED: {
std::println("[HomeAssistant] Websocket closed");
log(LOG::INF, "HomeAssistant::event_handler", "Websocket closed");
return;
}
case LWS_CALLBACK_CLIENT_WRITEABLE: {
/* skip whole callback if there's no tx to send */
if (tx.size() > 0) {
// std::println("[HomeAssistant] sending {}", tx);
size_t msgLen = LWS_PRE + tx.size();
unsigned char *buf = (unsigned char *)malloc(msgLen);
if (buf) {
@@ -122,7 +120,7 @@ int HomeAssistant::connect() {
context = lws_create_context(&info);
if (!context) {
std::println("[HomeAssistant] error creating context");
log(LOG::ERR, "HomeAssistant::connect", "error creating context");
return -1;
}
@@ -139,7 +137,7 @@ int HomeAssistant::connect() {
websock = lws_client_connect_via_info(&i);
if (!websock) {
std::println("[HomeAssistant] error creating lws client");
log(LOG::ERR, "HomeAssistant::connect", "error creating lws client");
return -1;
}
return 0;

View File

@@ -46,7 +46,7 @@ std::string pBytes(double bytes, bool isKbytes) {
ssize_t readFile2(const char* path, char* buffer, size_t maxlen) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
std::println("[readFile] Error reading {}", path);
log(LOG::ERR, "readFile2", "Error reading {}", path);
return 0;
}
ssize_t len = read(fd, buffer, maxlen - 1);