Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions examples/platformio-basic/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ String stringValue1 = "this is a string";
float floatValue;
double doubleValue;

void printTimestamp(Print *_logOutput) {
void printTimestamp(Print *_logOutput, int) {
char c[12];
int m = sprintf(c, "%10lu ", millis());
_logOutput->print(c);
}

void printCarret(Print *_logOutput) { _logOutput->print('>'); }
void printCarret(Print *_logOutput, int) { _logOutput->print('>'); }

void setup() {
// Set up serial port and wait until connected
Expand Down
47 changes: 33 additions & 14 deletions src/ArduinoLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

Logging::Logging()
#ifndef DISABLE_LOGGING
: _level(LOG_LEVEL_SILENT),
: _level(ArduinoLogLevel::LogLevelSilent),
_showLevel(true),
_showColors(false),
_handlerCount(0)
Expand All @@ -26,7 +26,17 @@ Logging::Logging()
{}
#endif

void Logging::begin(int level, Print* logOutput, bool showLevel, bool showColors) {
// Static helper function to constrain int values to valid enum range
ArduinoLogLevel Logging::constrainLevel(int level) {
if (level < static_cast<int>(ArduinoLogLevel::LogLevelSilent)) {
return ArduinoLogLevel::LogLevelSilent;
} else if (level > static_cast<int>(ArduinoLogLevel::LogLevelVerbose)) {
return ArduinoLogLevel::LogLevelVerbose;
}
return static_cast<ArduinoLogLevel>(level);
}

void Logging::begin(ArduinoLogLevel level, Print* logOutput, bool showLevel, bool showColors) {
#ifndef DISABLE_LOGGING
setLevel(level);
setShowLevel(showLevel);
Expand All @@ -45,15 +55,34 @@ void Logging::begin(int level, Print* logOutput, bool showLevel, bool showColors
#endif
}

// Backward compatibility: int overload
void Logging::begin(int level, Print *logOutput, bool showLevel, bool showColors) {
begin(constrainLevel(level), logOutput, showLevel, showColors);
}

void Logging::setLevel(ArduinoLogLevel level) {
#ifndef DISABLE_LOGGING
_level = level;
#endif
}

// Backward compatibility: int overload
void Logging::setLevel(int level) {
setLevel(constrainLevel(level));
}

ArduinoLogLevel Logging::getLogLevel() const {
#ifndef DISABLE_LOGGING
_level = constrain(level, LOG_LEVEL_SILENT, LOG_LEVEL_VERBOSE);
return _level;
#else
return ArduinoLogLevel::LogLevelSilent;
#endif
}

// Backward compatibility: return level as int
int Logging::getLevel() const {
#ifndef DISABLE_LOGGING
return _level;
return static_cast<int>(_level);
#else
return 0;
#endif
Expand Down Expand Up @@ -279,16 +308,6 @@ void Logging::printFormat(const char format, va_list *args) {
#endif
}

#ifndef DISABLE_LOGGING
template<typename... Args> void Logging::writeLog(Args... args) {
for (int i = 0; i < _handlerCount; i++) {
if (_logOutputs[i]) {
_logOutputs[i]->print(args...);
}
}
}
#endif

#ifndef __DO_NOT_INSTANTIATE__
Logging Log = Logging();
#endif
Loading