-
Notifications
You must be signed in to change notification settings - Fork 11
/
AVRCircularBufferLogger.h
115 lines (98 loc) · 2.74 KB
/
AVRCircularBufferLogger.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
#ifndef AVR_CIRCULAR_BUFFER_LOGGER_H_
#define AVR_CIRCULAR_BUFFER_LOGGER_H_
// By default, this logging strategy does not auto-flush
// You can still override this default setting if desired.
#ifndef LOG_AUTOFLUSH_DEFAULT
#define LOG_AUTOFLUSH_DEFAULT false
#endif
#include "ArduinoLogger.h"
#include "internal/circular_buffer.hpp"
#include <avr/wdt.h>
/** Circular log buffer
*
* A circular log buffer manages a memoroy buffer as if it were neverending. When the capacity
* of the buffer is reached, insertions will wrap around to the beginning of the buffer and
* overwrite old data. This enables seemingly "infinite" memory with a fixed capacity, preferring
* the newest data be kept.
*
* @tparam TBufferSize Defines the size of the circular log buffer.
* Set to 0 to disable logging completely (for memory constrained systems).
* @note Size requirement: power-of-2 for optimized queue logic.
*
* @code
* using PlatformLogger =
* PlatformLogger_t<AVRCircularLogBufferLogger<8 * 1024>>;
* @endcode
*
* @ingroup LoggingSubsystem
*/
template<size_t TBufferSize = (1 * 1024)>
class AVRCircularLogBufferLogger final : public LoggerBase
{
public:
/// Default constructor
AVRCircularLogBufferLogger() : LoggerBase() {}
/** Initialize the circular log buffer 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 AVRCircularLogBufferLogger(bool enable, log_level_e l = LOG_LEVEL_LIMIT(),
bool echo = LOG_ECHO_EN_DEFAULT) noexcept
: LoggerBase(enable, l, echo)
{
}
/// Default destructor
~AVRCircularLogBufferLogger() noexcept = default;
void resetCause()
{
auto reg = MCUSR;
if(reg & (1 << WDRF))
{
info("Watchdog reset\n");
}
if(reg & (1 << BORF))
{
info("Brown-out reset\n");
}
if(reg & (1 << EXTRF))
{
info("External reset\n");
}
if(reg & (1 << PORF))
{
info("Power-on reset\n");
}
}
size_t size() const noexcept final
{
return log_buffer_.size();
}
size_t capacity() const noexcept final
{
return log_buffer_.capacity();
}
protected:
void log_putc(char c) noexcept final
{
log_buffer_.put(c);
}
void flush_() noexcept final
{
while(!log_buffer_.empty())
{
_putchar(log_buffer_.get());
}
}
void clear_() noexcept final
{
log_buffer_.reset();
}
private:
CircularBuffer<char, TBufferSize> log_buffer_;
};
#endif // AVR_CIRCULAR_BUFFER_LOGGER_H_