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