log va_list FIX + log padding FIX + log clean

This commit is contained in:
Patrick Haßel 2024-04-10 09:54:24 +02:00
parent 35af48211e
commit 80a694116a
5 changed files with 20 additions and 26 deletions

View File

@ -9,7 +9,7 @@
void setup() { void setup() {
delay(500); delay(500);
Serial.begin(115200); Serial.begin(115200);
info("MAIN", "\n\n\nStartup..."); info("MAIN", "Startup...");
mqttSetup(); mqttSetup();
wifiSetup(); wifiSetup();
patrixSetup(); patrixSetup();

View File

@ -2,8 +2,6 @@
#include "log.h" #include "log.h"
#include "mqtt.h" #include "mqtt.h"
#define SEPARATOR " | "
bool debugEnabled = false; bool debugEnabled = false;
void getDateTime(char *buffer, size_t size) { void getDateTime(char *buffer, size_t size) {
@ -14,18 +12,18 @@ void getDateTime(char *buffer, size_t size) {
strftime(buffer, size, "%Y-%m-%d %H:%M:%S %z", &time); strftime(buffer, size, "%Y-%m-%d %H:%M:%S %z", &time);
} }
void log(const char *level, const char *module, const char *format, ...) { void log(const char *level, const char *module, const char *format, va_list vl) {
char datetime[26]; char datetime[26];
getDateTime(datetime, sizeof datetime); getDateTime(datetime, sizeof datetime);
va_list vl; char header[50];
va_start(vl, format); snprintf(header, sizeof header, " | %-5s | %-15s | ", level, module);
char message[500]; char message[500];
vsnprintf(message, sizeof message, format, vl); vsnprintf(message, sizeof message, format, vl);
va_end(vl);
const size_t len = Serial.print(datetime) + Serial.print(SEPARATOR) + Serial.print(module) + Serial.print(SEPARATOR) + Serial.print(level) + Serial.print(SEPARATOR) + Serial.print(message) + Serial.println(); const size_t len = Serial.print(datetime) + Serial.print(header) + Serial.print(message) + Serial.println();
mqttPublishLog(len, SEPARATOR, datetime, module, level, message); mqttPublishLog(len, datetime, header, message);
} }
void debug(const char *module, const char *format, ...) { void debug(const char *module, const char *format, ...) {

View File

@ -36,16 +36,16 @@ void mqttLoop() {
mqttLastConnectTry = millis(); mqttLastConnectTry = millis();
mqtt.setServer("10.0.0.50", 1883); mqtt.setServer("10.0.0.50", 1883);
if (!mqtt.connect(HOSTNAME, logTopic, 0, false, "disconnected")) { if (!mqtt.connect(HOSTNAME, logTopic, 0, false, "disconnected")) {
Serial.printf("failed to connect\n"); error("MQTT", "Failed to connect MQTT server!");
return; return;
} }
info("MQTT", "connected"); info("MQTT", "MQTT server connected!");
mqtt.setBufferSize(512); mqtt.setBufferSize(512);
mqtt.subscribe(cmdTopic); mqtt.subscribe(cmdTopic);
mqtt.setCallback([](const char *topic, const uint8_t *bytes, const unsigned int length) { mqtt.setCallback([](const char *topic, const uint8_t *bytes, const unsigned int length) {
char content[64]; char content[64];
if (length > sizeof content - 1) { if (length > sizeof content - 1) {
error("MQTT", "RECEIVE BUFFER OVERFLOW"); error("MQTT", "MQTT RECEIVE BUFFER OVERFLOW");
return; return;
} }
memcpy(content, bytes, length); memcpy(content, bytes, length);
@ -60,14 +60,10 @@ void mqttLoop() {
mqtt.loop(); mqtt.loop();
} }
void mqttPublishLog(const size_t len, const char *separator, const char *datetime, const char *module, const char *level, const char *message) { void mqttPublishLog(const size_t len, const char *datetime, const char *header, const char *message) {
if (mqtt.beginPublish(logTopic, len, false)) { if (mqtt.beginPublish(logTopic, len, false)) {
mqtt.print(datetime); mqtt.print(datetime);
mqtt.print(separator); mqtt.print(header);
mqtt.print(module);
mqtt.print(separator);
mqtt.print(level);
mqtt.print(separator);
mqtt.print(message); mqtt.print(message);
mqtt.endPublish(); mqtt.endPublish();
} }

View File

@ -9,7 +9,7 @@ void mqttLoop();
void mqttDisconnect(); void mqttDisconnect();
void mqttPublishLog(const size_t len, const char *separator, const char *datetime, const char *module, const char *level, const char *message); void mqttPublishLog(const size_t len, const char *datetime, const char *header, const char *message);
bool mqttPublishData(const char *name, const JsonDocument &doc); bool mqttPublishData(const char *name, const JsonDocument &doc);

View File

@ -39,25 +39,25 @@ void wifiConnect() {
WiFi.disconnect(); WiFi.disconnect();
yield(); yield();
wifiLastConnectTry = millis(); wifiLastConnectTry = millis();
info("WIFI", "Connecting: %s", WIFI_SSID); info("WIFI", "Connecting WiFi: %s", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PKEY); WiFi.begin(WIFI_SSID, WIFI_PKEY);
yield(); yield();
} }
void otaSetup() { void otaSetup() {
ArduinoOTA.onStart([] { ArduinoOTA.onStart([] {
info("OTA", "Start!"); info("OTA", "OTA start...");
otaLastLogStep = 0; otaLastLogStep = 0;
}); });
ArduinoOTA.onProgress([](unsigned int received, unsigned int total) { ArduinoOTA.onProgress([](unsigned int received, unsigned int total) {
uint8_t currentStep = 20 * received / total; uint8_t currentStep = 20 * received / total;
if (otaLastLogStep != currentStep) { if (otaLastLogStep != currentStep) {
info("OTA", "Progress: %3d%%", currentStep * 5); info("OTA", "OTA Progress: %3d%%", currentStep * 5);
otaLastLogStep = currentStep; otaLastLogStep = currentStep;
} }
}); });
ArduinoOTA.onEnd([] { ArduinoOTA.onEnd([] {
info("OTA", "Complete!"); info("OTA", "OTA Complete!");
}); });
ArduinoOTA.onError([](ota_error_t e) { ArduinoOTA.onError([](ota_error_t e) {
const char *name; const char *name;
@ -101,7 +101,7 @@ void bootDelay() {
yield(); yield();
wifiLoop(); wifiLoop();
} }
info("BOOT DELAY", "Completed."); info("BOOT DELAY", "Boot delay completed!");
#endif #endif
} }
@ -120,7 +120,7 @@ void wifiLoop() {
if (wifiConnected) { if (wifiConnected) {
if (!hasIp) { if (!hasIp) {
wifiConnected = false; wifiConnected = false;
info("WIFI", "Disconnected!"); info("WIFI", "WiFi disconnected!");
wifiConnect(); wifiConnect();
} }
} else { } else {
@ -128,7 +128,7 @@ void wifiLoop() {
wifiConnected = true; wifiConnected = true;
info("WIFI", "Connected as: %s", WiFi.localIP().toString().c_str()); info("WIFI", "Connected as: %s", WiFi.localIP().toString().c_str());
} else if (millis() - wifiLastConnectTry > 10000) { } else if (millis() - wifiLastConnectTry > 10000) {
info("WIFI", "Timeout!"); info("WIFI", "WiFi timeout!");
wifiConnect(); wifiConnect();
} }
} }