8000 replace serial print lines with logging macros · adityathakekar/esp8266-react@9e28c30 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e28c30

Browse files
committed
replace serial print lines with logging macros
1 parent d0e878d commit 9e28c30

13 files changed

+84
-86
lines changed

lib/framework/APSettingsService.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,24 @@ void APSettingsService::manageAP() {
4747
}
4848

4949
void APSettingsService::startAP() {
50-
Serial.println(F("Starting software access point"));
50+
LOG_I("Starting software access point");
5151
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
5252
if (!_dnsServer) {
5353
IPAddress apIp = WiFi.softAPIP();
54-
Serial.print(F("Starting captive portal on "));
55-
Serial.println(apIp);
54+
LOGF_I("Starting captive portal on: %s", apIp.toString().c_str());
5655
_dnsServer = new DNSServer;
5756
_dnsServer->start(DNS_PORT, "*", apIp);
5857
}
5958
}
6059

6160
void APSettingsService::stopAP() {
6261
if (_dnsServer) {
63-
Serial.println(F("Stopping captive portal"));
62+
LOG_I("Stopping captive portal");
6463
_dnsServer->stop();
6564
delete _dnsServer;
6665
_dnsServer = nullptr;
6766
}
68-
Serial.println(F("Stopping software access point"));
67+
LOG_I("Stopping software access point");
6968
WiFi.softAPdisconnect(true);
7069
}
7170

lib/framework/APSettingsService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <HttpEndpoint.h>
55
#include <FSPersistence.h>
6+
#include <Logger.h>
67

78
#include <DNSServer.h>
89
#include <IPAddress.h>

lib/framework/Logger.h

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
enum LogLevel { DEBUG = 0, INFO = 1, WARNING = 2, ERROR = 3 };
1111

12-
#define LOGF_D(fmt, ...) Logger::log_P(LogLevel::DEBUG, __FILE__, __LINE__, PSTR(fmt), ##__VA_ARGS__)
13-
#define LOGF_I(fmt, ...) Logger::log_P(LogLevel::INFO, __FILE__, __LINE__, PSTR(fmt), ##__VA_ARGS__)
14-
#define LOGF_W(fmt, ...) Logger::log_P(LogLevel::WARNING, __FILE__, __LINE__, PSTR(fmt), ##__VA_ARGS__)
15-
#define LOGF_E(fmt, ...) Logger::log_P(LogLevel::ERROR, __FILE__, __LINE__, PSTR(fmt), ##__VA_ARGS__)
12+
#define LOGF_D(fmt, ...) Logger::logf(LogLevel::DEBUG, PSTR(__FILE__), __LINE__, PSTR(fmt), ##__VA_ARGS__)
13+
#define LOGF_I(fmt, ...) Logger::logf(LogLevel::INFO, PSTR(__FILE__), __LINE__, PSTR(fmt), ##__VA_ARGS__)
14+
#define LOGF_W(fmt, ...) Logger::logf(LogLevel::WARNING, PSTR(__FILE__), __LINE__, PSTR(fmt), ##__VA_ARGS__)
15+
#define LOGF_E(fmt, ...) Logger::logf(LogLevel::ERROR, PSTR(__FILE__), __LINE__, PSTR(fmt), ##__VA_ARGS__)
1616

17-
#define LOG_D(msg) Logger::log(LogLevel::DEBUG, __FILE__, __LINE__, F(msg))
18-
#define LOG_I(msg) Logger::log(LogLevel::INFO, __FILE__, __LINE__, F(msg))
19-
#define LOG_W(msg) Logger::log(LogLevel::WARNING, __FILE__, __LINE__, F(msg))
20-
#define LOG_E(msg) Logger::log(LogLevel::ERROR, __FILE__, __LINE__, F(msg))
17+
#define LOG_D(msg) Logger::log(LogLevel::DEBUG, PSTR(__FILE__), __LINE__, PSTR(msg))
18+
#define LOG_I(msg) Logger::log(LogLevel::INFO, PSTR(__FILE__), __LINE__, PSTR(msg))
19+
#define LOG_W(msg) Logger::log(LogLevel::WARNING, PSTR(__FILE__), __LINE__, PSTR(msg))
20+
#define LOG_E(msg) Logger::log(LogLevel::ERROR, PSTR(__FILE__), __LINE__, PSTR(msg))
2121

2222
typedef size_t log_event_handler_id_t;
23-
typedef std::function<void(time_t time, LogLevel level, const String& file, const uint16_t line, const String& message)>
23+
typedef std::function<void(time_t time, LogLevel level, const char* file, const uint16_t line, const char* message)>
2424
LogEventHandler;
2525

2626
typedef struct LogEventHandlerInfo {
@@ -51,48 +51,47 @@ class Logger {
5151
}
5252
}
5353

54-
static void log(LogLevel level, char const* file, int line, const String& message) {
54+
static void log(LogLevel level, const char* file, int line, const char* message) {
5555
logEvent(time(nullptr), level, file, line, message);
5656
}
5757

58-
static void log_P(LogLevel level, char const* file, int line, PGM_P format, ...) {
58+
static void logf(LogLevel level, const char* file, int line, const char* format, ...) {
5959
va_list args;
60-
va_start(args, format);
61-
logEvent(time(nullptr), level, file, line, printf_P_toString(format, args));
62-
va_end(args);
63-
}
64-
65-
private:
66-
static std::list<LogEventHandlerInfo> _eventHandlers;
6760

68-
Logger() {
69-
// class is static-only, prevent instantiation
70-
}
71-
72-
static String printf_P_toString(PGM_P format, va_list args) {
7361
// inital buffer, we can extend it if the formatted string doesn't fit
7462
char temp[64];
75-
char* buffer = temp;
76-
// try and read into the buffer, exit early if buffer was large enough
63+
va_start(args, format);
7764
size_t len = vsnprintf_P(temp, sizeof(temp), format, args);
65+
va_end(args);
66+
67+
// buffer was large enough - log and exit early
7868
if (len < sizeof(temp)) {
79-
return buffer;
69+
logEvent(time(nullptr), level, file, line, temp);
70+
return;
8071
}
72+
8173
// create a new buffer of the correct length if possible
82-
buffer = new char[len + 1];
74+
char* buffer = new char[len + 1];
8375
if (buffer) {
84-
// format to the buffer
8576
vsnprintf_P(buffer, len + 1, format, args);
86-
// return the value as a string after releasing the buffer
87-
String value = buffer;
77+
logEvent(time(nullptr), level, file, line, buffer);
8878
delete[] buffer;
89-
return value;
79+
return;
9080
}
91-
return F("Error formatting log message");
81+
82+
// we failed to allocate
83+
logEvent(time(nullptr), level, file, line, PSTR("Error formatting log message"));
84+
}
85+
86+
private:
87+
static std::list<LogEventHandlerInfo> _eventHandlers;
88+
89+
Logger() {
90+
// class is static-only, prevent instantiation
9291
}
9392

9493
private:
95-
static void logEvent(time_t time, LogLevel level, char const* file, int line, const String& message) {
94+
static void logEvent(time_t time, LogLevel level, char const* file, int line, char const* message) {
9695
Serial.print(time);
9796
Serial.print(" ");
9897
Serial.print(level);

lib/framework/MqttSettingsService.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,11 @@ AsyncMqttClient* MqttSettingsService::getMqttClient() {
9292
}
9393

9494
void MqttSettingsService::onMqttConnect(bool sessionPresent) {
95-
Serial.print(F("Connected to MQTT, "));
96-
if (sessionPresent) {
97-
Serial.println(F("with persistent session"));
98-
} else {
99-
Serial.println(F("without persistent session"));
100-
}
95+
LOGF_I("Connected to MQTT, %s persistent session", sessionPresent ? PSTR("with") : PSTR("without"));
10196
}
10297

10398
void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
104-
Serial.print(F("Disconnected from MQTT reason: "));
105-
Serial.println((uint8_t)reason);
99+
LOGF_I("Disconnected from MQTT reason: %d", reason);
106100
_disconnectReason = reason;
107101
_disconnectedAt = millis();
108102
}
@@ -115,28 +109,28 @@ void MqttSettingsService::onConfigUpdated() {
115109
#ifdef ESP32
116110
void MqttSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
117111
if (_state.enabled) {
118-
Serial.println(F("WiFi connection dropped, starting MQTT client."));
112+
LOG_I("Got IP address, starting MQTT client.");
119113
onConfigUpdated();
120114
}
121115
}
122116

123117
void MqttSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
124118
if (_state.enabled) {
125-
Serial.println(F("WiFi connection dropped, stopping MQTT client."));
119+
LOG_I("WiFi connection dropped, stopping MQTT client.");
126120
onConfigUpdated();
127121
}
128122
}
129123
#elif defined(ESP8266)
130124
void MqttSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
131125
if (_state.enabled) {
132-
Serial.println(F("WiFi connection dropped, starting MQTT client."));
126+
LOG_I("Got IP address, starting MQTT client.");
133127
onConfigUpdated();
134128
}
135129
}
136130

137131
void MqttSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
138132
if (_state.enabled) {
139-
Serial.println(F("WiFi connection dropped, stopping MQTT client."));
133+
LOG_I("WiFi connection dropped, stopping MQTT client.");
140134
onConfigUpdated();
141135
}
142136
}
@@ -148,7 +142,7 @@ void MqttSettingsService::configureMqtt() {
148142

149143
// only connect if WiFi is connected and MQTT is enabled
150144
if (_state.enabled && WiFi.isConnected()) {
151-
Serial.println(F("Connecting to MQTT..."));
145+
LOG_I("Connecting to MQTT...");
152146
_mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port);
153147
if (_state.username.length() > 0) {
154148
_mqttClient.setCredentials(

lib/framework/MqttSettingsService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <FSPersistence.h>
77
#include <AsyncMqttClient.h>
88
#include <ESPUtils.h>
9+
#include <Logger.h>
910

1011
#define MQTT_RECONNECTION_DELAY 5000
1112

lib/framework/NTPSettingsService.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,29 @@ void NTPSettingsService::begin() {
3030

3131
#ifdef ESP32
3232
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
33-
Serial.println(F("Got IP address, starting NTP Synchronization"));
33+
LOG_I("Got IP address, starting NTP Synchronization");
3434
configureNTP();
3535
}
3636

3737
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
38-
Serial.println(F("WiFi connection dropped, stopping NTP."));
38+
LOG_I("WiFi connection dropped, stopping NTP.");
3939
configureNTP();
4040
}
4141
#elif defined(ESP8266)
4242
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
43-
Serial.println(F("Got IP address, starting NTP Synchronization"));
43+
LOG_I("Got IP address, starting NTP Synchronization");
4444
configureNTP();
4545
}
4646

4747
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
48-
Serial.println(F("WiFi connection dropped, stopping NTP."));
48+
LOG_I("WiFi connection dropped, stopping NTP.");
4949
configureNTP();
5050
}
5151
#endif
5252

