Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for printing to arbitrary output derived from the Print class. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@
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);
}
}


void Logging::Info(char* msg, ...){
void Logging::Info(const char* msg, ...){
if (LOG_LEVEL_INFOS <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
}
}

void Logging::Debug(char* msg, ...){
void Logging::Debug(const char* msg, ...){
if (LOG_LEVEL_DEBUG <= _level) {
va_list args;
va_start(args, msg);
Expand All @@ -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);
Expand All @@ -43,78 +49,80 @@ 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) {
if (*format == '%') {
++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();



Expand Down
30 changes: 23 additions & 7 deletions Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {
#define LOGLEVEL LOG_LEVEL_DEBUG


#define CR "\r\n"
#define CR "\n"
#define LOGGING_VERSION 1

/*!
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -107,7 +123,7 @@ class Logging {
* \return void
*/

void Info(char* msg, ...);
void Info(const char* msg, ...);

/**
* Output an debug message. Output message contains
Expand All @@ -119,7 +135,7 @@ class Logging {
* \return void
*/

void Debug(char* msg, ...);
void Debug(const char* msg, ...);

/**
* Output an verbose message. Output message contains
Expand All @@ -131,7 +147,7 @@ class Logging {
* \return void
*/

void Verbose(char* msg, ...);
void Verbose(const char* msg, ...);


private:
Expand Down