-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathArduinoLogger.h
602 lines (528 loc) · 15 KB
/
ArduinoLogger.h
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#ifndef ARDUINO_LOGGER_H_
#define ARDUINO_LOGGER_H_
#ifdef __XTENSA__
#include <ets_sys.h>
#include <internal/lambda.h>
#else
#include <LibPrintf.h>
#endif
#if !defined(__AVR__)
#include <utility>
#endif
#ifdef __XTENSA__
void _putchar(char c);
#endif
/// Logging is disabled
#define LOG_LEVEL_OFF 0
/// Indicates the system is unusable, or an error that is unrecoverable
#define LOG_LEVEL_CRITICAL 1
/// Indicates an error condition
#define LOG_LEVEL_ERROR 2
/// Indicates a warning condition
#define LOG_LEVEL_WARNING 3
/// Informational messages
#define LOG_LEVEL_INFO 4
/// Debug-level messages
#define LOG_LEVEL_DEBUG 5
/// The maximum log level that can be set
#define LOG_LEVEL_MAX LOG_LEVEL_DEBUG
/// The number of possible log levels
#define LOG_LEVEL_COUNT (LOG_LEVEL_MAX + 1)
#define LOG_LEVEL_CRITICAL_PREFIX "<!> "
#define LOG_LEVEL_ERROR_PREFIX "<E> "
#define LOG_LEVEL_WARNING_PREFIX "<W> "
#define LOG_LEVEL_INFO_PREFIX "<I> "
#define LOG_LEVEL_DEBUG_PREFIX "<D> "
#define LOG_LEVEL_INTERRUPT_PREFIX "<int> "
// Supply a default log level
#ifndef LOG_LEVEL
/** Default maximum log level.
*
* This is the maximum log level that will be compiled in.
* To set a custom log level, define the LOG_LEVEL before including this header
* (e.g., as a compiler definition)
*/
#define LOG_LEVEL LOG_LEVEL_DEBUG
#endif
#ifndef LOG_EN_DEFAULT
/// Whether the logging module is enabled automatically on boot.
#define LOG_EN_DEFAULT 1
#endif
#ifndef LOG_ECHO_EN_DEFAULT
/// Indicates that log statements should be echoed to the console
/// If true, log statements will be echoed.
/// If false, log statements will only go to the log.
#define LOG_ECHO_EN_DEFAULT 0
#endif
#ifndef LOG_LEVEL_NAMES
/// Users can override these default names with a compiler definition
#define LOG_LEVEL_NAMES \
{ \
"off", "critical", "error", "warning", "info", "debug", \
}
#endif
#ifndef LOG_LEVEL_SHORT_NAMES
/// Users can override these default short names with a compiler definition
#define LOG_LEVEL_SHORT_NAMES \
{ \
"O", LOG_LEVEL_CRITICAL_PREFIX, LOG_LEVEL_ERROR_PREFIX, LOG_LEVEL_WARNING_PREFIX, \
LOG_LEVEL_INFO_PREFIX, LOG_LEVEL_DEBUG_PREFIX, \
}
#endif
#pragma mark - Short File Name Macro -
using cstr = const char* const;
constexpr cstr past_last_slash(cstr str, cstr last_slash)
{
return *str == '\0' ? last_slash
: *str == '/' ? past_last_slash(str + 1, str + 1)
: past_last_slash(str + 1, last_slash);
}
constexpr cstr past_last_slash(cstr str)
{
return past_last_slash(str, str);
}
#define __SHORT_FILE__ \
({ \
constexpr cstr sf__{past_last_slash(__FILE__)}; \
sf__; \
})
#pragma mark - Tracing Macros -
#define STRINGPASTE(x) #x
#define TOSTRING(x) STRINGPASTE(x)
#define TRACE() \
({ \
constexpr cstr sf__{past_last_slash(__FILE__ ":" TOSTRING(__LINE__))}; \
sf__; \
})
#define FUNC() __FUNCTION__
#define PRETTY_FUNC() __PRETTY_FUNCTION__
#pragma mark - Logging Class -
enum log_level_e
{
off = LOG_LEVEL_OFF,
critical = LOG_LEVEL_CRITICAL,
error = LOG_LEVEL_ERROR,
warning = LOG_LEVEL_WARNING,
info = LOG_LEVEL_INFO,
debug = LOG_LEVEL_DEBUG,
};
class logNames
{
public:
constexpr static const char* level_short_names[LOG_LEVEL_COUNT] = LOG_LEVEL_SHORT_NAMES;
constexpr static const char* level_string_names[LOG_LEVEL_COUNT] = LOG_LEVEL_NAMES;
};
constexpr log_level_e LOG_LEVEL_LIMIT() noexcept
{
return static_cast<log_level_e>(LOG_LEVEL);
}
constexpr const char* LOG_LEVEL_TO_C_STRING(log_level_e level)
{
return logNames::level_string_names[level];
}
constexpr const char* LOG_LEVEL_TO_SHORT_C_STRING(log_level_e level)
{
return logNames::level_short_names[level];
}
class LoggerBase
{
public:
/** Get the current log buffer size
*
* Derived classes must implement this function.
*
* @returns The current size of the log buffer, in bytes.
* The base class returns SIZE_MAX to indicate a potentially invalid condition.
*/
virtual size_t size() const noexcept
{
return SIZE_MAX;
}
/** Get the log buffer capacity
*
* Derived classes must implement this function.
*
* @returns The total capacity of the log buffer, in bytes.
* The base class returns SIZE_MAX to indicate a potentially invalid condition.
*/
virtual size_t capacity() const noexcept
{
return SIZE_MAX;
}
/** Check if the log is enabled.
*
* @returns true if log output is enabled, false if it is disabled.
*/
bool enabled() const noexcept
{
return enabled_;
}
/** Check the echo setting
*
* @returns true if echo to console is enabled, false if disabled.
*/
bool echo() const noexcept
{
return echo_;
}
/** Enable/disable echo to console.
*
* @param en Echo switch. If true, log statements will also be echo'd to the console through
* printf(). If false, log statements will only go to the log buffer.
* @returns true if echo to console is enabled, false if disabled.
*/
bool echo(bool en) noexcept
{
echo_ = en;
return echo_;
}
/** Get the maximum log level (filtering)
*
* @returns the current log level maximum.
*/
log_level_e level() const noexcept
{
return level_;
}
/** Set the maximum log level (filtering)
*
* @param l The maximum log level. Levels greater than `l` will not be added to the log buffer.
* @returns the current log level maximum.
*/
log_level_e level(log_level_e l) noexcept
{
if(l <= LOG_LEVEL_LIMIT())
{
level_ = l;
}
return level_;
}
template<typename... Args>
void critical(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
log(log_level_e::critical, fmt, args...);
#else
log(log_level_e::critical, fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
void error(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
log(log_level_e::error, fmt, args...);
#else
log(log_level_e::error, fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
void warning(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
log(log_level_e::warning, fmt, args...);
#else
log(log_level_e::warning, fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
void info(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
log(log_level_e::info, fmt, args...);
#else
log(log_level_e::info, fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
void debug(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
log(log_level_e::debug, fmt, args...);
#else
log(log_level_e::debug, fmt, std::forward<const Args>(args)...);
#endif
}
#ifdef __XTENSA__
/// Prints directly to the log with no extra characters added to the message.
void print(const char* format, ...) noexcept
{
// We need to capture this with a lambda to call log_putc,
// Then decay that into a function pointer for a callback
auto f = [this](char c) { log_putc(c); };
void (*callback)(char) = Lambda::ptr(f);
va_list va;
va_start(va, format);
ets_vprintf(callback, format, va);
va_end(va);
if(echo_)
{
ets_vprintf(ets_putc, format, va);
}
}
#else
/// Prints directly to the log with no extra characters added to the message.
template<typename... Args>
void print(const Args&... args) noexcept
{
fctprintf(&LoggerBase::log_putc_bounce, this, args...);
if(echo_)
{
// cppcheck-suppress wrongPrintfScanfArgNum
printf(args...);
}
}
#endif
/** Add data to the log buffer
*
* @tparam Args Variadic template args. Will be deduced by the compiler. Enables support for
* a variadic function template.
* @param l The log level associated with this statement.
* @param fmt The log format string.
* @param args The variadic arguments that are associated with the format string.
*/
template<typename... Args>
void log(log_level_e l, const char* fmt, const Args&... args) noexcept
{
if(enabled_ && l <= level_)
{
// Add our prefix
print("%s", LOG_LEVEL_TO_SHORT_C_STRING(l));
log_customprefix();
// Send the primary log statementets_vprintf
print(fmt, args...);
}
}
/** Print the buffered log contents to the target output stream
*
* When called, the contents of the log buffer will be removed and placed into
* the target output stream.
*
* Outputs can be any target. You will notice many example implementations print
* the log buffer contents to Serial when flush() is called.
*
* Derived classes must implement this function.
*/
virtual void flush() noexcept {}
/** Clear the contents of the log buffer.
*
* Reset the log buffer to an empty state.
*
* @post The log buffer will be empty.
*/
virtual void clear() noexcept {}
protected:
/// Default constructor
LoggerBase() = default;
/** Initialize the logger with options
*
* @param enable If true, log statements will be output to the log buffer. If false,
* logging will be disabled and log statements will not be output to the log buffer.
* @param l Runtime log filtering level. Levels greater than the target will not be output
* to the log buffer.
* @param echo If true, log statements will be logged and printed to the console with printf().
* If false, log statements will only be added to the log buffer.
*/
explicit LoggerBase(bool enable, log_level_e l = LOG_LEVEL_LIMIT(),
bool echo = LOG_ECHO_EN_DEFAULT) noexcept
: enabled_(enable), level_(l), echo_(echo)
{
}
/// Default destructor
virtual ~LoggerBase() = default;
/** Add a custom prefix to the log file
*
* Define this function in your derived class to supply a custom prefix
* to the log statement. This will appear *after* the log level indicator
* (e.g., <!>), but before the message.
*
* Recommended use of this field might include generating a timestamp:
* <!> [0081838ms] Message goes here.
*
* Derived classes must implement this function.
*/
virtual void log_customprefix() {}
/** Log buffer putc function
*
* This function adds a character to the underlying log buffer.
*
* This function is used with the fctprintf() interface to output to the log buffer.
* This enables the framework to reuse the same print formatting for both logging and printf().
*
* Derived classes must implement this function.
*
* @param c The character to insert into the log buffer.
*/
virtual void log_putc(char c) = 0;
/** putc bounce function
*
* This is a bounce function which registers with the C printf API. We use the private parameter
* to store the `this` pointer so we can get back to our logger's putc instance.
*
* @param c The character to log.
* @param this_ptr The this pointer of the logger instance. Used to invoke log_putc() on the
* correct instance.
*/
static void log_putc_bounce(char c, void* this_ptr)
{
reinterpret_cast<LoggerBase*>(this_ptr)->log_putc(c);
}
private:
/// Indicates whether logging is currently enabled
bool enabled_ = LOG_EN_DEFAULT;
/// The current log level.
/// Levels greater than the current setting will be filtered out.
log_level_e level_ = LOG_LEVEL_LIMIT();
/// Console echoing.
/// If true, log statements will be printed to the console through printf().
bool echo_ = LOG_ECHO_EN_DEFAULT;
};
/** Declare a static platform logger instance.
*
* This class is used to declare a static platform logger instance.
*
* Define a platform logger which is templated on the log type:
*
* @code
* using PlatformLogger = PlatformLogger_t<CircularLogBufferLogger<8 * 1024>>;
* @endcode
*
* This `PlatformLogger` type will then work with the logging macros (loginfo(), etc.).
*
* The instance can also be grabbed manually:
*
* @code
* PlatformLogger::inst().dump();
* @endcode
*/
template<class TLogger>
class PlatformLogger_t
{
public:
PlatformLogger_t() = default;
~PlatformLogger_t() = default;
static TLogger& inst()
{
static TLogger logger_;
return logger_;
}
template<typename... Args>
inline static void critical(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().critical(fmt, args...);
#else
inst().critical(fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
inline static void error(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().error(fmt, args...);
#else
inst().error(fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
inline static void warning(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().warning(fmt, args...);
#else
inst().warning(fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
inline static void info(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().info(fmt, args...);
#else
inst().info(fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
inline static void debug(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().debug(fmt, args...);
#else
inst().debug(fmt, std::forward<const Args>(args)...);
#endif
}
template<typename... Args>
inline static void print(const char* fmt, const Args&... args)
{
#if defined(__AVR__)
inst().print(fmt, args...);
#else
inst().print(fmt, std::forward<const Args>(args)...);
#endif
}
inline static void flush()
{
inst().flush();
}
inline static void clear()
{
inst().clear();
}
inline static log_level_e level(log_level_e l)
{
return inst().level(l);
}
inline static bool echo(bool en)
{
return inst().echo(en);
}
};
/** @name Logging Macros
*
* The log macros can be overridden by defining them in your platform_logger.h file
*
* Warning is the default log level if one is not supplied
*
* For more information see @ref docs/development/ExtendingTheFramework/customizing_log_macros.md
*
* @{
*/
#if LOG_LEVEL >= LOG_LEVEL_CRITICAL
#ifndef logcritical
#define logcritical(...) PlatformLogger::critical(__VA_ARGS__)
#endif
#else
#define logcritical(...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_ERROR
#ifndef logerror
#define logerror(...) PlatformLogger::error(__VA_ARGS__)
#endif
#else
#define logerror(...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_WARNING
#ifndef logwarning
#define logwarning(...) PlatformLogger::warning(__VA_ARGS__)
#endif
#else
#define logwarning(...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_INFO
#ifndef loginfo
#define loginfo(...) PlatformLogger::info(__VA_ARGS__)
#endif
#else
#define loginfo(...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#ifndef logdebug
#define logdebug(...) PlatformLogger::debug(__VA_ARGS__)
#endif
#else
#define logdebug(...)
#endif
#define logflush() PlatformLogger::flush();
#define loglevel(lvl) PlatformLogger::level(lvl);
#define logecho(echo) PlatformLogger::echo(lvl);
#define logclear() PlatformLogger::clear();
#endif // ARDUINO_LOGGER_H_