-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathArduino_DebugUtils.cpp
189 lines (147 loc) · 4.97 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
/*
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 int const DEFAULT_DEBUG_LEVEL = DBG_INFO;
static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
/******************************************************************************
CTOR/DTOR
******************************************************************************/
Arduino_DebugUtils::Arduino_DebugUtils() {
timestampOff();
newlineOn();
debugLabelOff();
setDebugLevel(DEFAULT_DEBUG_LEVEL);
setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void Arduino_DebugUtils::setDebugLevel(int const debug_level) {
_debug_level = debug_level;
}
int 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::timestampOn() {
_timestamp_on = true;
}
void Arduino_DebugUtils::timestampOff() {
_timestamp_on = false;
}
void Arduino_DebugUtils::print(int 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(int 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) {
// 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);
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[20];
snprintf(timestamp, 20, "[ %lu ] ", millis());
_debug_output_stream->print(timestamp);
}
void Arduino_DebugUtils::printDebugLabel(int const debug_level)
{
static char const * DEBUG_MODE_STRING[5] =
{
"[DBG_ERROR ] ",
"[DBG_WARNING] ",
"[DBG_INFO ] ",
"[DBG_DEBUG ] ",
"[DBG_VERBOSE] ",
};
bool is_valid_debug_level = (debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE);
if (!is_valid_debug_level)
return;
_debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
}
bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
{
return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level));
}
/******************************************************************************
CLASS INSTANTIATION
******************************************************************************/
Arduino_DebugUtils Debug;
void setDebugMessageLevel(int const debug_level) {
Debug.setDebugLevel(debug_level);
}
int getDebugMessageLevel() {
return Debug.getDebugLevel();
}