Skip to content

Commit 21b5ccd

Browse files
committed
Add live Debug Level switching
Listen to the same serial port used for debug output to switch Debug Level. Enter V,D,I,W,E or VERBOSE, DEBUG, INFO, WARNING, or ERROR terminated by a new line/Line feed ('\n) character. CRLF ('\r\n') works as well. NOTE: Check your serial monitor or terminal settings is configured to send a line ending of new line/Line feed ('\n' or '\r\n') at the end of the string.
1 parent 26cc8bf commit 21b5ccd

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/Arduino_DebugUtils.cpp

+71-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121

2222
#include "Arduino_DebugUtils.h"
2323

24+
/******************************************************************************
25+
INPUT BUFFER
26+
******************************************************************************/
27+
28+
#define COMMAND_BUFFER_SIZE 50 // Define a reasonable size for the input buffer
29+
char commandBuffer[COMMAND_BUFFER_SIZE];
30+
2431
/******************************************************************************
2532
CONSTANTS
2633
******************************************************************************/
@@ -54,7 +61,7 @@ int Arduino_DebugUtils::getDebugLevel() const {
5461
}
5562

5663
void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) {
57-
_debug_output_stream = stream;
64+
_debug_io_stream = stream;
5865
}
5966

6067
void Arduino_DebugUtils::newlineOn() {
@@ -125,6 +132,65 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper
125132
va_end(args);
126133
}
127134

135+
void Arduino_DebugUtils::processDebugUpdateLevelCommand()
136+
{
137+
static size_t bufferIndex = 0; // Index to track buffer position
138+
139+
// Check if the stream is available and has data
140+
if (_debug_io_stream && _debug_io_stream->available()) {
141+
// Read each character from the stream
142+
char incomingChar = _debug_io_stream->read();
143+
144+
// If it's a newline character, process the command
145+
if (incomingChar == '\n') {
146+
commandBuffer[bufferIndex] = '\0'; // Null-terminate the string
147+
148+
// Compare C-strings for each command
149+
if (strcmp(commandBuffer, "V") == 0 || strcmp(commandBuffer, "VERBOSE") == 0)
150+
{
151+
setDebugLevel(DBG_VERBOSE);
152+
_debug_io_stream->println("Debug level set to VERBOSE.");
153+
}
154+
else if (strcmp(commandBuffer, "D") == 0 || strcmp(commandBuffer, "DEBUG") == 0)
155+
{
156+
setDebugLevel(DBG_INFO);
157+
_debug_io_stream->println("Debug level set to DEBUG.");
158+
}
159+
else if (strcmp(commandBuffer, "I") == 0 || strcmp(commandBuffer, "INFO") == 0)
160+
{
161+
setDebugLevel(DBG_INFO);
162+
_debug_io_stream->println("Debug level set to INFO.");
163+
}
164+
else if (strcmp(commandBuffer, "W") == 0 || strcmp(commandBuffer, "WARNING") == 0)
165+
{
166+
setDebugLevel(DBG_WARNING);
167+
_debug_io_stream->println("Debug level set to WARNING.");
168+
}
169+
else if (strcmp(commandBuffer, "E") == 0 || strcmp(commandBuffer, "ERROR") == 0)
170+
{
171+
setDebugLevel(DBG_ERROR);
172+
_debug_io_stream->println("Debug level set to ERROR.");
173+
}
174+
else
175+
{
176+
_debug_io_stream->println("Invalid command. Use V,D,I,W,E or VERBOSE, DEBUG, INFO, WARNING, or ERROR.");
177+
}
178+
179+
// Clear the buffer for the next command
180+
bufferIndex = 0;
181+
commandBuffer[0] = '\0';
182+
}
183+
else if (incomingChar != '\r')
184+
{
185+
// Add the character to the buffer if it's not a carriage return
186+
if (bufferIndex < COMMAND_BUFFER_SIZE - 1)
187+
{
188+
commandBuffer[bufferIndex++] = incomingChar;
189+
}
190+
}
191+
}
192+
}
193+
128194
/******************************************************************************
129195
PRIVATE MEMBER FUNCTIONS
130196
******************************************************************************/
@@ -147,9 +213,9 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
147213
va_end(args_copy);
148214

149215
if (_newline_on) {
150-
_debug_output_stream->println(msg_buf);
216+
_debug_io_stream->println(msg_buf);
151217
} else {
152-
_debug_output_stream->print(msg_buf);
218+
_debug_io_stream->print(msg_buf);
153219
}
154220

155221
#if __STDC_NO_VLA__ == 1
@@ -193,7 +259,7 @@ void Arduino_DebugUtils::printTimestamp()
193259
snprintf(timestamp, sizeof(timestamp), "[ %lu ] ", millis());
194260
}
195261

196-
_debug_output_stream->print(timestamp);
262+
_debug_io_stream->print(timestamp);
197263
}
198264

199265
void Arduino_DebugUtils::printDebugLabel(int const debug_level)
@@ -211,7 +277,7 @@ void Arduino_DebugUtils::printDebugLabel(int const debug_level)
211277
if (!is_valid_debug_level)
212278
return;
213279

214-
_debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
280+
_debug_io_stream->print(DEBUG_MODE_STRING[debug_level]);
215281
}
216282

217283
bool Arduino_DebugUtils::shouldPrint(int const debug_level) const

src/Arduino_DebugUtils.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Arduino_DebugUtils {
7070
void print(int const debug_level, const char * fmt, ...);
7171
void print(int const debug_level, const __FlashStringHelper * fmt, ...);
7272

73+
void processDebugUpdateLevelCommand();
7374

7475
private:
7576

@@ -78,7 +79,7 @@ class Arduino_DebugUtils {
7879
bool _print_debug_label;
7980
bool _format_timestamp_on;
8081
int _debug_level;
81-
Stream * _debug_output_stream;
82+
Stream * _debug_io_stream;
8283

8384
void vPrint(char const * fmt, va_list args);
8485
void printTimestamp();

0 commit comments

Comments
 (0)