From e1634ac5b35c27fc1e6d5ae5ec59b971758c4327 Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Thu, 19 Dec 2013 17:52:36 -0800 Subject: [PATCH] Added support for printing to arbitrary output derived from the Print class. Changed char* to const char* to be compatible print method Made \n automap to \r\n --- Logging.cpp | 58 ++++++++++++++++++++++++++++++----------------------- Logging.h | 30 ++++++++++++++++++++------- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/Logging.cpp b/Logging.cpp index aede239..638d49e 100644 --- a/Logging.cpp +++ b/Logging.cpp @@ -3,12 +3,18 @@ void Logging::Init(int level, long baud){ _level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE); _baud = baud; + _printer = &Serial; Serial.begin(_baud); } -void Logging::Error(char* msg, ...){ +void Logging::Init(int level, Print* printer){ + _level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE); + _printer = printer; +} + +void Logging::Error(const char* msg, ...){ if (LOG_LEVEL_ERRORS <= _level) { - print ("ERROR: ",0); + _printer->print("ERROR: "); va_list args; va_start(args, msg); print(msg,args); @@ -16,7 +22,7 @@ void Logging::Error(char* msg, ...){ } -void Logging::Info(char* msg, ...){ +void Logging::Info(const char* msg, ...){ if (LOG_LEVEL_INFOS <= _level) { va_list args; va_start(args, msg); @@ -24,7 +30,7 @@ void Logging::Info(char* msg, ...){ } } -void Logging::Debug(char* msg, ...){ +void Logging::Debug(const char* msg, ...){ if (LOG_LEVEL_DEBUG <= _level) { va_list args; va_start(args, msg); @@ -33,7 +39,7 @@ void Logging::Debug(char* msg, ...){ } -void Logging::Verbose(char* msg, ...){ +void Logging::Verbose(const char* msg, ...){ if (LOG_LEVEL_VERBOSE <= _level) { va_list args; va_start(args, msg); @@ -43,7 +49,7 @@ void Logging::Verbose(char* msg, ...){ - void Logging::print(const char *format, va_list args) { +void Logging::print(const char *format, va_list args) { // // loop through format string for (; *format != 0; ++format) { @@ -51,70 +57,72 @@ void Logging::Verbose(char* msg, ...){ ++format; if (*format == '\0') break; if (*format == '%') { - Serial.print(*format); + _printer->print(*format); continue; } if( *format == 's' ) { register char *s = (char *)va_arg( args, int ); - Serial.print(s); + _printer->print(s); continue; } if( *format == 'd' || *format == 'i') { - Serial.print(va_arg( args, int ),DEC); + _printer->print(va_arg( args, int ),DEC); continue; } if( *format == 'x' ) { - Serial.print(va_arg( args, int ),HEX); + _printer->print(va_arg( args, int ),HEX); continue; } if( *format == 'X' ) { - Serial.print("0x"); - Serial.print(va_arg( args, int ),HEX); + _printer->print("0x"); + _printer->print(va_arg( args, int ),HEX); continue; } if( *format == 'b' ) { - Serial.print(va_arg( args, int ),BIN); + _printer->print(va_arg( args, int ),BIN); continue; } if( *format == 'B' ) { - Serial.print("0b"); - Serial.print(va_arg( args, int ),BIN); + _printer->print("0b"); + _printer->print(va_arg( args, int ),BIN); continue; } if( *format == 'l' ) { - Serial.print(va_arg( args, long ),DEC); + _printer->print(va_arg( args, long ),DEC); continue; } if( *format == 'c' ) { - Serial.print(va_arg( args, int )); + _printer->print(va_arg( args, int )); continue; } if( *format == 't' ) { if (va_arg( args, int ) == 1) { - Serial.print("T"); + _printer->print("T"); } else { - Serial.print("F"); + _printer->print("F"); } continue; } if( *format == 'T' ) { if (va_arg( args, int ) == 1) { - Serial.print("true"); + _printer->print("true"); } else { - Serial.print("false"); + _printer->print("false"); } continue; } - } - Serial.print(*format); + else if (*format == '\n') { + _printer->print('\r'); + } + _printer->print(*format); } - } +} - Logging Log = Logging(); +Logging Log = Logging(); diff --git a/Logging.h b/Logging.h index 930fa1b..d7e6c36 100644 --- a/Logging.h +++ b/Logging.h @@ -23,7 +23,7 @@ extern "C" { #define LOGLEVEL LOG_LEVEL_DEBUG -#define CR "\r\n" +#define CR "\n" #define LOGGING_VERSION 1 /*! @@ -72,20 +72,36 @@ class Logging { private: int _level; long _baud; + Print* _printer; public: /*! * default Constructor */ - Logging(){} ; + Logging() + : _level(LOG_LEVEL_NOOUTPUT), + _baud(0), + _printer(NULL) {} /** * Initializing, must be called as first. - * \param void + * \param level - logging levels <= this will be logged. + * \param baud - baud rate to initialize the serial port to. * \return void * */ void Init(int level, long baud); + /** + * Initializing, must be called as first. Note that if you use + * this variant of Init, you need to initialize the baud rate + * yourself, if printer happens to be a serial port. + * \param level - logging levels <= this will be logged. + * \param printer - place that logging output will be sent to. + * \return void + * + */ + void Init(int level, Print *printer); + /** * Output an error message. Output message contains * ERROR: followed by original msg @@ -95,7 +111,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 +123,7 @@ class Logging { * \return void */ - void Info(char* msg, ...); + void Info(const char* msg, ...); /** * Output an debug message. Output message contains @@ -119,7 +135,7 @@ class Logging { * \return void */ - void Debug(char* msg, ...); + void Debug(const char* msg, ...); /** * Output an verbose message. Output message contains @@ -131,7 +147,7 @@ class Logging { * \return void */ - void Verbose(char* msg, ...); + void Verbose(const char* msg, ...); private: