mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-03-13 17:43:08 +01:00
added StringLogger
This commit is contained in:
parent
dc9368665d
commit
3c89d713fd
6
DIAG.h
6
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
|
||||
|
44
StringLogger.cpp
Normal file
44
StringLogger.cpp
Normal file
@ -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);
|
||||
|
||||
}
|
45
StringLogger.h
Normal file
45
StringLogger.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef StringLogger_h
|
||||
#define StringLogger_h
|
||||
|
||||
#include "MemStream.h"
|
||||
#include "RingStream.h"
|
||||
#include "StringFormatter.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
// 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
|
Loading…
Reference in New Issue
Block a user