Skip to content

Commit 871d4bb

Browse files
committed
reworked logging so that it can be overwritten by a custom implementation
1 parent 55a6702 commit 871d4bb

File tree

3 files changed

+62
-56
lines changed

3 files changed

+62
-56
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"files.associations": {
3-
"string": "cpp"
3+
"string": "cpp",
4+
"bitset": "cpp",
5+
"string_view": "cpp",
6+
"regex": "cpp"
47
}
58
}

wifimanager.cpp

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
#include <Preferences.h>
1818

1919

20-
void logMsg(String msg) {
20+
/**
21+
* @brief Write a message to the Serial interface
22+
* @param msg The message to be written
23+
*
24+
* This function is a simple wrapper around Serial.print() to write a message
25+
* to the serial console. It can be overwritten by a custom implementation for
26+
* enhanced logging.
27+
*/
28+
void WIFIMANAGER::logMessage(String msg) {
2129
Serial.print(msg);
2230
}
2331

@@ -64,7 +72,7 @@ void WIFIMANAGER::startBackgroundTask(String softApName, String softApPass) {
6472
);
6573

6674
if (taskCreated != pdPASS) {
67-
Serial.println("[ERROR] WifiManager: Error creating background task");
75+
logMessage("[ERROR] WifiManager: Error creating background task\n");
6876
}
6977
}
7078

