Skip to content

Commit eee2fce

Browse files
committed
Extracting check wether or not a debug message should be printed into shared function to avoid code duplication
1 parent 0b9bc1a commit eee2fce

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

Diff for: src/Arduino_DebugUtils.cpp

+27-26
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,32 @@ void Arduino_DebugUtils::timestampOff() {
6060

6161
void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...)
6262
{
63-
if (debug_level >= DBG_ERROR &&
64-
debug_level <= DBG_VERBOSE &&
65-
debug_level <= _debug_level)
66-
{
67-
if (_timestamp_on)
68-
printTimestamp();
69-
70-
va_list args;
71-
va_start(args, fmt);
72-
vPrint(fmt, args);
73-
va_end(args);
74-
}
63+
if (!shouldPrint(debug_level))
64+
return;
65+
66+
if (_timestamp_on)
67+
printTimestamp();
68+
69+
va_list args;
70+
va_start(args, fmt);
71+
vPrint(fmt, args);
72+
va_end(args);
7573
}
7674

7775
void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper * fmt, ...)
7876
{
79-
if (debug_level >= DBG_ERROR &&
80-
debug_level <= DBG_VERBOSE &&
81-
debug_level <= _debug_level)
82-
{
83-
if (_timestamp_on)
84-
printTimestamp();
85-
86-
String fmt_str(fmt);
87-
88-
va_list args;
89-
va_start(args, fmt_str.c_str());
90-
vPrint(fmt_str.c_str(), args);
91-
va_end(args);
92-
}
77+
if (!shouldPrint(debug_level))
78+
return;
79+
80+
if (_timestamp_on)
81+
printTimestamp();
82+
83+
String fmt_str(fmt);
84+
85+
va_list args;
86+
va_start(args, fmt_str.c_str());
87+
vPrint(fmt_str.c_str(), args);
88+
va_end(args);
9389
}
9490

9591
/******************************************************************************
@@ -112,6 +108,11 @@ void Arduino_DebugUtils::printTimestamp()
112108
_debug_output_stream->print(timestamp);
113109
}
114110

111+
bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
112+
{
113+
return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level));
114+
}
115+
115116
/******************************************************************************
116117
CLASS INSTANTIATION
117118
******************************************************************************/

Diff for: src/Arduino_DebugUtils.h

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Arduino_DebugUtils {
6868

6969
void vPrint(char const * fmt, va_list args);
7070
void printTimestamp();
71+
bool shouldPrint(int const debug_level) const;
7172

7273
};
7374

0 commit comments

Comments
 (0)