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

Add flash helper support. #2

Open
wants to merge 2 commits 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
190 changes: 90 additions & 100 deletions Logging.cpp
Original file line number Diff line number Diff line change
@@ -1,119 +1,109 @@
#include "Logging.h"

void Logging::Init(int level, long baud){
_level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE);
_baud = baud;
Serial.begin(_baud);
_level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE);
_baud = baud;
Serial.begin(_baud);
}

void Logging::Error(char* msg, ...){
if (LOG_LEVEL_ERRORS <= _level) {
print ("ERROR: ",0);
va_list args;
va_start(args, msg);
print(msg,args);
void Logging::print(const __FlashStringHelper *format, va_list args) {
PGM_P p = reinterpret_cast<PGM_P>(format);
char c = pgm_read_byte(p++);
for(;c != 0; c = pgm_read_byte(p++)){
if (c == '%') {
c = pgm_read_byte(p++);
printFormat(c, &args);
} else {
Serial.print(c);
}
}

}

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

void Logging::Debug(char* msg, ...){
if (LOG_LEVEL_DEBUG <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
void Logging::print(const char *format, va_list args) {
for (; *format != 0; ++format) {
if (*format == '%') {
++format;
printFormat(*format, &args);
} else {
Serial.print(*format);
}
}
}


void Logging::Verbose(char* msg, ...){
if (LOG_LEVEL_VERBOSE <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
void Logging::printFormat(const char format, va_list *args) {
if (format == '\0') return;

if (format == '%') {
Serial.print(format);
return;
}

if( format == 's' ) {
register char *s = (char *)va_arg( *args, int );
Serial.print(s);
return;
}

if( format == 'd' || format == 'i') {
Serial.print(va_arg( *args, int ),DEC);
return;
}

if( format == 'x' ) {
Serial.print(va_arg( *args, int ),HEX);
return;
}

if( format == 'X' ) {
Serial.print("0x");
Serial.print(va_arg( *args, int ),HEX);
return;
}

if( format == 'b' ) {
Serial.print(va_arg( *args, int ),BIN);
return;
}

if( format == 'B' ) {
Serial.print("0b");
Serial.print(va_arg( *args, int ),BIN);
return;
}

if( format == 'l' ) {
Serial.print(va_arg( *args, long ),DEC);
return;
}

if( format == 'c' ) {
Serial.print(va_arg( *args, int ));
return;
}

if( format == 't' ) {
if (va_arg( *args, int ) == 1) {
Serial.print("T");
}
}
else {
Serial.print("F");
}
return;
}

if( format == 'T' ) {
if (va_arg( *args, int ) == 1) {
Serial.print(F("true"));
}
else {
Serial.print(F("false"));
}
return;
}

}

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);
continue;
}
if( *format == 's' ) {
register char *s = (char *)va_arg( args, int );
Serial.print(s);
continue;
}
if( *format == 'd' || *format == 'i') {
Serial.print(va_arg( args, int ),DEC);
continue;
}
if( *format == 'x' ) {
Serial.print(va_arg( args, int ),HEX);
continue;
}
if( *format == 'X' ) {
Serial.print("0x");
Serial.print(va_arg( args, int ),HEX);
continue;
}
if( *format == 'b' ) {
Serial.print(va_arg( args, int ),BIN);
continue;
}
if( *format == 'B' ) {
Serial.print("0b");
Serial.print(va_arg( args, int ),BIN);
continue;
}
if( *format == 'l' ) {
Serial.print(va_arg( args, long ),DEC);
continue;
}

if( *format == 'c' ) {
Serial.print(va_arg( args, int ));
continue;
}
if( *format == 't' ) {
if (va_arg( args, int ) == 1) {
Serial.print("T");
}
else {
Serial.print("F");
}
continue;
}
if( *format == 'T' ) {
if (va_arg( args, int ) == 1) {
Serial.print("true");
}
else {
Serial.print("false");
}
continue;
}

}
Serial.print(*format);
}
}

Logging Log = Logging();


Expand Down
47 changes: 39 additions & 8 deletions Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,17 @@ class Logging {
* \param ... any number of variables
* \return void
*/
void Error(char* msg, ...);


template <class T> void Error(T msg, ...){
if (LOG_LEVEL_ERRORS <= _level) {
print (F("ERROR: "),0);
va_list args;
va_start(args, msg);
print(msg,args);
}
}


/**
* Output an info message. Output message contains
* Info messages are printed out at l
Expand All @@ -107,8 +116,14 @@ class Logging {
* \return void
*/

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

template <class T> void Info(T msg, ...){
if (LOG_LEVEL_INFOS <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
}
}

/**
* Output an debug message. Output message contains
* Debug messages are printed out at l
Expand All @@ -119,8 +134,16 @@ class Logging {
* \return void
*/

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


template <class T> void Debug(T msg, ...){
if (LOG_LEVEL_DEBUG <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
}
}


/**
* Output an verbose message. Output message contains
* Debug messages are printed out at l
Expand All @@ -131,11 +154,19 @@ class Logging {
* \return void
*/

void Verbose(char* msg, ...);
template <class T> void Verbose(T msg, ...){
if (LOG_LEVEL_VERBOSE <= _level) {
va_list args;
va_start(args, msg);
print(msg,args);
}
}



private:
void print(const char *format, va_list args);
void print(const __FlashStringHelper *format, va_list args);
void printFormat(const char format, va_list *args);
};

extern Logging Log;
Expand Down