removed 'module' from log

This commit is contained in:
Patrick Haßel 2024-04-10 13:18:23 +02:00
parent d8eb6c87d8
commit 0fa32d9b97
7 changed files with 51 additions and 51 deletions

View File

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

View File

@ -17,7 +17,7 @@ void consoleLoop() {
if (symbol == '\n' || symbol == '\r') { if (symbol == '\n' || symbol == '\r') {
if (*consoleBuffer != 0) { if (*consoleBuffer != 0) {
Serial.print('\n'); Serial.print('\n');
info("CONSOLE", "> %s", consoleBuffer); info("CONSOLE > %s", consoleBuffer);
consoleHandle(consoleBuffer); consoleHandle(consoleBuffer);
consoleBufferW = consoleBuffer; consoleBufferW = consoleBuffer;
*consoleBufferW = 0; *consoleBufferW = 0;
@ -45,34 +45,34 @@ void consoleLoop() {
void consoleHandle(const char *cmd) { void consoleHandle(const char *cmd) {
if (strcmp(cmd, "help") == 0) { if (strcmp(cmd, "help") == 0) {
info("CONSOLE", "help : print this help"); info("help : print this help");
info("CONSOLE", "net : print network info"); info("net : print network info");
info("CONSOLE", "reboot : reboot system"); info("reboot : reboot system");
info("CONSOLE", "wifi : reconnect wifi"); info("wifi : reconnect wifi");
info("CONSOLE", "mqtt : reconnect mqtt"); info("mqtt : reconnect mqtt");
info("CONSOLE", "debug : toggle debug log"); info("debug : toggle debug log");
} else if (strcmp(cmd, "net") == 0) { } else if (strcmp(cmd, "net") == 0) {
info("CONSOLE", "MAC: %17s", WiFi.macAddress().c_str()); info("MAC: %17s", WiFi.macAddress().c_str());
info("CONSOLE", "IP: %17s", WiFi.localIP().toString().c_str()); info("IP: %17s", WiFi.localIP().toString().c_str());
info("CONSOLE", "Gateway: %17s", WiFi.gatewayIP().toString().c_str()); info("Gateway: %17s", WiFi.gatewayIP().toString().c_str());
info("CONSOLE", "SSID: %17s", WiFi.SSID().c_str()); info("SSID: %17s", WiFi.SSID().c_str());
info("CONSOLE", "BSSID: %17s", WiFi.BSSIDstr().c_str()); info("BSSID: %17s", WiFi.BSSIDstr().c_str());
info("CONSOLE", "RSSI: %17d", WiFi.RSSI()); info("RSSI: %17d", WiFi.RSSI());
} else if (strcmp(cmd, "reboot") == 0) { } else if (strcmp(cmd, "reboot") == 0) {
info("CONSOLE", "Rebooting..."); info("Rebooting...");
delay(500); delay(500);
yield(); yield();
ESP.restart(); ESP.restart();
} else if (strcmp(cmd, "wifi") == 0) { } else if (strcmp(cmd, "wifi") == 0) {
info("CONSOLE", "Reconnecting WiFi..."); info("Reconnecting WiFi...");
wifiConnect(); wifiConnect();
} else if (strcmp(cmd, "mqtt") == 0) { } else if (strcmp(cmd, "mqtt") == 0) {
info("CONSOLE", "Reconnecting MQTT..."); info("Reconnecting MQTT...");
mqttDisconnect(); mqttDisconnect();
} else if (strcmp(cmd, "debug") == 0) { } else if (strcmp(cmd, "debug") == 0) {
setDebugEnabled(!isDebugEnabled()); setDebugEnabled(!isDebugEnabled());
info("CONSOLE", "DEBUG: %s", isDebugEnabled() ? "ON" : "OFF"); info("DEBUG: %s", isDebugEnabled() ? "ON" : "OFF");
} else { } else {
info("CONSOLE", "Unknown command: %s", cmd); info("Unknown command: %s", cmd);
} }
} }

View File

@ -12,12 +12,12 @@ 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, va_list vl) { void log(const char *level, const char *format, va_list vl) {
char datetime[26]; char datetime[26];
getDateTime(datetime, sizeof datetime); getDateTime(datetime, sizeof datetime);
char header[45]; char header[20];
snprintf(header, sizeof header, " | %-5s | %-10s | ", level, module); snprintf(header, sizeof header, " | %-5s | ", level);
char message[500]; char message[500];
vsnprintf(message, sizeof message, format, vl); vsnprintf(message, sizeof message, format, vl);
@ -30,27 +30,27 @@ void log(const char *level, const char *module, const char *format, va_list vl)
mqttPublishLog(datetime, header, message); mqttPublishLog(datetime, header, message);
} }
void debug(const char *module, const char *format, ...) { void debug(const char *format, ...) {
if (!debugEnabled) { if (!debugEnabled) {
return; return;
} }
va_list vl; va_list vl;
va_start(vl, format); va_start(vl, format);
log("DEBUG", module, format, vl); log("DEBUG", format, vl);
va_end(vl); va_end(vl);
} }
void info(const char *module, const char *format, ...) { void info(const char *format, ...) {
va_list vl; va_list vl;
va_start(vl, format); va_start(vl, format);
log("INFO", module, format, vl); log("INFO", format, vl);
va_end(vl); va_end(vl);
} }
void error(const char *module, const char *format, ...) { void error(const char *format, ...) {
va_list vl; va_list vl;
va_start(vl, format); va_start(vl, format);
log("ERROR", module, format, vl); log("ERROR", format, vl);
va_end(vl); va_end(vl);
} }

View File

@ -1,11 +1,11 @@
#ifndef SENSOR3_LOG_H #ifndef SENSOR3_LOG_H
#define SENSOR3_LOG_H #define SENSOR3_LOG_H
void debug(const char *module, const char *format, ...); void debug(const char *format, ...);
void info(const char *module, const char *format, ...); void info(const char *format, ...);
void error(const char *module, const char *format, ...); void error(const char *format, ...);
void setDebugEnabled(bool enabled); void setDebugEnabled(bool enabled);

View File

@ -32,7 +32,7 @@ void mqttSetup() {
void mqttDisconnect() { void mqttDisconnect() {
if (mqttConnected) { if (mqttConnected) {
info("MQTT", "Disconnecting"); info("MQTT disconnecting");
mqtt.disconnect(); mqtt.disconnect();
mqttConnected = false; mqttConnected = false;
} }
@ -42,34 +42,34 @@ void mqttLoop() {
const bool connected = mqtt.loop(); const bool connected = mqtt.loop();
if (mqttConnected != connected) { if (mqttConnected != connected) {
if (!connected) { if (!connected) {
error("MQTT", "DISCONNECTED"); error("MQTT DISCONNECTED");
} }
mqttConnected = connected; mqttConnected = connected;
} }
if (isWiFiConnected() && isTimeSet() && !connected && (mqttLastConnectTry == 0 || millis() - mqttLastConnectTry > CONNECT_TIMEOUT_MILLISECONDS)) { if (isWiFiConnected() && isTimeSet() && !connected && (mqttLastConnectTry == 0 || millis() - mqttLastConnectTry > CONNECT_TIMEOUT_MILLISECONDS)) {
error("MQTT", "Connecting MQTT host: %s", MQTT_HOST); error("MQTT connecting MQTT host: %s", MQTT_HOST);
mqttLastConnectTry = millis(); mqttLastConnectTry = millis();
mqtt.setServer(MQTT_HOST, 1883); mqtt.setServer(MQTT_HOST, 1883);
if (!mqtt.connect(HOSTNAME, logTopic, 0, false, "MQTT disconnected")) { if (!mqtt.connect(HOSTNAME, logTopic, 0, false, "MQTT disconnected")) {
error("MQTT", "FAILED TO CONNECT"); error("MQTT FAILED TO CONNECT");
return; return;
} }
info("MQTT", "CONNECTED"); info("MQTT CONNECTED");
mqtt.setKeepAlive(10); mqtt.setKeepAlive(10);
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", "MQTT RECEIVE BUFFER OVERFLOW"); error("MQTT RECEIVE BUFFER OVERFLOW");
return; return;
} }
memcpy(content, bytes, length); memcpy(content, bytes, length);
content[length] = 0; content[length] = 0;
if (strcmp(topic, cmdTopic) == 0) { if (strcmp(topic, cmdTopic) == 0) {
info("MQTT", "CMD > %s", content); info("MQTT CMD > %s", content);
consoleHandle(content); consoleHandle(content);
} }
}); });

View File

@ -43,20 +43,20 @@ public:
first = false; first = false;
uint8_t count = sensors.getDeviceCount(); uint8_t count = sensors.getDeviceCount();
if (count == 0) { if (count == 0) {
error("DALLAS", "No devices found!"); error("No devices found!");
} else { } else {
debug("DALLAS", "Found %d devices:", count); debug("Found %d devices:", count);
uint64_t address; uint64_t address;
for (int index = 0; index < count; ++index) { for (int index = 0; index < count; ++index) {
sensors.getAddress((uint8_t *) &address, index); sensors.getAddress((uint8_t *) &address, index);
debug("DALLAS", " - 0x%016llX = %5.1f C", address, sensors.getTempC((uint8_t *) &address)); debug(" - 0x%016llX = %5.1f C", address, sensors.getTempC((uint8_t *) &address));
} }
} }
} }
converting = false; converting = false;
converted = true; converted = true;
} else if (millis() - lastMillis > DALLAS_TIMEOUT_MILLISECONDS) { } else if (millis() - lastMillis > DALLAS_TIMEOUT_MILLISECONDS) {
error("DALLAS", "Timeout!"); error("Timeout!");
converting = false; converting = false;
converted = false; converted = false;
} }

View File

@ -55,7 +55,7 @@ void wifiConnect() {
WiFi.setAutoReconnect(false); WiFi.setAutoReconnect(false);
yield(); yield();
info("WIFI", "Connecting: %s", WIFI_SSID); info("WIFI connecting: %s", WIFI_SSID);
WiFiClass::hostname(HOSTNAME); WiFiClass::hostname(HOSTNAME);
yield(); yield();
@ -66,18 +66,18 @@ void wifiConnect() {
void otaSetup() { void otaSetup() {
ArduinoOTA.onStart([] { ArduinoOTA.onStart([] {
info("OTA", "OTA start..."); info("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", "OTA Progress: %3d%%", currentStep * 5); info("OTA Progress: %3d%%", currentStep * 5);
otaLastLogStep = currentStep; otaLastLogStep = currentStep;
} }
}); });
ArduinoOTA.onEnd([] { ArduinoOTA.onEnd([] {
info("OTA", "OTA Complete!"); info("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 otaSetup() {
name = "[???]"; name = "[???]";
break; break;
} }
error("OTA", name); error(name);
}); });
} }
@ -132,7 +132,7 @@ void wifiLoop() {
if (!timeSet) { if (!timeSet) {
preTimeOffset = epochSeconds; preTimeOffset = epochSeconds;
} else { } else {
info("NTP", "Time set!"); info("Got time via NTP");
} }
} }
@ -140,7 +140,7 @@ void wifiLoop() {
if (wifiConnected) { if (wifiConnected) {
if (!hasIp) { if (!hasIp) {
wifiConnected = false; wifiConnected = false;
info("WIFI", "WiFi disconnected!"); info("WiFi disconnected!");
wifiConnect(); wifiConnect();
} else { } else {
ArduinoOTA.handle(); ArduinoOTA.handle();
@ -148,11 +148,11 @@ void wifiLoop() {
} else { } else {
if (hasIp) { if (hasIp) {
wifiConnected = true; wifiConnected = true;
info("WIFI", "WiFi connected: ip=%s", WiFi.localIP().toString().c_str()); info("WiFi connected: ip=%s", WiFi.localIP().toString().c_str());
ArduinoOTA.begin(); ArduinoOTA.begin();
configTime(TIMEZONE_OFFSET, DST_OFFSET, WiFi.gatewayIP().toString().c_str(), NTP_SERVER); configTime(TIMEZONE_OFFSET, DST_OFFSET, WiFi.gatewayIP().toString().c_str(), NTP_SERVER);
} else if (millis() - wifiLastConnectTry > WIFI_TIMEOUT_MS) { } else if (millis() - wifiLastConnectTry > WIFI_TIMEOUT_MS) {
info("WIFI", "WiFi timeout!"); info("WiFi timeout!");
wifiConnect(); wifiConnect();
} }
} }