Skip to content

Commit

Permalink
Make the logs Colorful! (#4199)
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett authored Jun 29, 2024
1 parent 9c232da commit 5263c73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
61 changes: 49 additions & 12 deletions src/RedirectablePrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,49 @@ size_t RedirectablePrint::vprintf(const char *format, va_list arg)
return len;
}

size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)
size_t RedirectablePrint::vprintf(const char *logLevel, const char *format, va_list arg)
{
va_list copy;
static char printBuf[160];

va_copy(copy, arg);
size_t len = vsnprintf(printBuf, sizeof(printBuf), format, copy);
va_end(copy);

// If the resulting string is longer than sizeof(printBuf)-1 characters, the remaining characters are still counted for the
// return value

if (len > sizeof(printBuf) - 1) {
len = sizeof(printBuf) - 1;
printBuf[sizeof(printBuf) - 2] = '\n';
}
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_DEBUG) == 0)
Print::write("\u001b[34m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_INFO) == 0)
Print::write("\u001b[32m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_WARN) == 0)
Print::write("\u001b[33m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_ERROR) == 0)
Print::write("\u001b[31m", 6);
len = Print::write(printBuf, len);
Print::write("\u001b[0m", 5);
return len;
}

void RedirectablePrint::log(const char *logLevel, const char *format, ...)
{
#ifdef ARCH_PORTDUINO
if (settingsMap[logoutputlevel] < level_debug && strcmp(logLevel, MESHTASTIC_LOG_LEVEL_DEBUG) == 0)
return 0;
return;
else if (settingsMap[logoutputlevel] < level_info && strcmp(logLevel, MESHTASTIC_LOG_LEVEL_INFO) == 0)
return 0;
return;
else if (settingsMap[logoutputlevel] < level_warn && strcmp(logLevel, MESHTASTIC_LOG_LEVEL_WARN) == 0)
return 0;
return;
#endif
if (moduleConfig.serial.override_console_serial_port && strcmp(logLevel, MESHTASTIC_LOG_LEVEL_DEBUG) == 0) {
return 0;
return;
}
size_t r = 0;

#ifdef HAS_FREE_RTOS
if (inDebugPrint != nullptr && xSemaphoreTake(inDebugPrint, portMAX_DELAY) == pdTRUE) {
#else
Expand All @@ -100,6 +129,14 @@ size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)

// If we are the first message on a report, include the header
if (!isContinuationMessage) {
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_DEBUG) == 0)
Print::write("\u001b[34m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_INFO) == 0)
Print::write("\u001b[32m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_WARN) == 0)
Print::write("\u001b[33m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_ERROR) == 0)
Print::write("\u001b[31m", 6);
uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice, true); // display local time on logfile
if (rtc_sec > 0) {
long hms = rtc_sec % SEC_PER_DAY;
Expand All @@ -113,15 +150,15 @@ size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)
int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN;
int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN
#ifdef ARCH_PORTDUINO
r += ::printf("%s | %02d:%02d:%02d %u ", logLevel, hour, min, sec, millis() / 1000);
::printf("%s \u001b[0m| %02d:%02d:%02d %u ", logLevel, hour, min, sec, millis() / 1000);
#else
r += printf("%s | %02d:%02d:%02d %u ", logLevel, hour, min, sec, millis() / 1000);
printf("%s \u001b[0m| %02d:%02d:%02d %u ", logLevel, hour, min, sec, millis() / 1000);
#endif
} else
#ifdef ARCH_PORTDUINO
r += ::printf("%s | ??:??:?? %u ", logLevel, millis() / 1000);
::printf("%s \u001b[0m| ??:??:?? %u ", logLevel, millis() / 1000);
#else
r += printf("%s | ??:??:?? %u ", logLevel, millis() / 1000);
printf("%s \u001b[0m| ??:??:?? %u ", logLevel, millis() / 1000);
#endif

auto thread = concurrency::OSThread::currentThread;
Expand All @@ -133,7 +170,7 @@ size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)
print("] ");
}
}
r += vprintf(format, arg);
vprintf(logLevel, format, arg);

#if (HAS_WIFI || HAS_ETHERNET) && !defined(ARCH_PORTDUINO)
// if syslog is in use, collect the log messages and send them to syslog
Expand Down Expand Up @@ -211,7 +248,7 @@ size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)
#endif
}

return r;
return;
}

void RedirectablePrint::hexDump(const char *logLevel, unsigned char *buf, uint16_t len)
Expand Down
3 changes: 2 additions & 1 deletion src/RedirectablePrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ class RedirectablePrint : public Print
* log message. Otherwise we assume more prints will come before the log message ends. This
* allows you to call logDebug a few times to build up a single log message line if you wish.
*/
size_t log(const char *logLevel, const char *format, ...) __attribute__((format(printf, 3, 4)));
void log(const char *logLevel, const char *format, ...) __attribute__((format(printf, 3, 4)));

/** like printf but va_list based */
size_t vprintf(const char *format, va_list arg);
size_t vprintf(const char *logLevel, const char *format, va_list arg);

void hexDump(const char *logLevel, unsigned char *buf, uint16_t len);

Expand Down

0 comments on commit 5263c73

Please sign in to comment.