00001 #include "Logging.h"
00002
00003 void Logging::Init(int level, long baud){
00004 _level = constrain(level,LOG_LEVEL_NOOUTPUT,LOG_LEVEL_VERBOSE);
00005 _baud = baud;
00006 Serial.begin(_baud);
00007 }
00008
00009 void Logging::Error(char* msg, ...){
00010 if (LOG_LEVEL_ERRORS <= _level) {
00011 print ("ERROR: ",0);
00012 va_list args;
00013 va_start(args, msg);
00014 print(msg,args);
00015 }
00016 }
00017
00018
00019 void Logging::Info(char* msg, ...){
00020 if (LOG_LEVEL_INFOS <= _level) {
00021 va_list args;
00022 va_start(args, msg);
00023 print(msg,args);
00024 }
00025 }
00026
00027 void Logging::Debug(char* msg, ...){
00028 if (LOG_LEVEL_DEBUG <= _level) {
00029 va_list args;
00030 va_start(args, msg);
00031 print(msg,args);
00032 }
00033 }
00034
00035
00036 void Logging::Verbose(char* msg, ...){
00037 if (LOG_LEVEL_VERBOSE <= _level) {
00038 va_list args;
00039 va_start(args, msg);
00040 print(msg,args);
00041 }
00042 }
00043
00044
00045
00046 void Logging::print(const char *format, va_list args) {
00047
00048
00049 for (; *format != 0; ++format) {
00050 if (*format == '%') {
00051 ++format;
00052 if (*format == '\0') break;
00053 if (*format == '%') {
00054 Serial.print(*format);
00055 continue;
00056 }
00057 if( *format == 's' ) {
00058 register char *s = (char *)va_arg( args, int );
00059 Serial.print(s);
00060 continue;
00061 }
00062 if( *format == 'd' || *format == 'i') {
00063 Serial.print(va_arg( args, int ),DEC);
00064 continue;
00065 }
00066 if( *format == 'x' ) {
00067 Serial.print(va_arg( args, int ),HEX);
00068 continue;
00069 }
00070 if( *format == 'X' ) {
00071 Serial.print("0x");
00072 Serial.print(va_arg( args, int ),HEX);
00073 continue;
00074 }
00075 if( *format == 'b' ) {
00076 Serial.print(va_arg( args, int ),BIN);
00077 continue;
00078 }
00079 if( *format == 'B' ) {
00080 Serial.print("0b");
00081 Serial.print(va_arg( args, int ),BIN);
00082 continue;
00083 }
00084 if( *format == 'l' ) {
00085 Serial.print(va_arg( args, long ),DEC);
00086 continue;
00087 }
00088
00089 if( *format == 'c' ) {
00090 Serial.print(va_arg( args, int ));
00091 continue;
00092 }
00093 if( *format == 't' ) {
00094 if (va_arg( args, int ) == 1) {
00095 Serial.print("T");
00096 }
00097 else {
00098 Serial.print("F");
00099 }
00100 continue;
00101 }
00102 if( *format == 'T' ) {
00103 if (va_arg( args, int ) == 1) {
00104 Serial.print("true");
00105 }
00106 else {
00107 Serial.print("false");
00108 }
00109 continue;
00110 }
00111
00112 }
00113 Serial.print(*format);
00114 }
00115 }
00116
00117 Logging Log = Logging();
00118
00119
00120
00121
00122
00123
00124
00125