Simple logging library that outputs over the PC serial link to add logging to a file simply `include "xenonym_logging.h" ` then you can add logging with which looks slighly like c++ std::cout logging by using the LOG define or the LOGGER define thus; LOG << "this message will begin a new line" or LOGGER << "this message will append to the end of the previous log"; there is also a simple `LOGFUNC( "funcname" );` define that will log the entry and exit from a function and indent the stream so its easy to see where you are. if you want to disable the logging from a file without removing your logging all you need do is ~#define LOGGING_DISABLED and the logging will compile to nothing. also has simple manipulators hex,dec to change the output radix and eol to start a new line. i.e. <<code>> #include "xenonym_logging.h" using namespace xenonym; class Devr : public Base { public: Devr( int i ) : Base(i) { LOGFUNC( "mylib::Devr" ); ... do stuff .. LOG << "param is still " << logging::dec << i; .... do more stuff .... } virtual uint16_t something( uint16_t msg ) { LOGFUNC( "Derv::something" ); uint16_t reply = proccess( msg ); LOG << "sent:0x" << logging::hex << msg << " reply:0x"<< reply << logging::dec; return reply; } }; <</code>>

Committer:
xenonym
Date:
Thu Aug 04 21:59:29 2011 +0000
Revision:
1:41f6c5ec11c2
Parent:
0:eae9045546f3
updated so func and scope are also disabled when logging is turned off

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xenonym 1:41f6c5ec11c2 1 #include "xenonym_logging.h"
xenonym 1:41f6c5ec11c2 2
xenonym 1:41f6c5ec11c2 3 namespace xenonym { namespace logging {
xenonym 1:41f6c5ec11c2 4
xenonym 1:41f6c5ec11c2 5 RealLogger RealLogger::logger;
xenonym 1:41f6c5ec11c2 6 NullLogger NullLogger::logger;
xenonym 1:41f6c5ec11c2 7
xenonym 1:41f6c5ec11c2 8 RealLogger::RealLogger()
xenonym 1:41f6c5ec11c2 9 : m_comms( USBTX, USBRX ), m_radix(10)
xenonym 1:41f6c5ec11c2 10 {
xenonym 1:41f6c5ec11c2 11 m_comms.printf("\nLogging ENABLED");
xenonym 1:41f6c5ec11c2 12 }
xenonym 1:41f6c5ec11c2 13
xenonym 1:41f6c5ec11c2 14 RealLogger & RealLogger::operator<< ( unsigned i )
xenonym 1:41f6c5ec11c2 15 {
xenonym 1:41f6c5ec11c2 16 switch( m_radix )
xenonym 1:41f6c5ec11c2 17 {
xenonym 1:41f6c5ec11c2 18 default:
xenonym 1:41f6c5ec11c2 19 m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
xenonym 1:41f6c5ec11c2 20 case 10:
xenonym 1:41f6c5ec11c2 21 m_comms.printf( "%u", i ) ;
xenonym 1:41f6c5ec11c2 22 break;
xenonym 1:41f6c5ec11c2 23 case 16:
xenonym 1:41f6c5ec11c2 24 m_comms.printf( "%x", i ) ;
xenonym 1:41f6c5ec11c2 25 break;
xenonym 1:41f6c5ec11c2 26 }
xenonym 1:41f6c5ec11c2 27 return *this;
xenonym 1:41f6c5ec11c2 28 }
xenonym 1:41f6c5ec11c2 29
xenonym 1:41f6c5ec11c2 30 RealLogger & RealLogger::operator<< ( int i )
xenonym 1:41f6c5ec11c2 31 {
xenonym 1:41f6c5ec11c2 32 switch( m_radix )
xenonym 1:41f6c5ec11c2 33 {
xenonym 1:41f6c5ec11c2 34 default:
xenonym 1:41f6c5ec11c2 35 m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
xenonym 1:41f6c5ec11c2 36 case 10:
xenonym 1:41f6c5ec11c2 37 m_comms.printf( "%d", i ) ;
xenonym 1:41f6c5ec11c2 38 break;
xenonym 1:41f6c5ec11c2 39 case 16:
xenonym 1:41f6c5ec11c2 40 m_comms.printf( "%x", i ) ;
xenonym 1:41f6c5ec11c2 41 break;
xenonym 1:41f6c5ec11c2 42 }
xenonym 1:41f6c5ec11c2 43 return *this;
xenonym 1:41f6c5ec11c2 44 }
xenonym 1:41f6c5ec11c2 45
xenonym 1:41f6c5ec11c2 46 RealLogger & RealLogger::operator<< ( long long i )
xenonym 1:41f6c5ec11c2 47 {
xenonym 1:41f6c5ec11c2 48 switch( m_radix )
xenonym 1:41f6c5ec11c2 49 {
xenonym 1:41f6c5ec11c2 50 default:
xenonym 1:41f6c5ec11c2 51 m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
xenonym 1:41f6c5ec11c2 52 case 10:
xenonym 1:41f6c5ec11c2 53 m_comms.printf( "%lld", i ) ;
xenonym 1:41f6c5ec11c2 54 break;
xenonym 1:41f6c5ec11c2 55 case 16:
xenonym 1:41f6c5ec11c2 56 m_comms.printf( "%llx", i ) ;
xenonym 1:41f6c5ec11c2 57 break;
xenonym 1:41f6c5ec11c2 58 }
xenonym 1:41f6c5ec11c2 59 return *this;
xenonym 1:41f6c5ec11c2 60 }
xenonym 1:41f6c5ec11c2 61
xenonym 1:41f6c5ec11c2 62 RealLogger & RealLogger::operator<< ( unsigned long long int i )
xenonym 1:41f6c5ec11c2 63 {
xenonym 1:41f6c5ec11c2 64 switch( m_radix )
xenonym 1:41f6c5ec11c2 65 {
xenonym 1:41f6c5ec11c2 66 default:
xenonym 1:41f6c5ec11c2 67 m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
xenonym 1:41f6c5ec11c2 68 case 10:
xenonym 1:41f6c5ec11c2 69 m_comms.printf( "%llu", i ) ;
xenonym 1:41f6c5ec11c2 70 break;
xenonym 1:41f6c5ec11c2 71 case 16:
xenonym 1:41f6c5ec11c2 72 m_comms.printf( "%llx", i ) ;
xenonym 1:41f6c5ec11c2 73 break;
xenonym 1:41f6c5ec11c2 74 }
xenonym 1:41f6c5ec11c2 75 return *this;
xenonym 1:41f6c5ec11c2 76 }
xenonym 1:41f6c5ec11c2 77
xenonym 1:41f6c5ec11c2 78 RealLogger & RealLogger::operator<< ( char i )
xenonym 1:41f6c5ec11c2 79 {
xenonym 1:41f6c5ec11c2 80 m_comms.printf( "%c", i ) ;
xenonym 1:41f6c5ec11c2 81 return *this;
xenonym 1:41f6c5ec11c2 82 }
xenonym 1:41f6c5ec11c2 83
xenonym 1:41f6c5ec11c2 84 RealLogger & RealLogger::operator<< ( char const * str )
xenonym 1:41f6c5ec11c2 85 {
xenonym 1:41f6c5ec11c2 86 m_comms.printf( "%s", str ) ;
xenonym 1:41f6c5ec11c2 87 return *this;
xenonym 1:41f6c5ec11c2 88 }
xenonym 1:41f6c5ec11c2 89
xenonym 1:41f6c5ec11c2 90 class RealLogManipulator : public LogManipulator
xenonym 1:41f6c5ec11c2 91 {
xenonym 1:41f6c5ec11c2 92 public:
xenonym 1:41f6c5ec11c2 93 virtual void log ( unsigned i ) { RealLogger::logger << i; }
xenonym 1:41f6c5ec11c2 94 virtual void log ( int i ) { RealLogger::logger << i; }
xenonym 1:41f6c5ec11c2 95 virtual void log ( long long i ) { RealLogger::logger << i; }
xenonym 1:41f6c5ec11c2 96 virtual void log ( unsigned long long i ) { RealLogger::logger << i; }
xenonym 1:41f6c5ec11c2 97 virtual void log ( char c ) { RealLogger::logger << c; }
xenonym 1:41f6c5ec11c2 98 virtual void log ( char const * str ) { RealLogger::logger << str; }
xenonym 1:41f6c5ec11c2 99 virtual void set_radix ( int i ) { RealLogger::logger.m_radix = i; }
xenonym 1:41f6c5ec11c2 100 virtual void set_indent ( int i ) { RealLogger::logger.m_indent = i; }
xenonym 1:41f6c5ec11c2 101 virtual int get_indent () { return RealLogger::logger.m_indent; }
xenonym 1:41f6c5ec11c2 102 };
xenonym 1:41f6c5ec11c2 103
xenonym 1:41f6c5ec11c2 104 RealLogger & RealLogger::operator<< ( manipFunc manipulator )
xenonym 1:41f6c5ec11c2 105 {
xenonym 1:41f6c5ec11c2 106 RealLogManipulator r;
xenonym 1:41f6c5ec11c2 107 manipulator( r );
xenonym 1:41f6c5ec11c2 108 return *this;
xenonym 1:41f6c5ec11c2 109 }
xenonym 1:41f6c5ec11c2 110
xenonym 1:41f6c5ec11c2 111 void eol( LogManipulator & lm ) { lm.log( "\r\n" ); for( int i = 0, e = lm.get_indent(); i < e ; ++i ) { lm.log( (i%4)==2 ? '|' : ' ' ); } }
xenonym 1:41f6c5ec11c2 112 void hex( LogManipulator & lm ) { lm.set_radix( 16 ); }
xenonym 1:41f6c5ec11c2 113 void dec( LogManipulator & lm ) { lm.set_radix( 10 ); }
xenonym 1:41f6c5ec11c2 114 void indent( LogManipulator & lm ) { int i = lm.get_indent(); if ( i < 40 ) lm.set_indent( i + 2 ); }
xenonym 1:41f6c5ec11c2 115 void outdent( LogManipulator & lm ) { int i = lm.get_indent(); if ( i > 1 ) lm.set_indent( i - 2 ); }
xenonym 1:41f6c5ec11c2 116
xenonym 1:41f6c5ec11c2 117 Locn::Locn( char const * const file, int line )
xenonym 1:41f6c5ec11c2 118 : m_file(file), m_line( line )
xenonym 1:41f6c5ec11c2 119 {
xenonym 1:41f6c5ec11c2 120 }
xenonym 1:41f6c5ec11c2 121
xenonym 1:41f6c5ec11c2 122 Locn::~Locn()
xenonym 1:41f6c5ec11c2 123 {
xenonym 1:41f6c5ec11c2 124 }
xenonym 1:41f6c5ec11c2 125
xenonym 1:41f6c5ec11c2 126 Scope::Scope( char const * const file, int line )
xenonym 1:41f6c5ec11c2 127 : Locn( file, line )
xenonym 1:41f6c5ec11c2 128 {
xenonym 1:41f6c5ec11c2 129 LOG << "entering scope " << file << ":" << dec << line << indent;
xenonym 1:41f6c5ec11c2 130 }
xenonym 1:41f6c5ec11c2 131 Scope::~Scope()
xenonym 1:41f6c5ec11c2 132 {
xenonym 1:41f6c5ec11c2 133 LOGGER << outdent << eol << "leaving scope " << m_file << ":" << dec << m_line;
xenonym 1:41f6c5ec11c2 134 }
xenonym 1:41f6c5ec11c2 135
xenonym 1:41f6c5ec11c2 136 Func::Func(
xenonym 1:41f6c5ec11c2 137 char const * const name, char const * const file, int line
xenonym 1:41f6c5ec11c2 138 ) : Locn( file, line ), m_name( name )
xenonym 1:41f6c5ec11c2 139 {
xenonym 1:41f6c5ec11c2 140 LOG << "entering " << name << " " << file << ":" << dec << line << indent;
xenonym 1:41f6c5ec11c2 141 }
xenonym 1:41f6c5ec11c2 142
xenonym 1:41f6c5ec11c2 143 Func::~Func()
xenonym 1:41f6c5ec11c2 144 {
xenonym 1:41f6c5ec11c2 145 LOGGER << outdent << eol << "leaving " << m_name << " " << m_file << ":" << dec << m_line;
xenonym 1:41f6c5ec11c2 146 }
xenonym 1:41f6c5ec11c2 147
xenonym 1:41f6c5ec11c2 148 } // namespace logging
xenonym 1:41f6c5ec11c2 149 } // namespace xenonym