|
22 | 22 | #include "Arduino_DebugUtils.h"
|
23 | 23 |
|
24 | 24 | /******************************************************************************
|
25 |
| - NAMESPACE |
| 25 | + CONSTANTS |
26 | 26 | ******************************************************************************/
|
27 | 27 |
|
28 |
| -namespace impl { |
| 28 | +static int const DEFAULT_DEBUG_LEVEL = DEBUG_LVL_INFO; |
| 29 | +static Stream * DEFAULT_OUTPUT_STREAM = &Serial; |
29 | 30 |
|
30 |
| - /****************************************************************************** |
31 |
| - CONSTANTS |
32 |
| - ******************************************************************************/ |
33 |
| - |
34 |
| - static int const DEFAULT_DEBUG_LEVEL = DEBUG_LVL_INFO; |
35 |
| - static Stream * DEFAULT_OUTPUT_STREAM = &Serial; |
36 |
| - |
37 |
| - /****************************************************************************** |
38 |
| - CTOR/DTOR |
39 |
| - ******************************************************************************/ |
40 |
| - |
41 |
| - Arduino_DebugUtils::Arduino_DebugUtils() { |
42 |
| - timestampOff(); |
43 |
| - setDebugLevel(DEFAULT_DEBUG_LEVEL); |
44 |
| - setDebugOutputStream(DEFAULT_OUTPUT_STREAM); |
45 |
| - } |
46 |
| - |
47 |
| - /****************************************************************************** |
48 |
| - PUBLIC MEMBER FUNCTIONS |
49 |
| - ******************************************************************************/ |
50 |
| - |
51 |
| - void Arduino_DebugUtils::setDebugLevel(int const debug_level) { |
52 |
| - _debug_level = debug_level; |
53 |
| - } |
| 31 | +/****************************************************************************** |
| 32 | + CTOR/DTOR |
| 33 | + ******************************************************************************/ |
54 | 34 |
|
55 |
| - void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) { |
56 |
| - _debug_output_stream = stream; |
57 |
| - } |
| 35 | +Arduino_DebugUtils::Arduino_DebugUtils() { |
| 36 | + timestampOff(); |
| 37 | + setDebugLevel(DEFAULT_DEBUG_LEVEL); |
| 38 | + setDebugOutputStream(DEFAULT_OUTPUT_STREAM); |
| 39 | +} |
58 | 40 |
|
59 |
| - void Arduino_DebugUtils::timestampOn() { |
60 |
| - _timestamp_on = true; |
61 |
| - } |
62 |
| - |
63 |
| - void Arduino_DebugUtils::timestampOff() { |
64 |
| - _timestamp_on = false; |
65 |
| - } |
| 41 | +/****************************************************************************** |
| 42 | + PUBLIC MEMBER FUNCTIONS |
| 43 | + ******************************************************************************/ |
66 | 44 |
|
67 |
| - void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) { |
68 |
| - if (debug_level >= DEBUG_LVL_ERROR && |
69 |
| - debug_level <= DEBUG_LVL_VERBOSE && |
70 |
| - debug_level <= _debug_level) { |
71 |
| - if (_timestamp_on) { |
72 |
| - char timestamp[20]; |
73 |
| - snprintf(timestamp, 20, "[ %lu ] ", millis()); |
74 |
| - _debug_output_stream->print(timestamp); |
75 |
| - } |
76 |
| - |
77 |
| - va_list args; |
78 |
| - va_start(args, fmt); |
79 |
| - vPrint(fmt, args); |
80 |
| - va_end(args); |
| 45 | +void Arduino_DebugUtils::setDebugLevel(int const debug_level) { |
| 46 | + _debug_level = debug_level; |
| 47 | +} |
| 48 | + |
| 49 | +void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) { |
| 50 | + _debug_output_stream = stream; |
| 51 | +} |
| 52 | + |
| 53 | +void Arduino_DebugUtils::timestampOn() { |
| 54 | + _timestamp_on = true; |
| 55 | +} |
| 56 | + |
| 57 | +void Arduino_DebugUtils::timestampOff() { |
| 58 | + _timestamp_on = false; |
| 59 | +} |
| 60 | + |
| 61 | +void Arduino_DebugUtils::print(int const debug_level, const char * fmt, ...) { |
| 62 | + if (debug_level >= DEBUG_LVL_ERROR && |
| 63 | + debug_level <= DEBUG_LVL_VERBOSE && |
| 64 | + debug_level <= _debug_level) { |
| 65 | + if (_timestamp_on) { |
| 66 | + char timestamp[20]; |
| 67 | + snprintf(timestamp, 20, "[ %lu ] ", millis()); |
| 68 | + _debug_output_stream->print(timestamp); |
81 | 69 | }
|
82 |
| - } |
83 | 70 |
|
84 |
| - /****************************************************************************** |
85 |
| - PRIVATE MEMBER FUNCTIONS |
86 |
| - ******************************************************************************/ |
87 |
| - |
88 |
| - void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) { |
89 |
| - static size_t const MSG_BUF_SIZE = 120; |
90 |
| - char msg_buf[MSG_BUF_SIZE] = {0}; |
| 71 | + va_list args; |
| 72 | + va_start(args, fmt); |
| 73 | + vPrint(fmt, args); |
| 74 | + va_end(args); |
| 75 | + } |
| 76 | +} |
91 | 77 |
|
92 |
| - vsnprintf(msg_buf, MSG_BUF_SIZE, fmt, args); |
| 78 | +/****************************************************************************** |
| 79 | + PRIVATE MEMBER FUNCTIONS |
| 80 | + ******************************************************************************/ |
93 | 81 |
|
94 |
| - _debug_output_stream->println(msg_buf); |
95 |
| - } |
| 82 | +void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) { |
| 83 | + static size_t const MSG_BUF_SIZE = 120; |
| 84 | + char msg_buf[MSG_BUF_SIZE] = {0}; |
96 | 85 |
|
97 |
| - /****************************************************************************** |
98 |
| - NAMESPACE |
99 |
| - ******************************************************************************/ |
| 86 | + vsnprintf(msg_buf, MSG_BUF_SIZE, fmt, args); |
100 | 87 |
|
101 |
| -} /* impl */ |
| 88 | + _debug_output_stream->println(msg_buf); |
| 89 | +} |
102 | 90 |
|
103 | 91 | /******************************************************************************
|
104 | 92 | CLASS INSTANTIATION
|
105 | 93 | ******************************************************************************/
|
106 | 94 |
|
107 |
| -impl::Arduino_DebugUtils Debug; |
| 95 | +Arduino_DebugUtils Debug; |
0 commit comments