forked from thijse/Arduino-Log
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArduinoLog.h
More file actions
485 lines (425 loc) · 13.4 KB
/
ArduinoLog.h
File metadata and controls
485 lines (425 loc) · 13.4 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
_ ___ ___ _ _ ___ _ _ ___ _ ___ ___
/_\ | _ \ \| | | |_ _| \| |/ _ \| | / _ \ / __|
/ _ \| / |) | |_| || || .` | (_) | |_| (_) | (_ |
/_/ \_\_|_\___/ \___/|___|_|\_|\___/|____\___/ \___|
Log library for Arduino
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
*/
#ifndef _ARDUINO_LOG_H_
#define _ARDUINO_LOG_H_
#include <inttypes.h>
#include <stdarg.h>
// Mutex is supported only on ESP32 boards.
#ifdef ESP32
#include <mutex>
#endif
// Non standard: Arduino.h also chosen if ARDUINO is not defined.
// To facilitate use in non-Arduino test environments
#if ARDUINO < 100
#include "WProgram.h"
#else
#include "Arduino.h"
#endif
// PGM stubs to facilitate use in non-Arduino test environments
#ifndef PGM_P
#define PGM_P const char *
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define PSTR(str) (str)
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
#endif
typedef void (*printfunction)(Print*, int);
// This allows Arduino-Log to output the stream to multiple outputs.
// If you need more than 5 outputs at a time, re-define this constant.
#define LOG_MAX_HANDLERS 5
// *************************************************************************
// Uncomment line below to fully disable logging, and reduce project size
// ************************************************************************
//#define DISABLE_LOGGING
#define LOG_LEVEL_SILENT 0
#define LOG_LEVEL_FATAL 1
#define LOG_LEVEL_ERROR 2
#define LOG_LEVEL_WARNING 3
#define LOG_LEVEL_INFO 4
#define LOG_LEVEL_NOTICE 4
#define LOG_LEVEL_TRACE 5
#define LOG_LEVEL_VERBOSE 6
#define LOG_COLOR_BOLD_RED F("\033[1;31m")
#define LOG_COLOR_BOLD_WHITE F("\033[1;37m")
#define LOG_COLOR_RED "\033[;31m"
#define LOG_COLOR_YELLOW "\033[;33m"
#define LOG_COLOR_GREEN "\033[;32m"
#define LOG_COLOR_WHITE "\033[;37m"
#define LOG_COLOR_BLACK "\033[;38;5;240m"
#define LOG_COLOR_END "\033[0m"
#ifndef ARDUINOLOG_DISABLE_LINE_END_DEFINES
#ifndef CR
#define CR "\n"
#else
#warning "CR already defined"
#endif
#ifndef LF
#define LF "\r"
#else
#warning "LF already defined"
#endif
#ifndef NL
#define NL "\n\r"
#else
#warning "NL already defined"
#endif
#endif
#define LOGGING_VERSION 1_0_4
/**
* ArduinoLog is a minimalistic framework to help the programmer output log statements to an output of choice,
* fashioned after extensive logging libraries such as log4cpp ,log4j and log4net. In case of problems with an
* application, it is helpful to enable logging so that the problem can be located. ArduinoLog is designed so
* that log statements can remain in the code with minimal performance cost. In order to facilitate this the
* loglevel can be adjusted, and (if your code is completely tested) all logging code can be compiled out.
*
* ---- Wildcards
*
* %s display as string (char*)
* %S display as string from flash memory (__FlashStringHelper* or char[] PROGMEM)
* %c display as single character
* %C display as single character or as hexadecimal value (prefixed by `0x`) if not a printable character
* %d display as integer value
* %l display as long value
* %u display as unsigned long value
* %x display as hexadecimal value
* %X display as hexadecimal value prefixed by `0x` and leading zeros
* %b display as binary number
* %B display as binary number, prefixed by `0b`
* %t display as boolean value "t" or "f"
* %T display as boolean value "true" or "false"
* %D,%F display as double value
* %p display a printable object
*
* ---- Loglevels
*
* 0 - LOG_LEVEL_SILENT no output
* 1 - LOG_LEVEL_FATAL fatal errors
* 2 - LOG_LEVEL_ERROR all errors
* 3 - LOG_LEVEL_WARNING errors and warnings
* 4 - LOG_LEVEL_INFO errors, warnings and notices
* 4 - LOG_LEVEL_NOTICE Same as INFO, kept for backward compatibility
* 5 - LOG_LEVEL_TRACE errors, warnings, notices, traces
* 6 - LOG_LEVEL_VERBOSE all
*/
class Logging {
public:
static constexpr char lineEnd[] = "\n";
explicit Logging();
/**
* Initializing, must be called as first. Note that if you use
* this variant of Init, you need to initialize the baud rate
* yourself, if printer happens to be a serial port.
*
* \param level - logging levels <= this will be logged.
* \param printer - place that logging output will be sent to.
* \param showLevel - if true, the level will be shown in the output.
* \param showColors - if true, the color will be shown in the output.
* \return void
*
*/
void begin(int level, Print *output, bool showLevel = true, bool showColors = false);
/**
* Set the log level.
*
* \param level - The new log level.
* \return void
*/
void setLevel(int level);
/**
* Get the log level.
*
* \return The current log level.
*/
int getLevel() const;
/**
* Set whether to show the log level.
*
* \param showLevel - true if the log level should be shown for each log
* false otherwise.
* \return void
*/
void setShowLevel(bool showLevel);
/**
* Get whether the log level is shown during logging
*
* \return true if the log level is be shown for each log
* false otherwise.
*/
bool getShowLevel() const;
/**
* Set whether to show the log colors.
*
* \param showColors - true if the log colors should be shown for each log
* false otherwise.
* \return void
*/
void setShowColors(bool showColors);
/**
* Get whether the log colors are shown during logging
*
* \return true if the log colors are shown for each log
* false otherwise.
*/
bool getShowColors() const;
/**
* Sets a function to be called before each log command.
*
* \param f - The function to be called
* \return void
*/
void setPrefix(printfunction f);
/**
* clears prefix.
*
* \return void
*/
void clearPrefix();
/**
* Sets a function to be called after each log command.
*
* \param f - The function to be called
* \return void
*/
void setSuffix(printfunction f);
/**
* clears suffix.
*
* \return void
*/
void clearSuffix();
/**
* Adds a new handler which outputs the stream to another source.
* For example, you can output to Serial and into a file at the same time.
*/
void addHandler(Print* handler);
/**
* Removes stream handler and stops outputting the data.
*/
void removeHandler(Print* handler);
/**
* Output a fatal error message. Output message contains
* F: followed by original message
* Fatal error messages are printed out at
* loglevels >= LOG_LEVEL_FATAL
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void fatal(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_FATAL, false, msg, args...);
#endif
}
template <class T, typename... Args> void fatalln(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_FATAL, true, msg, args...);
#endif
}
/**
* Output an error message. Output message contains
* E: followed by original message
* Error messages are printed out at
* loglevels >= LOG_LEVEL_ERROR
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void error(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_ERROR, false, msg, args...);
#endif
}
template <class T, typename... Args> void errorln(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_ERROR, true, msg, args...);
#endif
}
/**
* Output a warning message. Output message contains
* W: followed by original message
* Warning messages are printed out at
* loglevels >= LOG_LEVEL_WARNING
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void warning(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_WARNING, false, msg, args...);
#endif
}
template <class T, typename... Args> void warningln(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_WARNING, true, msg, args...);
#endif
}
/**
* Output a notice message. Output message contains
* I: followed by original message
* Notice messages are printed out at
* loglevels >= LOG_LEVEL_NOTICE
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void notice(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_NOTICE, false, msg, args...);
#endif
}
template <class T, typename... Args> void noticeln(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_NOTICE, true, msg, args...);
#endif
}
template <class T, typename... Args> void info(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_INFO, false, msg, args...);
#endif
}
template <class T, typename... Args> void infoln(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_INFO, true, msg, args...);
#endif
}
/**
* Output a trace message. Output message contains
* N: followed by original message
* Trace messages are printed out at
* loglevels >= LOG_LEVEL_TRACE
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void trace(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_TRACE, false, msg, args...);
#endif
}
template <class T, typename... Args> void traceln(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_TRACE, true, msg, args...);
#endif
}
/**
* Output a verbose message. Output message contains
* V: followed by original message
* Debug messages are printed out at
* loglevels >= LOG_LEVEL_VERBOSE
*
* \param msg format string to output
* \param ... any number of variables
* \return void
*/
template <class T, typename... Args> void verbose(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_VERBOSE, false, msg, args...);
#endif
}
template <class T, typename... Args> void verboseln(T msg, Args... args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_VERBOSE, true, msg, args...);
#endif
}
private:
void print(const char *format, va_list args);
void print(const __FlashStringHelper *format, va_list args);
void print(const Printable& obj, va_list args) {
#ifndef DISABLE_LOGGING
for (int i = 0; i < _handlerCount; i++) {
if (_logOutputs[i]) {
_logOutputs[i]->print(obj);
}
}
#endif
}
void printFormat(const char format, va_list *args);
template <class T> void printLevel(int level, bool newLine, T msg, ...) {
#ifndef DISABLE_LOGGING
if (level > _level) {
return;
}
if (level < LOG_LEVEL_SILENT) {
level = LOG_LEVEL_SILENT;
}
#ifdef ESP32
if (xSemaphoreTake(_semaphore, (TickType_t) 10 )) {
#endif
if (_showColors) {
switch (level) {
case LOG_LEVEL_FATAL:
writeLog(LOG_COLOR_BOLD_RED);
break;
case LOG_LEVEL_ERROR:
writeLog(LOG_COLOR_RED);
break;
case LOG_LEVEL_WARNING:
writeLog(LOG_COLOR_YELLOW);
break;
default:
break;
}
}
if (_prefix != NULL && _showLevel) {
for (int i = 0; i < _handlerCount; i++) {
if (_logOutputs[i]) {
_prefix(_logOutputs[i], level);
}
}
}
if (_showLevel) {
static const char levels[] = "FEWITV";
writeLog(levels[level - 1]);
writeLog(": ");
}
va_list args;
va_start(args, msg);
print(msg, args);
if (_suffix != NULL && _showLevel) {
for (int i = 0; i < _handlerCount; i++) {
if (_logOutputs[i]) {
_suffix(_logOutputs[i], level);
}
}
}
if (newLine) {
writeLog(lineEnd);
}
if (_showColors) {
writeLog(LOG_COLOR_END);
}
va_end(args);
#ifdef ESP32
xSemaphoreGive(_semaphore);
}
#endif
#endif
}
#ifndef DISABLE_LOGGING
int _level;
bool _showLevel;
bool _showColors;
Print* _logOutputs[LOG_MAX_HANDLERS];
int _handlerCount;
template<typename... Args> void writeLog(Args... args);
#ifdef ESP32
SemaphoreHandle_t _semaphore;
#endif
printfunction _prefix = NULL;
printfunction _suffix = NULL;
#endif
};
#ifndef __DO_NOT_INSTANTIATE__
extern Logging Log;
#endif
#endif