Skip to content

Commit f04b427

Browse files
Introducing enum to keep track of debug levels
1 parent 585549e commit f04b427

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

Diff for: src/Arduino_DebugUtils.cpp

+41-15
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
CONSTANTS
2626
******************************************************************************/
2727

28-
static int const DEFAULT_DEBUG_LEVEL = DBG_INFO;
29-
static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
28+
static Arduino_DebugUtils::Level const DEFAULT_DEBUG_LEVEL = DBG_INFO;
29+
static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
3030

3131
/******************************************************************************
3232
CTOR/DTOR
@@ -45,11 +45,11 @@ Arduino_DebugUtils::Arduino_DebugUtils() {
4545
PUBLIC MEMBER FUNCTIONS
4646
******************************************************************************/
4747

48-
void Arduino_DebugUtils::setDebugLevel(int const debug_level) {
48+
void Arduino_DebugUtils::setDebugLevel(Arduino_DebugUtils::Level const debug_level) {
4949
_debug_level = debug_level;
5050
}
5151

52-
int Arduino_DebugUtils::getDebugLevel() const {
52+
Arduino_DebugUtils::Level Arduino_DebugUtils::getDebugLevel() const {
5353
return _debug_level;
5454
}
5555

@@ -89,7 +89,7 @@ void Arduino_DebugUtils::timestampOff() {
8989
_timestamp_on = false;
9090
}
9191

92-
void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...)
92+
void Arduino_DebugUtils::print(Arduino_DebugUtils::Level const debug_level, const char * fmt, ...)
9393
{
9494
if (!shouldPrint(debug_level))
9595
return;
@@ -106,7 +106,7 @@ void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...)
106106
va_end(args);
107107
}
108108

109-
void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper * fmt, ...)
109+
void Arduino_DebugUtils::print(Arduino_DebugUtils::Level const debug_level, const __FlashStringHelper * fmt, ...)
110110
{
111111
if (!shouldPrint(debug_level))
112112
return;
@@ -196,7 +196,7 @@ void Arduino_DebugUtils::printTimestamp()
196196
_debug_output_stream->print(timestamp);
197197
}
198198

199-
void Arduino_DebugUtils::printDebugLabel(int const debug_level)
199+
void Arduino_DebugUtils::printDebugLabel(Arduino_DebugUtils::Level const debug_level)
200200
{
201201
static char const * DEBUG_MODE_STRING[5] =
202202
{
@@ -207,16 +207,42 @@ void Arduino_DebugUtils::printDebugLabel(int const debug_level)
207207
"[DBG_VERBOSE] ",
208208
};
209209

210-
bool is_valid_debug_level = (debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE);
211-
if (!is_valid_debug_level)
212-
return;
210+
const char* level_str = nullptr;
211+
switch(debug_level) {
212+
case Arduino_DebugUtils::Level::Error:
213+
level_str = DEBUG_MODE_STRING[0];
214+
break;
215+
case Arduino_DebugUtils::Level::Warning:
216+
level_str = DEBUG_MODE_STRING[1];
217+
break;
218+
case Arduino_DebugUtils::Level::Info:
219+
level_str = DEBUG_MODE_STRING[2];
220+
break;
221+
case Arduino_DebugUtils::Level::Debug:
222+
level_str = DEBUG_MODE_STRING[3];
223+
break;
224+
case Arduino_DebugUtils::Level::Verbose:
225+
level_str = DEBUG_MODE_STRING[4];
226+
break;
227+
case Arduino_DebugUtils::Level::None:
228+
case Arduino_DebugUtils::Level::Profiling:
229+
case Arduino_DebugUtils::Level::ContinuousIntegration:
230+
case Arduino_DebugUtils::Level::All:
231+
default:
232+
break;
233+
}
213234

214-
_debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
235+
if(level_str != nullptr) {
236+
_debug_output_stream->print(level_str);
237+
}
215238
}
216239

217-
bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
240+
bool Arduino_DebugUtils::shouldPrint(Arduino_DebugUtils::Level const debug_level) const
218241
{
219-
return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level));
242+
uint_fast16_t dl = static_cast<uint_fast16_t>(debug_level);
243+
uint_fast16_t _dl = static_cast<uint_fast16_t>(_debug_level);
244+
245+
return _dl & dl == dl;
220246
}
221247