@@ -78,15 +86,15 @@ WIFIMANAGER::WIFIMANAGER(const char * ns) {
7886

7987
// AP on/off
8088
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
81-
Serial.println(F("[WIFI] onEvent() AP mode started!"));
89+
logMessage("[WIFI] onEvent() AP mode started!\n");
8290
softApRunning = true;
8391
#if ESP_ARDUINO_VERSION_MAJOR >= 2
8492
}, ARDUINO_EVENT_WIFI_AP_START); // arduino-esp32 2.0.0 and later
8593
#else
8694
}, SYSTEM_EVENT_AP_START); // arduino-esp32 1.0.6
8795
#endif
8896
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
89-
Serial.println(F("[WIFI] onEvent() AP mode stopped!"));
97+
logMessage("[WIFI] onEvent() AP mode stopped!\n");
9098
softApRunning = false;
9199
#if ESP_ARDUINO_VERSION_MAJOR >= 2
92100
}, ARDUINO_EVENT_WIFI_AP_STOP); // arduino-esp32 2.0.0 and later
@@ -95,14 +103,14 @@ WIFIMANAGER::WIFIMANAGER(const char * ns) {
95103
#endif
96104
// AP client join/leave
97105
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
98-
Serial.println(F("[WIFI] onEvent() New client connected to softAP!"));
106+
logMessage("[WIFI] onEvent() new client connected to softAP!\n");
99107
#if ESP_ARDUINO_VERSION_MAJOR >= 2
100108
}, ARDUINO_EVENT_WIFI_AP_STACONNECTED); // arduino-esp32 2.0.0 and later
101109
#else
102110
}, SYSTEM_EVENT_AP_STACONNECTED); // arduino-esp32 1.0.6
103111
#endif
104112
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
105-
Serial.println(F("[WIFI] onEvent() Client disconnected from softAP!"));
113+
logMessage("[WIFI] onEvent() Client disconnected from softAP!\n");
106114
#if ESP_ARDUINO_VERSION_MAJOR >= 2
107115
}, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED); // arduino-esp32 2.0.0 and later
108116
#else
@@ -165,7 +173,7 @@ bool WIFIMANAGER::loadFromNVS() {
165173
if (apName.length() > 0) {
166174
sprintf(tmpKey, "apPass%d", i);
167175
String apPass = preferences.getString(tmpKey);
168-
Serial.printf("[WIFI] Load SSID '%s' to %d. slot.\n", apName.c_str(), i+1);
176+
logMessage(String("[WIFI] Load SSID '") + apName + "' to " + String(i+1) + ". slot.\n");
169177
apList[i].apName = apName;
170178
apList[i].apPass = apPass;
171179
configuredSSIDs++;
@@ -175,7 +183,7 @@ bool WIFIMANAGER::loadFromNVS() {
175183
preferences.end();
176184
return true;
177185
}
178-
Serial.println(F("[WIFI] Unable to load data from NVS, giving up..."));
186+
logMessage("[WIFI] Unable to load data from NVS, giving up...\n");
179187
return false;
180188
}
181189

@@ -186,7 +194,7 @@ bool WIFIMANAGER::loadFromNVS() {
186194
*/
187195
bool WIFIMANAGER::writeToNVS() {
188196
if (!preferences.begin(NVS, false)) {
189-
Serial.println(F("[WIFI] Unable to write data to NVS, giving up..."));
197+
logMessage("[WIFI] Unable to write data to NVS, giving up...");
190198
return false;
191199
}
192200

@@ -216,26 +224,26 @@ bool WIFIMANAGER::writeToNVS() {
216224
*/
217225
bool WIFIMANAGER::addWifi(String apName, String apPass, bool updateNVS) {
218226
if(apName.length() < 1 || apName.length() > 31) {
219-
Serial.println(F("[WIFI] No SSID given or ssid too long"));
220-
return false;
227+
logMessage("[WIFI] No SSID given or ssid too long");
228+
return false;
221229
}
222230

223231
if(apPass.length() > 63) {
224-
Serial.println(F("[WIFI] Passphrase too long"));
225-
return false;
232+
logMessage("[WIFI] Passphrase too long");
233+
return false;
226234
}
227235

228236
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
229237
if (apList[i].apName == "") {
230-
Serial.printf("[WIFI] Found unused slot Nr. %d to store the new SSID '%s' credentials.\n", i, apName.c_str());
238+
logMessage(String("[WIFI] Found unused slot Nr. ") + String(i) + " to store the new SSID '" + apName + "' credentials.\n");
231239
apList[i].apName = apName;
232240
apList[i].apPass = apPass;
233241
configuredSSIDs++;
234242
if (updateNVS) return writeToNVS();
235243
else return true;
236244
}
237245
}
238-
Serial.println(F("[WIFI] No slot available to store SSID credentials"));
246+
logMessage("[WIFI] No slot available to store SSID credentials");
239247
return false; // max entries reached
240248
}
241249

@@ -290,8 +298,8 @@ uint8_t WIFIMANAGER::getApEntry() {
290298
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
291299
if (apList[i].apName.length()) return i;
292300
}
293-
Serial.print(F("[WIFI][ERROR] We did not find a valid entry!"));
294-
Serial.print(F("[WIFI][ERROR] Make sure to not call this function if configuredSSIDs != 1."));
301+
logMessage("[WIFI][ERROR] We did not find a valid entry!\n");
302+
logMessage("[WIFI][ERROR] Make sure to not call this function if configuredSSIDs != 1.\n");
295303
return 0;
296304
}
297305

@@ -307,35 +315,31 @@ void WIFIMANAGER::loop() {
307315
// Check if we are connected to a well known SSID
308316
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
309317
if (WiFi.SSID() == apList[i].apName) {
310-
Serial.printf("[WIFI][STATUS] Connected to known SSID: '%s' with IP %s.\n",
311-
WiFi.SSID().c_str(),
312-
WiFi.localIP().toString().c_str()
313-
);
318+
logMessage(String("[WIFI][STATUS] Connected to known SSID: '") + WiFi.SSID() + "' with IP " + WiFi.localIP().toString() + "\n");
314319
return;
315320
}
316321
}
317322
// looks like we are connected to something else, strange!?
318-
Serial.print(F("[WIFI] We are connected to an unknown SSID ignoring. Connected to: "));
319-
Serial.println(WiFi.SSID());
323+
logMessage("[WIFI] We are connected to an unknown SSID ignoring. Connected to: " + WiFi.SSID() + "\n");
320324
} else {
321325
if (softApRunning) {
322-
Serial.printf("[WIFI] Not trying to connect to a known SSID. SoftAP has %d clients connected!\n", WiFi.softAPgetStationNum());
326+
logMessage("[WIFI] Not trying to connect to a known SSID. SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
323327
} else {
324328
// let's try to connect to some WiFi in Range
325329
if (!tryConnect()) {
326330
if (createFallbackAP) runSoftAP();
327-
else Serial.println(F("[WIFI] Auto creation of SoftAP is disabled, no starting AP!"));
331+
else logMessage("[WIFI] Auto creation of SoftAP is disabled, no starting AP!\n");
328332
}
329333
}
330334
}
331335

332336
if (softApRunning && millis() - startApTimeMillis > timeoutApMillis) {
333337
if (WiFi.softAPgetStationNum() > 0) {
334-
Serial.printf("[WIFI] SoftAP has %d clients connected!\n", WiFi.softAPgetStationNum());
338+
logMessage("[WIFI] SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
335339
startApTimeMillis = millis(); // reset timeout as someone is connected
336340
return;
337341
}
338-
Serial.println(F("[WIFI] Running in AP mode but timeout reached. Closing AP!"));
342+
logMessage("[WIFI] Running in AP mode but timeout reached. Closing AP!\n");
339343
stopSoftAP();
340344
delay(100);
341345
}
@@ -349,13 +353,13 @@ void WIFIMANAGER::loop() {
349353
*/
350354
bool WIFIMANAGER::tryConnect() {
351355
if (!configAvailable()) {
352-
Serial.println(F("[WIFI] No SSIDs configured in NVS, unable to connect."));
356+
logMessage("[WIFI] No SSIDs configured in NVS, unable to connect\n");
353357
if (createFallbackAP) runSoftAP();
354358
return false;
355359
}
356360

357361
if (softApRunning) {
358-
Serial.printf("[WIFI] Not trying to connect. SoftAP has %d clients connected!\n", WiFi.softAPgetStationNum());
362+
logMessage("[WIFI] Not trying to connect. SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
359363
return false;
360364
}
361365

@@ -367,11 +371,10 @@ bool WIFIMANAGER::tryConnect() {
367371
WiFi.mode(WIFI_STA);
368372
int8_t scanResult = WiFi.scanNetworks(false, true);
369373
if(scanResult <= 0) {
370-
Serial.println(F("[WIFI] Unable to find WIFI networks in range to this device!"));
374+
logMessage("[WIFI] Unable to find WIFI networks in range to this device!\n");
371375
return false;
372376
}
373-
Serial.print(F("[WIFI] Found networks: "));
374-
Serial.println(scanResult);
377+
logMessage(String("[WIFI] Found ") + String(scanResult) + " networks in range\n");
375378
int choosenRssi = INT_MIN; // we want to select the strongest signal with the highest priority if we have multiple SSIDs available
376379
for(int8_t x = 0; x < scanResult; ++x) {
377380
String ssid;
@@ -395,12 +398,11 @@ bool WIFIMANAGER::tryConnect() {
395398
}
396399

397400
if (choosenAp == INT_MIN) {
398-
Serial.println(F("[WIFI] Unable to find an SSID to connect to!"));
401+
logMessage("[WIFI] Unable to find an SSID to connect to!\n");
399402
return false;
400403
} else {
401-
Serial.printf("[WIFI] Trying to connect to SSID %s with password %s.\n",
402-
apList[choosenAp].apName.c_str(),
403-
(apList[choosenAp].apPass.length() > 0 ? "'***'" : "''")
404+
logMessage(String("[WIFI] Trying to connect to SSID ") + apList[choosenAp].apName
405+
+ " with password " + (apList[choosenAp].apPass.length() > 0 ? "'***'" : "''") + "\n"
404406
);
405407

406408
WiFi.begin(apList[choosenAp].apName.c_str(), apList[choosenAp].apPass.c_str());
@@ -414,36 +416,35 @@ bool WIFIMANAGER::tryConnect() {
414416
}
415417
switch(status) {
416418
case WL_IDLE_STATUS:
417-
Serial.println(F("[WIFI] Connecting failed (0): Idle"));
419+
logMessage("[WIFI] Connecting failed (0): Idle\n");
418420
break;
419421
case WL_NO_SSID_AVAIL:
420-
Serial.println(F("[WIFI] Connection failed (1): The AP can't be found."));
422+
logMessage("[WIFI] Connecting failed (1): The AP can't be found\n");
421423
break;
422424
case WL_SCAN_COMPLETED:
423-
Serial.println(F("[WIFI] Connecting failed (2): Scan completed"));
425+
logMessage("[WIFI] Connecting failed (2): Scan completed\n");
424426
break;
425427
case WL_CONNECTED: // 3
426-
Serial.println(F("[WIFI] Connection successful."));
427-
Serial.printf("[WIFI] SSID : %s\n", WiFi.SSID().c_str());
428-
Serial.printf("[WIFI] IP : %s\n", WiFi.localIP().toString().c_str());
429-
428+
logMessage("[WIFI] Connection successful\n");
429+
logMessage("[WIFI] SSID : " + WiFi.SSID() + "\n");
430+
logMessage("[WIFI] IP : " + WiFi.localIP().toString() + "\n");
430431
stopSoftAP();
431432
return true;
432433
break;
433434
case WL_CONNECT_FAILED:
434-
Serial.println(F("[WIFI] Connecting failed (4): Unknown reason"));
435+
logMessage("[WIFI] Connecting failed (4): Unknown reason\n");
435436
break;
436437
case WL_CONNECTION_LOST:
437-
Serial.println(F("[WIFI] Connecting failed (5): Connection lost"));
438+
logMessage("[WIFI] Connecting failed (5): Connection lost\n");
438439
break;
439440
case WL_DISCONNECTED:
440-
Serial.println(F("[WIFI] Connecting failed (6): Disconnected"));
441+
logMessage("[WIFI] Connecting failed (6): Disconnected\n");
441442
break;
442443
case WL_NO_SHIELD:
443-
Serial.println(F("[WIFI] Connecting failed (255): No Wifi shield found"));
444+
logMessage("[WIFI] Connecting failed (7): No Wifi shield found\n");
444445
break;
445446
default:
446-
Serial.printf("[WIFI] Connecting Failed (Status: %d).\n", status);
447+
logMessage("[WIFI] Connecting failed (" + String(status) + "): Unknown status code\n");
447448
break;
448449
}
449450
}
@@ -469,17 +470,16 @@ bool WIFIMANAGER::runSoftAP(String apName, String apPass) {
469470
startApTimeMillis = millis();
470471

471472
if (this->softApName == "") this->softApName = "ESP_" + String((uint32_t)ESP.getEfuseMac());
472-
Serial.printf("[WIFI] Starting configuration portal on AP SSID %s\n", this->softApName.c_str());
473+
logMessage("[WIFI] Starting configuration portal on AP SSID " + this->softApName + "\n");
473474

474475
WiFi.mode(WIFI_AP);
475476
bool state = WiFi.softAP(this->softApName.c_str(), (this->softApPass.length() ? this->softApPass.c_str() : NULL));
476477
if (state) {
477478
IPAddress IP = WiFi.softAPIP();
478-
Serial.print(F("[WIFI] AP created. My IP is: "));
479-
Serial.println(IP);
479+
logMessage("[WIFI] AP created. My IP is: " + String(IP) + "\n");
480480
return true;
481481
} else {
482-
Serial.println(F("[WIFI] Unable to create soft AP!"));
482+
logMessage("[WIFI] Unable to create SoftAP!\n");
483483
return false;
484484
}
485485
}
@@ -547,7 +547,7 @@ void WIFIMANAGER::attachWebServer(WebServer * srv) {
547547
message += "path=";
548548
message += webServer->arg("path");
549549
message += '\n';
550-
Serial.print(message);
550+
logMessage(message);
551551
});
552552
#endif
553553

wifimanager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
void wifiTask(void* param);
2929

3030
class WIFIMANAGER {
31-
private:
31+
protected:
3232
#if ASYNC_WEBSERVER == true
3333
AsyncWebServer * webServer; // The Webserver to register routes on
3434
#else
@@ -62,7 +62,10 @@ class WIFIMANAGER {
6262
void clearApList();
6363

6464
// Get id of the first non empty entry
65-
uint8_t getApEntry();
65+
uint8_t getApEntry()
66+
67+
// Print a log message to Serial, can be overwritten
68+
void logMessage(String msg);
6669

6770
public:
6871
// We let the loop run as as Task

0 commit comments

Comments
 (0)