Skip to content

Commit

Permalink
Trim extra vprintf and filter for unprintable characters
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett committed Jun 29, 2024
1 parent ca969e2 commit 6f3d7ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
43 changes: 14 additions & 29 deletions src/RedirectablePrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ size_t RedirectablePrint::write(uint8_t c)
// serial port said (which could be zero)
}

size_t RedirectablePrint::vprintf(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';
}

len = Print::write(printBuf, len);
return len;
}

size_t RedirectablePrint::vprintf(const char *logLevel, const char *format, va_list arg)
{
va_list copy;
Expand All @@ -87,14 +66,20 @@ size_t RedirectablePrint::vprintf(const char *logLevel, const char *format, va_l
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);
for (size_t f = 0; f < len; f++) {
if (!std::isprint(static_cast<unsigned char>(printBuf[f])) && printBuf[f] != '\n')
printBuf[f] = '#';
}
if (logLevel != nullptr) {
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;
Expand Down
1 change: 0 additions & 1 deletion src/RedirectablePrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class RedirectablePrint : public Print
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
2 changes: 1 addition & 1 deletion src/SerialConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void consolePrintf(const char *format, ...)
{
va_list arg;
va_start(arg, format);
console->vprintf(format, arg);
console->vprintf(nullptr, format, arg);
va_end(arg);
console->flush();
}
Expand Down

0 comments on commit 6f3d7ca

Please sign in to comment.