From 3c89d713fd5049e96db1847a05fd2f67afcfe17a Mon Sep 17 00:00:00 2001 From: Gregor Baues Date: Wed, 2 Jun 2021 20:21:30 +0200 Subject: [PATCH] added StringLogger --- DIAG.h | 6 +++++- StringLogger.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ StringLogger.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 StringLogger.cpp create mode 100644 StringLogger.h diff --git a/DIAG.h b/DIAG.h index 1457796..e223639 100644 --- a/DIAG.h +++ b/DIAG.h @@ -20,6 +20,10 @@ #define DIAG_h #include "StringFormatter.h" -#define DIAG StringFormatter::diag +#include "StringLogger.h" + +// #define DIAG StringFormatter::diag +#define DIAG StringLogger::get().diag // allows to add other log writers #define LCD StringFormatter::lcd + #endif diff --git a/StringLogger.cpp b/StringLogger.cpp new file mode 100644 index 0000000..4b22279 --- /dev/null +++ b/StringLogger.cpp @@ -0,0 +1,44 @@ +#include "StringLogger.h" + +// DIAG.h the #define DIAG points to here ... +// EthernetSetup , Wifisetup, etc can register a function to be called allowing the channel +// to publish the diag info to +// serial is default end enabled all the time + +StringLogger StringLogger::singleton; // static instantiation; + +void StringLogger::addDiagWriter(DiagWriter l) { + if ( registered == MAXWRITERS ) { + Serial.println("Error: Max amount of writers exceeded."); + return; + } + writers[registered] = l; + registered++; +} + + +void StringLogger::diag(const FSH *input,...) +{ + + char b1[128]; + + va_list args; + va_start(args, input); + + int len = 0; + len += sprintf(&b1[len], "<* "); + len += vsprintf_P(&b1[len], (const char *)input, args); + len += sprintf(&b1[len], " *>\n"); + + // allways print to Serial + Serial.print(b1); + + // callback the other registered diag writers + for (size_t i = 0; i < (size_t) registered; i++) + { + writers[i](b1, len); + } + + va_end(args); + +} \ No newline at end of file diff --git a/StringLogger.h b/StringLogger.h new file mode 100644 index 0000000..72a2c4f --- /dev/null +++ b/StringLogger.h @@ -0,0 +1,45 @@ +#ifndef StringLogger_h +#define StringLogger_h + +#include "MemStream.h" +#include "RingStream.h" +#include "StringFormatter.h" +#include + +// stream for diagnostics in addition to serial you should be able to configure +// additional (besides Serial) 'outputs' to e.g. Ethernet or MQTT or WIFI etc ... +// Serial outpout is managed in StringFormatter on top for formatting the message +// which gets printed char by char + +#define MAXWRITERS 10 + +typedef void (*DiagWriter)(const char *msg, const int length); + +class StringLogger +{ + +private: + // Methods + StringLogger() = default; + StringLogger(const StringLogger &); // non construction-copyable + StringLogger &operator=(const StringLogger &); // non copyable + + // Members + static StringLogger singleton; // unique instance of the MQTTInterface object + DiagWriter writers[MAXWRITERS]; + int registered = 0; // number of registered writers ( Serial is not counted as always used ) + +public: + // Methods + static StringLogger &get() noexcept + { // return a reference to the unique instance + return singleton; + } + void diag(const FSH *input...); + void addDiagWriter(DiagWriter l); + ~StringLogger() = default; + + // Members +}; + +#endif \ No newline at end of file