Rework logging
Some checks failed
Test compiling project / test (push) Failing after 1m34s

This commit is contained in:
2025-03-10 23:33:41 +01:00
parent 374b23d99a
commit eeaac9548e
12 changed files with 111 additions and 121 deletions

View File

@@ -8,7 +8,7 @@
#include "AsyncJson.h"
#include <ArduinoJson.h>
#include <ArduinoLog.h>
#include <Elog.h>
#include <ArduinoOTA.h>
@@ -26,6 +26,7 @@
#include <fetchOTA.h>
#include <ESP32Ping.h>
#define MYLOG 0
Preferences prefs;
@@ -40,19 +41,18 @@ extern "C" int rom_phy_get_vdd33();
Version current_spiffs_version;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
AsyncWebSocket webSocket("/webSocket");
#define FORMAT_LITTLEFS_IF_FAILED true
void setup()
{
{
logger.registerSerial(MYLOG, DEBUG, "tst");
logger.
logger.provideTime(2025, 3, 10, 23, 28, 00);
prefs.begin("waterlevel", false);
Serial.begin(115200);
Log.begin(LOG_LEVEL_VERBOSE, &Serial);
Log.setSuffix(printSuffix);
Log.setPrefix(print_prefix);
Log.verbose("Init LEDs");
logger.log(0, DEBUG, "Init LEDs");
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
@@ -64,7 +64,7 @@ void setup()
display_error_code(17);
init_sensor();
Log.verbose("Beginning SPIFFS");
logger.log(0, DEBUG, "Beginning SPIFFS");
SPIFFS.begin(true);
// Read the current SPIFFS version from the versions file on the spiffs
@@ -73,21 +73,21 @@ void setup()
Serial.println("Failed to open version file for reading");
} else {
String version = file.readStringUntil('\n');
Log.verbose("Version: %s", version);
logger.log(0, DEBUG, "Version: %s", version);
current_spiffs_version = parseVersion(version.c_str());
Log.verbose("Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
logger.log(0, DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
}
Log.verbose("SPIFFS initialized");
logger.log(0, DEBUG, "SPIFFS initialized");
display_error_code(19);
Log.verbose("Begin INA");
logger.log(0, DEBUG, "Begin INA");
display_error_code(22);
/////////////////////////////// ROUTES ///////////////////////////////
Log.verbose("Route Setup");
logger.log(0, DEBUG, "Route Setup");
// Normal HTML stuff
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/status.html", "text/html", false, processor); });
@@ -106,7 +106,7 @@ void setup()
// For settings SSID
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
Log.verbose("Updating SSID config");
logger.log(0, DEBUG, "Updating SSID config");
AsyncWebParameter* ssid_param = request->getParam(ssid_key, true);
AsyncWebParameter* password_param = request->getParam(wifi_password_key, true);
prefs.putString(ssid_key, ssid_param->value().c_str());
@@ -122,7 +122,7 @@ void setup()
int params = request->params();
if (request->hasParam(level_sensor_range_key, true) && request->hasParam(water_level_min_key, true) && request->hasParam(water_level_max_key, true) && request->hasParam(water_volume_key, true)) {
Log.verbose("Updating Sensor config");
logger.log(0, DEBUG, "Updating Sensor config");
AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true);
AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true);
AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true);
@@ -144,24 +144,24 @@ void setup()
// Convert the C string to a float using strtod
float value = strtod(paramCStr, &endPtr);
Log.verbose("range_float:%D:", range_float);
logger.log(0, DEBUG, "range_float:%D:", range_float);
prefs.putFloat(level_sensor_range_key, range_float);
prefs.putFloat(water_level_min_key, level_min_float);
prefs.putFloat(water_level_max_key, level_max_float);
prefs.putFloat(water_volume_key, liters_float);
Log.verbose("range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0));
logger.log(0, DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0));
} else {
Log.verbose("!!!! FAIL lo");
logger.log(0, DEBUG, "!!!! FAIL lo");
for (int i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
if (p->isFile()) { // p->isPost() is also true
Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else if (p->isPost()) {
Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else {
Log.verbose("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
logger.log(0, DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(400, "text/plain", "Missing parameters"); // TODO add proper error messages
@@ -171,8 +171,8 @@ void setup()
setup_api_endpoints();
display_error_code(23);
ws.onEvent(onWsEvent);
server.addHandler(&ws);
webSocket.onEvent(onWsEvent);
server.addHandler(&webSocket);
@@ -181,7 +181,7 @@ void setup()
display_error_code(24);
Log.verbose("OTA Setup");
logger.log(0, DEBUG, "OTA Setup");
ArduinoOTA
.onStart([]() {
String type;
@@ -191,16 +191,16 @@ void setup()
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Log.verbose("Start updating %s", type); })
.onEnd([]() { Log.verbose("\nEnd"); })
logger.log(0, DEBUG, "Start updating %s", type); })
.onEnd([]() { logger.log(0, DEBUG, "\nEnd"); })
.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Log.verbose("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Log.verbose("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Log.verbose("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Log.verbose("Receive Failed");
else if (error == OTA_END_ERROR) Log.verbose("End Failed"); });
if (error == OTA_AUTH_ERROR) logger.log(0, DEBUG, "Auth Failed");
else if (error == OTA_BEGIN_ERROR) logger.log(0, DEBUG, "Begin Failed");
else if (error == OTA_CONNECT_ERROR) logger.log(0, DEBUG, "Connect Failed");
else if (error == OTA_RECEIVE_ERROR) logger.log(0, DEBUG, "Receive Failed");
else if (error == OTA_END_ERROR) logger.log(0, DEBUG, "End Failed"); });
display_error_code(26);
digitalWrite(LED_RED, 0);
@@ -216,11 +216,13 @@ void setup()
if (!started) {
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
}
logger.configureSyslog("192.168.6.11", 5514, "esp32");
logger.registerSyslog(MYLOG, DEBUG, FAC_LOCAL4, "mylog");
Log.verbose("Starting webserver");
logger.log(0, DEBUG, "Starting webserver");
server.begin();
ArduinoOTA.begin();
display_error_code(25);
@@ -234,13 +236,13 @@ void setup()
if (WiFi.status() == WL_CONNECTED || ETH.localIP()) {
int pingResult = Ping.ping("8.8.8.8"); // Use Google's public DNS server as a test IP
if (pingResult >= 0) {
Log.verbose("Network connection established");
logger.log(0, DEBUG, "Network connection established");
break;
} else {
Log.verbose("Network not ready, retrying...");
logger.log(0, DEBUG, "Network not ready, retrying...");
}
} else {
Log.verbose("No WiFi or Ethernet connection, retrying...");
logger.log(0, DEBUG, "No WiFi or Ethernet connection, retrying...");
}
delay(1000); // Delay to prevent rapid retry
}