5353
void NTPSettingsService::configureNTP() {
5454
if (WiFi.isConnected() && _state.enabled) {
55-
Serial.println(F("Starting NTP..."));
55+
LOG_I("Starting NTP...");
5656
#ifdef ESP32
5757
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
5858
#elif defined(ESP8266)

lib/framework/NTPSettingsService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <HttpEndpoint.h>
55
#include <FSPersistence.h>
6+
#include <Logger.h>
67

78
#include <time.h>
89
#ifdef ESP32

lib/framework/OTASettingsService.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ void OTASettingsService::loop() {
3030
}
3131
}
3232

33+
static const char* otaErrorMessage(ota_error_t error) {
34+
switch (error) {
35+
case OTA_AUTH_ERROR:
36+
return PSTR("Authentication failed");
37+
case OTA_BEGIN_ERROR:
38+
return PSTR("Begin failed");
39+
case OTA_CONNECT_ERROR:
40+
return PSTR("Connect failed");
41+
case OTA_RECEIVE_ERROR:
42+
return PSTR("Receive failed");
43+
case OTA_END_ERROR:
44+
return PSTR("End failed");
45+
default:
46+
return PSTR("Unknown error");
47+
}
48+
}
49+
3350
void OTASettingsService::configureArduinoOTA() {
3451
if (_arduinoOTA) {
3552
#ifdef ESP32
@@ -39,28 +56,17 @@ void OTASettingsService::configureArduinoOTA() {
3956
_arduinoOTA = nullptr;
4057
}
4158
if (_state.enabled) {
42-
Serial.println(F("Starting OTA Update Service..."));
59+
LOG_I("Starting OTA update service...");
4360
_arduinoOTA = new ArduinoOTAClass;
4461
_arduinoOTA->setPort(_state.port);
4562
_arduinoOTA->setPassword(_state.password.c_str());
46-
_arduinoOTA->onStart([]() { Serial.println(F("Starting")); });
47-
_arduinoOTA->onEnd([]() { Serial.println(F("\r\nEnd")); });
63+
_arduinoOTA->onStart([]() { LOG_I("Starting OTA update"); });
64+
_arduinoOTA->onEnd([]() { LOG_I("Ending OTA update"); });
4865
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
49-
Serial.printf_P(PSTR("Progress: %u%%\r\n"), (progress / (total / 100)));
50-
});
51-
_arduinoOTA->onError([](ota_error_t error) {
52-
Serial.printf("Error[%u]: ", error);
53-
if (error == OTA_AUTH_ERROR)
54-
Serial.println(F("Auth Failed"));
55-
else if (error == OTA_BEGIN_ERROR)
56-
Serial.println(F("Begin Failed"));
57-
else if (error == OTA_CONNECT_ERROR)
58-
Serial.println(F("Connect Failed"));
59-
else if (error == OTA_RECEIVE_ERROR)
60-
Serial.println(F("Receive Failed"));
61-
else if (error == OTA_END_ERROR)
62-
Serial.println(F("End Failed"));
66+
LOGF_I("OTA update progress: %u%%", progress / (total / 100));
6367
});
68+
_arduinoOTA->onError(
69+
[](ota_error_t error) { LOGF_E("OTA update error [%u]: %s", error, otaErrorMessage(error)); });
6470
_arduinoOTA->begin();
6571
}
6672
}