222248
/******************************************************************************
@@ -226,9 +252,9 @@ bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
226252
Arduino_DebugUtils Debug;
227253

228254
void setDebugMessageLevel(int const debug_level) {
229-
Debug.setDebugLevel(debug_level);
255+
Debug.setDebugLevel(static_cast<Arduino_DebugUtils::Level>(debug_level));
230256
}
231257

232258
int getDebugMessageLevel() {
233-
return Debug.getDebugLevel();
259+
return static_cast<int>(Debug.getDebugLevel());
234260
}

Diff for: src/Arduino_DebugUtils.h

+23-14
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ int getDebugMessageLevel();
4444
class Arduino_DebugUtils {
4545

4646
public:
47+
enum class Level: uint_fast16_t {
48+
None = DEBUG_LEVEL_NONE,
49+
Error = DEBUG_LEVEL_ERROR,
50+
Warning = DEBUG_LEVEL_WARNING,
51+
Info = DEBUG_LEVEL_INFO,
52+
Verbose = DEBUG_LEVEL_VERBOSE,
53+
Debug = DEBUG_LEVEL_DEBUG,
54+
All = DEBUG_LEVEL_ALL,
55+
};
4756

4857
Arduino_DebugUtils();
4958

50-
void setDebugLevel(int const debug_level);
51-
int getDebugLevel() const;
59+
void setDebugLevel(Level const debug_level);
60+
Arduino_DebugUtils::Level getDebugLevel() const;
5261

5362
void setDebugOutputStream(Stream * stream);
5463

@@ -64,8 +73,8 @@ class Arduino_DebugUtils {
6473
void formatTimestampOn();
6574
void formatTimestampOff();
6675

67-
void print(int const debug_level, const char * fmt, ...);
68-
void print(int const debug_level, const __FlashStringHelper * fmt, ...);
76+
void print(Level const debug_level, const char * fmt, ...);
77+
void print(Level const debug_level, const __FlashStringHelper * fmt, ...);
6978

7079

7180
private:
@@ -74,27 +83,27 @@ class Arduino_DebugUtils {
7483
bool _newline_on;
7584
bool _print_debug_label;
7685
bool _format_timestamp_on;
77-
int _debug_level;
86+
Level _debug_level;
7887
Stream * _debug_output_stream;
7988

8089
void vPrint(char const * fmt, va_list args);
8190
void printTimestamp();
82-
void printDebugLabel(int const debug_level);
83-
bool shouldPrint(int const debug_level) const;
91+
void printDebugLabel(Arduino_DebugUtils::Level const debug_level);
92+
inline bool shouldPrint(Level const debug_level) const;
8493

8594
};
8695

8796
/******************************************************************************
8897
CONSTANTS
8998
******************************************************************************/
9099

91-
static constexpr int DBG_NONE = DEBUG_LEVEL_NONE;
92-
static constexpr int DBG_ERROR = DEBUG_LEVEL_ERROR;
93-
static constexpr int DBG_WARNING = DEBUG_LEVEL_WARNING;
94-
static constexpr int DBG_INFO = DEBUG_LEVEL_INFO;
95-
static constexpr int DBG_DEBUG = DEBUG_LEVEL_DEBUG;
96-
static constexpr int DBG_VERBOSE = DEBUG_LEVEL_VERBOSE;
97-
static constexpr int DBG_ALL = DEBUG_LEVEL_ALL;
100+
static constexpr Arduino_DebugUtils::Level DBG_NONE = Arduino_DebugUtils::Level::None;
101+
static constexpr Arduino_DebugUtils::Level DBG_ERROR = Arduino_DebugUtils::Level::Error;
102+
static constexpr Arduino_DebugUtils::Level DBG_WARNING = Arduino_DebugUtils::Level::Warning;
103+
static constexpr Arduino_DebugUtils::Level DBG_INFO = Arduino_DebugUtils::Level::Info;
104+
static constexpr Arduino_DebugUtils::Level DBG_DEBUG = Arduino_DebugUtils::Level::Debug;
105+
static constexpr Arduino_DebugUtils::Level DBG_VERBOSE = Arduino_DebugUtils::Level::Verbose;
106+
static constexpr Arduino_DebugUtils::Level DBG_ALL = Arduino_DebugUtils::Level::All;
98107

99108
/******************************************************************************
100109
EXTERN

0 commit comments

Comments
 (0)