add real formatted debug logging with timestamps

This commit is contained in:
Kevin Hester
2020-12-25 15:17:56 +08:00
parent c349ad62e7
commit 59577b9d79
5 changed files with 108 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include <Print.h>
#include <stdarg.h>
/**
* A Printable that can be switched to squirt its bytes to a different sink.
@@ -11,6 +12,13 @@ class RedirectablePrint : public Print
{
Print *dest;
/// We dynamically grow this scratch buffer if necessary
char *printBuf = new char[64];
size_t printBufLen = 64;
/// Used to allow multiple logDebug messages to appear on a single log line
bool isContinuationMessage = false;
public:
RedirectablePrint(Print *_dest) : dest(_dest) {}
@@ -20,6 +28,21 @@ class RedirectablePrint : public Print
void setDestination(Print *dest);
virtual size_t write(uint8_t c);
/**
* Debug logging print message
*
* If the provide format string ends with a newline we assume it is the final print of a single
* 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.
*
* FIXME, eventually add log levels (INFO, WARN, ERROR) and subsystems. Move into
* a different class.
*/
size_t logDebug(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
/** like printf but va_list based */
size_t vprintf(const char *format, va_list arg);
};
class NoopPrint : public Print