Skip to content

Add runtime update of Debug Level and display settings. #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
robroypt committed Sep 6, 2024
commit 21b5ccd9221ebb20de7ac4893e664f6a54085eb9
76 changes: 71 additions & 5 deletions src/Arduino_DebugUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@

#include "Arduino_DebugUtils.h"

/******************************************************************************
INPUT BUFFER
******************************************************************************/

#define COMMAND_BUFFER_SIZE 50 // Define a reasonable size for the input buffer
char commandBuffer[COMMAND_BUFFER_SIZE];

/******************************************************************************
CONSTANTS
******************************************************************************/
Expand Down Expand Up @@ -54,7 +61,7 @@ int Arduino_DebugUtils::getDebugLevel() const {
}

void Arduino_DebugUtils::setDebugOutputStream(Stream * stream) {
_debug_output_stream = stream;
_debug_io_stream = stream;
}

void Arduino_DebugUtils::newlineOn() {
Expand Down Expand Up @@ -125,6 +132,65 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper
va_end(args);
}

void Arduino_DebugUtils::processDebugUpdateLevelCommand()
{
static size_t bufferIndex = 0; // Index to track buffer position

// Check if the stream is available and has data
if (_debug_io_stream && _debug_io_stream->available()) {
// Read each character from the stream
char incomingChar = _debug_io_stream->read();

// If it's a newline character, process the command
if (incomingChar == '\n') {
commandBuffer[bufferIndex] = '\0'; // Null-terminate the string

// Compare C-strings for each command
if (strcmp(commandBuffer, "V") == 0 || strcmp(commandBuffer, "VERBOSE") == 0)
{
setDebugLevel(DBG_VERBOSE);
_debug_io_stream->println("Debug level set to VERBOSE.");
}
else if (strcmp(commandBuffer, "D") == 0 || strcmp(commandBuffer, "DEBUG") == 0)
{
setDebugLevel(DBG_INFO);
_debug_io_stream->println("Debug level set to DEBUG.");
}
else if (strcmp(commandBuffer, "I") == 0 || strcmp(commandBuffer, "INFO") == 0)
{
setDebugLevel(DBG_INFO);
_debug_io_stream->println("Debug level set to INFO.");
}
else if (strcmp(commandBuffer, "W") == 0 || strcmp(commandBuffer, "WARNING") == 0)
{
setDebugLevel(DBG_WARNING);
_debug_io_stream->println("Debug level set to WARNING.");
}
else if (strcmp(commandBuffer, "E") == 0 || strcmp(commandBuffer, "ERROR") == 0)
{
setDebugLevel(DBG_ERROR);
_debug_io_stream->println("Debug level set to ERROR.");
}
else
{
_debug_io_stream->println("Invalid command. Use V,D,I,W,E or VERBOSE, DEBUG, INFO, WARNING, or ERROR.");
}

// Clear the buffer for the next command
bufferIndex = 0;
commandBuffer[0] = '\0';
}
else if (incomingChar != '\r')
{
// Add the character to the buffer if it's not a carriage return
if (bufferIndex < COMMAND_BUFFER_SIZE - 1)
{
commandBuffer[bufferIndex++] = incomingChar;
}
}
}
}

/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/
Expand All @@ -147,9 +213,9 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) {
va_end(args_copy);

if (_newline_on) {
_debug_output_stream->println(msg_buf);
_debug_io_stream->println(msg_buf);
} else {
_debug_output_stream->print(msg_buf);
_debug_io_stream->print(msg_buf);
}

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

_debug_output_stream->print(timestamp);
_debug_io_stream->print(timestamp);
}

void Arduino_DebugUtils::printDebugLabel(int const debug_level)
Expand All @@ -211,7 +277,7 @@ void Arduino_DebugUtils::printDebugLabel(int const debug_level)
if (!is_valid_debug_level)
return;

_debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
_debug_io_stream->print(DEBUG_MODE_STRING[debug_level]);
}

bool Arduino_DebugUtils::shouldPrint(int const debug_level) const
Expand Down
3 changes: 2 additions & 1 deletion src/Arduino_DebugUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Arduino_DebugUtils {
void print(int const debug_level, const char * fmt, ...);
void print(int const debug_level, const __FlashStringHelper * fmt, ...);

void processDebugUpdateLevelCommand();

private:

Expand All @@ -78,7 +79,7 @@ class Arduino_DebugUtils {
bool _print_debug_label;
bool _format_timestamp_on;
int _debug_level;
Stream * _debug_output_stream;
Stream * _debug_io_stream;

void vPrint(char const * fmt, va_list args);
void printTimestamp();
Expand Down