diff --git a/Logging.cpp b/Logging.cpp index aede239..5464fc6 100644 --- a/Logging.cpp +++ b/Logging.cpp @@ -1,120 +1,147 @@ #include "Logging.h" -void Logging::Init(int level, long baud){ +void Logging::init(int level, long baud){ _level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE); _baud = baud; Serial.begin(_baud); + + HardwareSerial &serial = Serial; + _stream = &static_cast(serial); + + info("Start logging"); +} +void Logging::init(int level, long baud, Stream* additionalLogger) { + _additionalLogger = additionalLogger; + init(level, baud); } -void Logging::Error(char* msg, ...){ - if (LOG_LEVEL_ERRORS <= _level) { - print ("ERROR: ",0); +void Logging::error(const char* msg, ...){ + if (LOG_LEVEL_ERRORS <= _level) { + print("E: "); va_list args; va_start(args, msg); - print(msg,args); + printFormat(msg, args); } } -void Logging::Info(char* msg, ...){ +void Logging::info(const char* msg, ...){ if (LOG_LEVEL_INFOS <= _level) { + print("I: "); va_list args; va_start(args, msg); - print(msg,args); + printFormat(msg,args); } } -void Logging::Debug(char* msg, ...){ +void Logging::debug(const char* msg, ...){ if (LOG_LEVEL_DEBUG <= _level) { + print("D: "); va_list args; va_start(args, msg); - print(msg,args); + printFormat(msg,args); } } -void Logging::Verbose(char* msg, ...){ +void Logging::verbose(const char* msg, ...){ if (LOG_LEVEL_VERBOSE <= _level) { + print("V: "); va_list args; va_start(args, msg); - print(msg,args); + printFormat(msg,args); } } +void Logging::print(const char *msg) { + _stream->print(msg); + if (_additionalLogger != 0) + _additionalLogger->print(msg); +} +void Logging::printFormat(const char *format, va_list args) { + printFormat(_stream, format, args); + if (_additionalLogger != 0) + printFormat(_additionalLogger, format, args); +} - - void Logging::print(const char *format, va_list args) { - // +void Logging::printFormat(Stream *stream, const char *format, va_list args) { // loop through format string for (; *format != 0; ++format) { if (*format == '%') { ++format; if (*format == '\0') break; if (*format == '%') { - Serial.print(*format); + stream->print(*format); continue; } if( *format == 's' ) { register char *s = (char *)va_arg( args, int ); - Serial.print(s); + stream->print(s); continue; } if( *format == 'd' || *format == 'i') { - Serial.print(va_arg( args, int ),DEC); + stream->print(va_arg( args, int ),DEC); + continue; + } + if( *format == 'f') { + stream->print(va_arg( args, double ),DEC); continue; } if( *format == 'x' ) { - Serial.print(va_arg( args, int ),HEX); + stream->print(va_arg( args, int ),HEX); continue; } if( *format == 'X' ) { - Serial.print("0x"); - Serial.print(va_arg( args, int ),HEX); + stream->print("0x"); + stream->print(va_arg( args, int ),HEX); continue; } if( *format == 'b' ) { - Serial.print(va_arg( args, int ),BIN); + stream->print(va_arg( args, int ),BIN); continue; } if( *format == 'B' ) { - Serial.print("0b"); - Serial.print(va_arg( args, int ),BIN); + stream->print("0b"); + stream->print(va_arg( args, int ),BIN); continue; } if( *format == 'l' ) { - Serial.print(va_arg( args, long ),DEC); + stream->print(va_arg( args, long ),DEC); continue; } if( *format == 'c' ) { - Serial.print(va_arg( args, int )); + stream->print(va_arg( args, int )); continue; } if( *format == 't' ) { if (va_arg( args, int ) == 1) { - Serial.print("T"); + stream->print("T"); } else { - Serial.print("F"); + stream->print("F"); } continue; } if( *format == 'T' ) { if (va_arg( args, int ) == 1) { - Serial.print("true"); + stream->print("true"); } else { - Serial.print("false"); + stream->print("false"); } continue; } } - Serial.print(*format); + stream->print(*format); } + stream->println(); } - Logging Log = Logging(); + + +Logging Log = Logging(); diff --git a/Logging.h b/Logging.h index 930fa1b..4b71bd9 100644 --- a/Logging.h +++ b/Logging.h @@ -7,11 +7,9 @@ #else #include "WProgram.h" #endif -//#include "pins_arduino.h" -extern "C" { - #include -} +#include +#include #define LOG_LEVEL_NOOUTPUT 0 #define LOG_LEVEL_ERRORS 1 @@ -22,8 +20,6 @@ extern "C" { // default loglevel if nothing is set from user #define LOGLEVEL LOG_LEVEL_DEBUG - -#define CR "\r\n" #define LOGGING_VERSION 1 /*! @@ -84,7 +80,8 @@ class Logging { * \return void * */ - void Init(int level, long baud); + void init(int level, long baud); + void init(int level, long baud, Stream* additionalLogger); /** * Output an error message. Output message contains @@ -95,7 +92,7 @@ class Logging { * \param ... any number of variables * \return void */ - void Error(char* msg, ...); + void error(const char* msg, ...); /** * Output an info message. Output message contains @@ -107,7 +104,7 @@ class Logging { * \return void */ - void Info(char* msg, ...); + void info(const char* msg, ...); /** * Output an debug message. Output message contains @@ -119,7 +116,7 @@ class Logging { * \return void */ - void Debug(char* msg, ...); + void debug(const char* msg, ...); /** * Output an verbose message. Output message contains @@ -131,11 +128,15 @@ class Logging { * \return void */ - void Verbose(char* msg, ...); - + void verbose(const char* msg, ...); private: - void print(const char *format, va_list args); + void print(const char *msg); + void printFormat(const char *format, va_list args); + void printFormat(Stream *stream, const char *format, va_list args); + + Stream* _stream; + Stream* _additionalLogger; }; extern Logging Log; diff --git a/Logging.zip b/Logging.zip deleted file mode 100644 index 6978edb..0000000 Binary files a/Logging.zip and /dev/null differ