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() {
delay(500);
Serial.begin(115200);
info("MAIN", "\n\n\nStartup...");
info("MAIN", "Startup...");
mqttSetup();
wifiSetup();
patrixSetup();

View File

@ -2,8 +2,6 @@
#include "log.h"
#include "mqtt.h"
#define SEPARATOR " | "
bool debugEnabled = false;
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);
}
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];
getDateTime(datetime, sizeof datetime);
va_list vl;
va_start(vl, format);
char header[50];
snprintf(header, sizeof header, " | %-5s | %-15s | ", level, module);
char message[500];
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();
mqttPublishLog(len, SEPARATOR, datetime, module, level, message);
const size_t len = Serial.print(datetime) + Serial.print(header) + Serial.print(message) + Serial.println();
mqttPublishLog(len, datetime, header, message);
}
void debug(const char *module, const char *format, ...) {

View File

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

View File

@ -9,7 +9,7 @@ void mqttLoop();
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);

View File

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