-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlogger.cpp
331 lines (282 loc) · 9.52 KB
/
logger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* ======================================================================
This file is part of disk91_logger.
disk91_logger is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
=======================================================================
*/
/* ======================================================================
* LOGGER / ESP8266 / logging module
* ----------------------------------------------------------------------
* (c) Disk91 - 2018
* Author : Paul Pinault aka disk91.com
* ----------------------------------------------------------------------
*/
#include "logger.h"
#include "config.h"
LoggerClass _log;
/**
* Init the logger structure from a given configuration
* The configuration is a 32bits field for each of the
* 4 possible logging option with the 4 level of trace.
* This configuration is optimize to be stored in the application
* configuration and context to be easily saved & restore when
* going deep sleep.
* Format is:
* +-------------------------------------------------------------------+
* | FILE output | SoftSerial output | Serial1 output | Serial output |
* +-------------------------------------------------------------------+
* | D I W E | D I W E | D I W E | D I W E |
* +-------------------------------------------------------------------+
* D : DEBUG Level - activated when 1 / Mask when 0
* I : INFO Level - activated when 1 / Mask when 0
* W : WARN Level - activated when 1 / Mask when 0
* E : ERROR Level - activated when 1 / Mask when 0
*/
bool LoggerClass::init(uint16_t config) {
this->logError = ( config & LOGGER_CONFIG_ERROR_LVL_MASK );
this->logWarn = ( config & LOGGER_CONFIG_WARN_LVL_MASK );
this->logInfo = ( config & LOGGER_CONFIG_INFO_LVL_MASK );
this->logDebug = ( config & LOGGER_CONFIG_DEBUG_LVL_MASK );
this->onSerial = ( config & LOGGER_CONFIG_SERIAL_MASK );
this->onSerial1 = ( config & LOGGER_CONFIG_SERIAL1_MASK );
this->onSSerial = ( config & LOGGER_CONFIG_SSERIAL_MASK );
this->onFile = ( config & LOGGER_CONFIG_FILE_MASK );
// Init the loggers
if (this->onSerial) {
if ( !Serial ) {
Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED);
}
if ( Serial.baudRate() != LOGGER_SERIAL_DEFAULT_SPEED) {
Serial.begin(LOGGER_SERIAL_DEFAULT_SPEED);
}
}
if (this->onSerial1) {
Serial1.begin(LOGGER_SERIAL1_DEFAULT_SPEED);
}
if (this->onSSerial) {
if ( SSerial == NULL ) SSerial = new SoftwareSerial(DEBUG_SOFTSERIAL_RX_PIN, DEBUG_SOFTSERIAL_TX_PIN, false, 256); // RX, TX, Invert, Buffer Size
SSerial->begin(9600);
}
if (this->onFile) {
if ( !SPIFFS.begin() || !SPIFFS.exists("/formatComplete.txt") ) {
// format fs
SPIFFS.format();
File f = SPIFFS.open("/formatComplete.txt", "w");
f.close();
}
this->logFile = SPIFFS.open("/log.txt", "a");
if ( this->logFile ) {
if ( this->logFile.size() >= LOGGER_FILE_MAX_SIZE ) {
this->logFile.close();
SPIFFS.remove("/log.txt");
this->logFile = SPIFFS.open("/log.txt", "a");
}
}
if ( !this->logFile ) {
// problem, disable file logging
this->onFile = false;
}
}
this->logConf = config;
this->ready = true;
return true;
}
/**
* Terminate the logging. This should be called before going deep-sleep to flush
* Current log processing.
*/
uint16_t LoggerClass::close() {
if ( this->onFile) {
this->logFile.close();
SPIFFS.end();
}
if ( this->onSerial) {
Serial.flush();
}
if ( this->onSerial1) {
Serial1.flush();
}
if ( this->onSSerial) {
SSerial->flush();
}
this->ready=false;
return this->logConf;
}
/**
* Print the log file over the serial line. As this is usually called during the
* sleeping loop the SPIFF is supposed to be closed. The function try to manage this and
* restore the initial state.
*/
void LoggerClass::cat() {
if ( this->ready && this->onFile) {
this->logFile.close();
}
if ( !this->ready ) {
SPIFFS.begin();
}
this->logFile = SPIFFS.open("/log.txt", "r");
if (this->logFile) {
Serial.printf("====== Read Log File (%db)=======\n",this->logFile.size());
while ( this->logFile.available() ) {
Serial.print((char)this->logFile.read());
}
Serial.println("====== end of Log File =======\n");
this->logFile.close();
}
if ( this->ready ) {
this->logFile = SPIFFS.open("/log.txt", "a");
} else {
SPIFFS.end();
}
}
/**
* Purge the log file
* sleeping loop the SPIFF is supposed to be closed. The function try to manage this and
* restore the initial state.
*/
void LoggerClass::clean() {
if ( this->ready && this->onFile) {
this->logFile.close();
}
if ( !this->ready ) {
SPIFFS.begin();
}
SPIFFS.remove("/log.txt");
if ( this->ready ) {
this->logFile = SPIFFS.open("/log.txt", "a");
} else {
this->logFile.close();
SPIFFS.end();
}
}
/**
* Log an error according to the configuration on the different
* possible logger
*/
void LoggerClass::error(char *format, ...) {
va_list args;
if ( this->logError && this->ready ) {
va_start(args,format);
vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args);
va_end(args);
if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) {
Serial.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) {
Serial1.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) {
SSerial->print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_ERROR_LVL_MASK ) {
this->logFile.printf("%lu [error] ",millis());
this->logFile.print(fmtBuffer);
}
}
}
/**
* Log a warning according to the configuration on the different
* possible logger
*/
void LoggerClass::warn(char *format, ...) {
va_list args;
if ( this->logWarn && this->ready ) {
va_start(args,format);
vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args);
va_end(args);
if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) {
Serial.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) {
Serial1.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) {
SSerial->print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_WARN_LVL_MASK ) {
this->logFile.printf("%lu [warn ] ",millis());
this->logFile.print(fmtBuffer);
}
}
}
/**
* Log a info according to the configuration on the different
* possible logger
*/
void LoggerClass::info(char *format, ...) {
va_list args;
if ( this->logInfo && this->ready ) {
va_start(args,format);
vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args);
va_end(args);
if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) {
Serial.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) {
Serial1.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) {
SSerial->print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_INFO_LVL_MASK ) {
this->logFile.printf("%lu [info ] ",millis());
this->logFile.print(fmtBuffer);
}
}
}
/**
* Log a debug according to the configuration on the different
* possible logger
*/
void LoggerClass::debug(char *format, ...) {
va_list args;
if ( this->logDebug && this->ready ) {
va_start(args,format);
vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args);
va_end(args);
if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) {
Serial.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) {
Serial1.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) {
SSerial->print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_FILE_MASK & LOGGER_CONFIG_DEBUG_LVL_MASK ) {
this->logFile.printf("%lu [debug] ",millis());
this->logFile.print(fmtBuffer);
}
}
}
/**
* Log a debug according to the configuration on the different
* possible logger
*/
void LoggerClass::any(char *format, ...) {
va_list args;
va_start(args,format);
vsnprintf(fmtBuffer,LOGGER_MAX_BUF_SZ,format,args);
va_end(args);
if ( this->logConf & LOGGER_CONFIG_SERIAL_MASK ) {
Serial.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SERIAL1_MASK ) {
Serial1.print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_SSERIAL_MASK ) {
SSerial->print(fmtBuffer);
}
if ( this->logConf & LOGGER_CONFIG_FILE_MASK ) {
this->logFile.printf("%lu [any ] ",millis());
this->logFile.print(fmtBuffer);
}
}