firmware/src/RedirectablePrint.cpp

111 lines
3.3 KiB
C++
Raw Normal View History

#include "configuration.h"
#include "RedirectablePrint.h"
#include "RTC.h"
#include "NodeDB.h"
2021-03-09 08:45:40 +00:00
#include "concurrency/OSThread.h"
2021-08-18 16:25:17 +00:00
// #include "wifi/WiFiServerAPI.h"
#include <assert.h>
#include <sys/time.h>
#include <time.h>
2021-05-24 19:38:06 +00:00
#include <cstring>
/**
* A printer that doesn't go anywhere
*/
NoopPrint noopPrint;
void RedirectablePrint::setDestination(Print *_dest)
{
assert(_dest);
dest = _dest;
}
size_t RedirectablePrint::write(uint8_t c)
{
// Always send the characters to our segger JTAG debugger
#ifdef SEGGER_STDOUT_CH
SEGGER_RTT_PutChar(SEGGER_STDOUT_CH, c);
#endif
2021-08-18 16:25:17 +00:00
// FIXME - clean this up, the whole relationship of this class to SerialConsole to TCP/bluetooth debug log output is kinda messed up. But for now, just have this hack to
// optionally send chars to TCP also
//WiFiServerPort::debugOut(c);
if (!config.has_lora || config.device.serial_enabled)
dest->write(c);
return 1; // We always claim one was written, rather than trusting what the
// 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);
2022-10-22 12:18:47 +00:00
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;
}
2022-12-30 13:48:59 +00:00
size_t RedirectablePrint::log(const char *logLevel, const char *format, ...)
{
size_t r = 0;
2021-03-09 08:45:40 +00:00
if (!inDebugPrint) {
inDebugPrint = true;
va_list arg;
va_start(arg, format);
2022-10-18 09:18:12 +00:00
// Cope with 0 len format strings, but look for new line terminator
2021-03-09 08:45:40 +00:00
bool hasNewline = *format && format[strlen(format) - 1] == '\n';
// If we are the first message on a report, include the header
if (!isContinuationMessage) {
uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice);
2021-03-15 02:42:57 +00:00
if (rtc_sec > 0) {
long hms = rtc_sec % SEC_PER_DAY;
2021-03-09 08:45:40 +00:00
// hms += tz.tz_dsttime * SEC_PER_HOUR;
// hms -= tz.tz_minuteswest * SEC_PER_MIN;
// mod `hms` to ensure in positive range of [0...SEC_PER_DAY)
hms = (hms + SEC_PER_DAY) % SEC_PER_DAY;
// Tear apart hms into h:m:s
int hour = hms / SEC_PER_HOUR;
int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN;
int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN
2022-12-30 13:48:59 +00:00
r += printf("%s | %02d:%02d:%02d %u ", logLevel, hour, min, sec, millis() / 1000);
2021-03-09 08:45:40 +00:00
} else
2022-12-30 13:48:59 +00:00
r += printf("%s | ??:??:?? %u ", logLevel, millis() / 1000);
2021-03-09 08:45:40 +00:00
auto thread = concurrency::OSThread::currentThread;
if (thread) {
print("[");
// printf("%p ", thread);
// assert(thread->ThreadName.length());
print(thread->ThreadName);
print("] ");
}
}
2021-03-09 08:45:40 +00:00
r += vprintf(format, arg);
va_end(arg);
2021-03-09 08:45:40 +00:00
isContinuationMessage = !hasNewline;
inDebugPrint = false;
}
return r;
2022-10-18 09:18:12 +00:00
}