-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathArduino_DebugUtils.cpp
258 lines (205 loc) · 7.04 KB
/
Arduino_DebugUtils.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
/*
This file is part of Arduino_DebugUtils.
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "Arduino_DebugUtils.h"
/******************************************************************************
CONSTANTS
******************************************************************************/
static Arduino_DebugUtils::Level const DEFAULT_DEBUG_LEVEL = DBG_INFO;
static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
/******************************************************************************
CTOR/DTOR
******************************************************************************/
Arduino_DebugUtils::Arduino_DebugUtils() {
timestampOff();
newlineOn();
debugLabelOff();
formatTimestampOff();
setDebugLevel(DEFAULT_DEBUG_LEVEL);
setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void Arduino_DebugUtils::setDebugLevel(Arduino_DebugUtils::Level const debug_level) {
_debug_level = debug_level;
}
Arduino_DebugUtils::Level Arduino_DebugUtils::getDebugLevel() const {
return _debug_level;
}
void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) {
_debug_output_stream = stream;
}
void Arduino_DebugUtils::newlineOn() {
_newline_on = true;
}
void Arduino_DebugUtils::newlineOff() {
_newline_on = false;
}
void Arduino_DebugUtils::debugLabelOn() {
_print_debug_label = true;
}
void Arduino_DebugUtils::debugLabelOff() {
_print_debug_label = false;
}
void Arduino_DebugUtils::formatTimestampOn() {
_format_timestamp_on = true;
}
void Arduino_DebugUtils::formatTimestampOff() {
_format_timestamp_on = false;
}
void Arduino_DebugUtils::timestampOn() {
_timestamp_on = true;
}
void Arduino_DebugUtils::timestampOff() {
_timestamp_on = false;
}
void Arduino_DebugUtils::print(Arduino_DebugUtils::Level const debug_level, const char * fmt, ...)
{
if (!shouldPrint(debug_level))
return;
if (_print_debug_label)
printDebugLabel(debug_level);
if (_timestamp_on)
printTimestamp();
va_list args;
va_start(args, fmt);
vPrint(fmt, args);
va_end(args);
}
void Arduino_DebugUtils::print(Arduino_DebugUtils::Level const debug_level, const __FlashStringHelper * fmt, ...)
{
if (!shouldPrint(debug_level))
return;
if (_print_debug_label)
printDebugLabel(debug_level);
if (_timestamp_on)
printTimestamp();
String fmt_str(fmt);
va_list args;
va_start(args, fmt);
vPrint(fmt_str.c_str(), args);
va_end(args);
}
/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/
void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
va_list args_copy;
va_copy(args_copy, args);
// calculate required buffer length
int msg_buf_size = vsnprintf(nullptr, 0, fmt, args) + 1; // add one for null terminator
#if __STDC_NO_VLA__ == 1
// in the rare case where VLA is not allowed by compiler, fall back on heap-allocated memory
char * msg_buf = new char[msg_buf_size];
#else
char msg_buf[msg_buf_size];
#endif
vsnprintf(msg_buf, msg_buf_size, fmt, args_copy);
va_end(args_copy);
if (_newline_on) {
_debug_output_stream->println(msg_buf);
} else {
_debug_output_stream->print(msg_buf);
}
#if __STDC_NO_VLA__ == 1
// remember to clean up memory
delete[] msg_buf;
#endif
}
void Arduino_DebugUtils::printTimestamp()
{
char timestamp[32];
if (_format_timestamp_on)
{
auto const msCount = millis();
uint16_t const milliseconds = msCount % 1000; // ms remaining when converted to seconds
uint16_t const allSeconds = msCount / 1000; // total number of seconds to calculate remaining values
uint16_t const hours = allSeconds / 3600; // convert seconds to hours
uint16_t const secondsRemaining = allSeconds % 3600; // seconds left over
uint16_t const minutes = secondsRemaining / 60 ; // convert seconds left over to minutes
uint16_t const seconds = secondsRemaining % 60; // seconds left over
snprintf(timestamp, sizeof(timestamp), // "prints" formatted output to a char array (string)
"[ "
"%02d:" //HH:
"%02d:" //MM:
"%02d." //SS.
"%03d" //MMM
" ] ",
hours,
minutes,
seconds,
milliseconds
);
}
else
{
snprintf(timestamp, sizeof(timestamp), "[ %lu ] ", millis());
}
_debug_output_stream->print(timestamp);
}
void Arduino_DebugUtils::printDebugLabel(Arduino_DebugUtils::Level const debug_level)
{
static char const * DEBUG_MODE_STRING[5] =
{
"[DBG_ERROR ] ",
"[DBG_WARNING] ",
"[DBG_INFO ] ",
"[DBG_DEBUG ] ",
"[DBG_VERBOSE] ",
};
const char* level_str = nullptr;
switch(debug_level) {
case Arduino_DebugUtils::Level::Error:
level_str = DEBUG_MODE_STRING[0];
break;
case Arduino_DebugUtils::Level::Warning:
level_str = DEBUG_MODE_STRING[1];
break;
case Arduino_DebugUtils::Level::Info:
level_str = DEBUG_MODE_STRING[2];
break;
case Arduino_DebugUtils::Level::Debug:
level_str = DEBUG_MODE_STRING[3];
break;
case Arduino_DebugUtils::Level::Verbose:
level_str = DEBUG_MODE_STRING[4];
break;
case Arduino_DebugUtils::Level::None:
case Arduino_DebugUtils::Level::All:
default:
break;
}
if(level_str != nullptr) {
_debug_output_stream->print(level_str);
}
}
bool Arduino_DebugUtils::shouldPrint(Arduino_DebugUtils::Level const debug_level) const
{
uint_fast16_t dl = static_cast<uint_fast16_t>(debug_level);
uint_fast16_t _dl = static_cast<uint_fast16_t>(_debug_level);
return _dl & dl == dl;
}
/******************************************************************************
CLASS INSTANTIATION
******************************************************************************/
Arduino_DebugUtils Debug;
void setDebugMessageLevel(Arduino_DebugUtils::Level const debug_level) {
Debug.setDebugLevel(debug_level);
}
Arduino_DebugUtils::Level getDebugMessageLevel() {
return Debug.getDebugLevel();
}