Skip to content

Commit 94b3d50

Browse files
committed
Provide API for printing the debug level as a string.
This fixes #7.
1 parent 7b07264 commit 94b3d50

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: src/Arduino_DebugUtils.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
3535
Arduino_DebugUtils::Arduino_DebugUtils() {
3636
timestampOff();
3737
newlineOn();
38+
debugLabelOff();
3839
setDebugLevel(DEFAULT_DEBUG_LEVEL);
3940
setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
4041
}
@@ -63,6 +64,14 @@ void Arduino_DebugUtils::newlineOff() {
6364
_newline_on = false;
6465
}
6566

67+
void Arduino_DebugUtils::debugLabelOn() {
68+
_print_debug_label = true;
69+
}
70+
71+
void Arduino_DebugUtils::debugLabelOff() {
72+
_print_debug_label = false;
73+
}
74+
6675
void Arduino_DebugUtils::timestampOn() {
6776
_timestamp_on = true;
6877
}
@@ -76,6 +85,9 @@ void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...)
7685
if (!shouldPrint(debug_level))
7786
return;
7887

88+
if (_print_debug_label)
89+
printDebugLabel(debug_level);
90+
7991
if (_timestamp_on)
8092
printTimestamp();
8193

@@ -90,6 +102,9 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper
90102
if (!shouldPrint(debug_level))
91103
return;
92104

105+
if (_print_debug_label)
106+
printDebugLabel(debug_level);
107+
93108
if (_timestamp_on)
94109
printTimestamp();
95110

@@ -136,6 +151,24 @@ void Arduino_DebugUtils::printTimestamp()
136151
_debug_output_stream->print(timestamp);
137152
}
138153

154+
void Arduino_DebugUtils::printDebugLabel(int const debug_level)
155+
{
156+
static char const * DEBUG_MODE_STRING[5] =
157+
{
158+
"[DBG_ERROR ] ",
159+
"[DBG_WARNING] ",
160+
"[DBG_INFO ] ",
161+
"[DBG_DEBUG ] ",
162+
"[DBG_VERBOSE] ",
163+
};
164+
165+
bool is_valid_debug_level = (debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE);
166+
if (!is_valid_debug_level)
167+
return;
168+
169+
_debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
170+
}
171+
139172
bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
140173
{
141174
return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level));

Diff for: src/Arduino_DebugUtils.h

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class Arduino_DebugUtils {
6161
void newlineOn();
6262
void newlineOff();
6363

64+
void debugLabelOn();
65+
void debugLabelOff();
66+
6467
void print(int const debug_level, const char * fmt, ...);
6568
void print(int const debug_level, const __FlashStringHelper * fmt, ...);
6669

@@ -69,11 +72,13 @@ class Arduino_DebugUtils {
6972

7073
bool _timestamp_on;
7174
bool _newline_on;
75+
bool _print_debug_label;
7276
int _debug_level;
7377
Stream * _debug_output_stream;
7478

7579
void vPrint(char const * fmt, va_list args);
7680
void printTimestamp();
81+
void printDebugLabel(int const debug_level);
7782
bool shouldPrint(int const debug_level) const;
7883

7984
};

0 commit comments

Comments
 (0)