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

Committer:
xenonym
Date:
2011-08-04
Revision:
0:eae9045546f3
Child:
1:41f6c5ec11c2

File content as of revision 0:eae9045546f3:

#include "xenonym_logging.h"

namespace xenonym { namespace logging {

RealLogger RealLogger::logger;
NullLogger NullLogger::logger;

RealLogger::RealLogger()
 : m_comms( USBTX, USBRX ), m_radix(10)
{
    m_comms.printf("\nLogging ENABLED");
}

RealLogger & RealLogger::operator<< ( unsigned i )
{
    switch( m_radix )
    {
    default:
        m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
    case 10:
        m_comms.printf( "%u", i ) ;
        break;
    case 16:
        m_comms.printf( "%x", i ) ;
        break;
    }
    return *this;
}

RealLogger & RealLogger::operator<< ( int i )
{
    switch( m_radix )
    {
    default:
        m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
    case 10:
        m_comms.printf( "%d", i ) ;
        break;
    case 16:
        m_comms.printf( "%x", i ) ;
        break;
    }
    return *this;
}

RealLogger & RealLogger::operator<< ( long long i )
{
    switch( m_radix )
    {
    default:
        m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
    case 10:
        m_comms.printf( "%lld", i ) ;
        break;
    case 16:
        m_comms.printf( "%llx", i ) ;
        break;
    }
    return *this;
}

RealLogger & RealLogger::operator<< ( unsigned long long int i )
{
    switch( m_radix )
    {
    default:
        m_comms.printf( "(bad radix:%d) dec:", m_radix ) ;
    case 10:
        m_comms.printf( "%llu", i ) ;
        break;
    case 16:
        m_comms.printf( "%llx", i ) ;
        break;
    }
    return *this;
}

RealLogger & RealLogger::operator<< ( char i )
{
    m_comms.printf( "%c", i ) ;
    return *this;
}

RealLogger & RealLogger::operator<< ( char const * str )
{
    m_comms.printf( "%s", str ) ;
    return *this;
}

class RealLogManipulator : public LogManipulator
{
public:
    virtual void log ( unsigned i )           { RealLogger::logger << i; }
    virtual void log ( int i )                { RealLogger::logger << i; }
    virtual void log ( long long i )          { RealLogger::logger << i; }
    virtual void log ( unsigned long long i ) { RealLogger::logger << i; }
    virtual void log ( char c )               { RealLogger::logger << c; }
    virtual void log ( char const * str )     { RealLogger::logger << str; }
    virtual void set_radix ( int i ) { RealLogger::logger.m_radix = i; }
    virtual void set_indent ( int i ) { RealLogger::logger.m_indent = i; }
    virtual int  get_indent ()        { return RealLogger::logger.m_indent; }
};

RealLogger & RealLogger::operator<< ( manipFunc manipulator )
{
    RealLogManipulator r;
    manipulator( r );
    return *this;
}

void eol( LogManipulator & lm ) { lm.log( "\r\n" ); for( int i = 0,  e = lm.get_indent(); i < e ; ++i ) { lm.log( (i%4)==2 ? '|' : ' ' ); } }
void hex( LogManipulator & lm ) { lm.set_radix( 16 ); }
void dec( LogManipulator & lm ) { lm.set_radix( 10 ); }
void indent( LogManipulator & lm )  { int i = lm.get_indent(); if ( i < 40 ) lm.set_indent( i + 2 ); }
void outdent( LogManipulator & lm ) { int i = lm.get_indent(); if ( i > 1  ) lm.set_indent( i - 2 ); }

Locn::Locn( char const * const file, int line ) 
: m_file(file), m_line( line ) 
{
}

Locn::~Locn()
{
}
        
Scope::Scope( char const * const file, int line ) 
: Locn( file, line ) 
{
    LOG << "entering scope " << file << ":" << dec << line << indent;
}
Scope::~Scope()
{
    LOGGER << outdent << eol << "leaving scope " << m_file << ":" << dec << m_line;
}

Func::Func( 
    char const * const name, char const * const file, int line 
) : Locn( file, line ), m_name( name )
{
    LOG << "entering " << name << " " << file << ":" << dec << line << indent;
}

Func::~Func() 
{
    LOGGER << outdent << eol << "leaving " << m_name << " " << m_file << ":" << dec << m_line;
}

} //    namespace logging
} //    namespace xenonym