DebugTrace provides the facilities to dump debug output to either serial or a log file, and to turn it on/off as required.

.:How To Use:.


The demo (main.cpp, shown below) shows send debug out to serial and to file using a variety of formats. As it loops continually the log file should fill-up, create a backup and continue in the cycle.


Serial Dump: this the equivalent to Serial printf(), except you can easily turn it off, or change it to dump to file. Declare DebugTrace pc(ON, TO_SERIAL) to use. To turn off recompile as pc(OFF, TO_SERIAL).

File Dump: as default dumps to log.txt. When the file reaches 1024 bytes, the log file is saved to a backup file (e.g. log.bak), before being deleated and started again. For default usage declare DebugTrace file(ON, TO_FILE). If you want to a different log file name and size declare DebugTrace myfile(ON, TO_FILE, "mylog.txt", 2048) for example. NOTE - no file path name is required. You may wish to first perform a call to clear( ) to remove the previous log files (if any). To turn off recompile as file(OFF, TO_FILE).

The traceOut( ) function does the actual logging, and uses printf style parameters.

void traceOut(const char* fmt, ...);


#include "DebugTrace.h"

DebugTrace pc(ON, TO_SERIAL);
DebugTrace file(ON, TO_FILE);    // i.e. file(ON, TO_FILE, "log.txt", 1024)

int main() 
    int val = 122;
    float fval = 1.414;
    file.clear();        // remove any log file from last time
        pc.traceOut("Test message\r\n");
        pc.traceOut("%x \r\n", val);
        pc.traceOut("%d \r\n", val);
        pc.traceOut("%f \r\n", fval);
        file.traceOut("Test message\r\n");
        file.traceOut("%x \r\n", val);
        file.traceOut("%d \r\n", val);
        file.traceOut("%f \r\n", fval);

.:Library Files:.

DebugTrace.h - DebugTrace class declaraton

DebugTrace.cpp - DebugTrace source

.:TODO Wish List:.

1. Should read the size of any existing log file on startup, so that size limit file backup and deletion occurs correctly (for small log files this is probably not an issue, those using large log files should call clear( ) first.)

2. Would be nice if control of logging was done from a file, so just changing the file resulted in switch logging on/off, or moves it between serial and file. This would enable in the field debug without having to recompile to change settings.



15 Dec 2009

Here's how to do it in one function:

#include <stdarg.h>

void DebugTrace::traceOut(const char* fmt, ...)
    if (enabled)
        va_list ap;
        va_start(ap, fmt);    
        if (TO_SERIAL == logMode)
            vfprintf(logSerial, fmt, ap);
        else    // TO_FILE
            // [check code skipped]
            FILE* fp = fopen(logFile,"a");
            vfprintf(fp, fmt, ap);


16 Dec 2009

Thanks Igor - now incorporated into DebugTrace. That and a the log file backup file now done, so thats two TODOs off the list.

You need to log in to post a comment