lib/framework/OTASettingsService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <HttpEndpoint.h>
55
#include <FSPersistence.h>
6+
#include <Logger.h>
67

78
#ifdef ESP32
89
#include <ESPmDNS.h>

lib/framework/WiFiSettingsService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void WiFiSettingsService::manageSTA() {
6969
}
7070
// Connect or reconnect as required
7171
if ((WiFi.getMode() & WIFI_STA) == 0) {
72-
LOG_D("Connecting to WiFi.");
72+
LOG_I("Connecting to WiFi.");
7373
if (_state.staticIPConfig) {
7474
// configure for static IP
7575
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2);

lib/framework/WiFiStatus.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,27 @@ WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager)
1818

1919
#ifdef ESP32
2020
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
21-
Serial.println(F("WiFi Connected."));
21+
LOG_I("WiFi connected");
2222
}
2323

2424
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
25-
Serial.print(F("WiFi Disconnected. Reason code="));
26-
Serial.println(info.disconnected.reason);
25+
LOGF_I("WiFi disconnected: reason=%d", info.disconnected.reason);
2726
}
2827

2928
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
30-
Serial.printf_P(
31-
PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
29+
LOGF_I("WiFi got IP: localIP=%s, hostName=%s", WiFi.localIP().toString().c_str(), WiFi.getHostname());
3230
}
3331
#elif defined(ESP8266)
3432
void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
35-
Serial.print(F("WiFi Connected. SSID="));
36-
Serial.println(event.ssid);
33+
LOGF_I("WiFi connected: SSID=%s", event.ssid.c_str());
3734
}
3835

3936
void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
40-
Serial.print(F("WiFi Disconnected. Reason code="));
41-
Serial.println(event.reason);
37+
LOGF_I("WiFi disconnected: reason=%d", event.reason);
4238
}
4339

4440
void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
45-
Serial.printf_P(
46-
PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), event.ip.toString().c_str(), WiFi.hostname().c_str());
41+
LOGF_I("WiFi got IP: localIP=%s, hostName=%s", event.ip.toString().c_str(), WiFi.hostname().c_str());
4742
}
4843
#endif
4944

lib/framework/WiFiStatus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <ESPAsyncWebServer.h>
1515
#include <IPAddress.h>
1616
#include <SecurityManager.h>
17+
#include <Logger.h>
1718

1819
#define MAX_WIFI_STATUS_SIZE 1024
1920
#define WIFI_STATUS_SERVICE_PATH "/rest/wifiStatus"

src/LightStateService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void LightStateService::begin() {
4040
}
4141

4242
void LightStateService::onConfigUpdated() {
43-
LOGF_D("The light is now: %s", _state.ledOn ? "on" : "off");
43+
LOGF_I("The light is now: %s", _state.ledOn ? "on" : "off");
4444
digitalWrite(BLINK_LED, _state.ledOn ? LED_ON : LED_OFF);
4545
}
4646

0 commit comments

Comments
 (0)
0