DebugTrace provides the facilities to dump debug output to either serial or a log file, and to turn it on/off as required. Now supports fully supports printf style logging and creates a running backup log. 03/01/2010 - Potential memory leak fixed.
Revision 0:153a2086d828, committed 2010-01-03
- Comitter:
- snatch59
- Date:
- Sun Jan 03 11:51:51 2010 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 153a2086d828 DebugTrace.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebugTrace.cpp Sun Jan 03 11:51:51 2010 +0000 @@ -0,0 +1,143 @@ +/* +* DebugTrace. Allows dumping debug messages/values to serial or +* to file. +* +* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> +* +* This file is part of DebugTrace. +* +* DebugTrace is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DebugTrace is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "DebugTrace.h" +#include <mbed.h> +#include <stdarg.h> +#include <string.h> + +Serial logSerial(USBTX, USBRX); +LocalFileSystem local("local"); + +const char* FILE_PATH = "/local/"; +const char* EXTN = ".bak"; + +DebugTrace::DebugTrace(eLog on, eLogTarget mode, const char* fileName, int maxSize) : + enabled(on), logMode(mode), maxFileSize(maxSize), currentFileSize(0), + logFileStatus(0) +{ + // allocate memory for file name strings + int str_size = (strlen(fileName) + strlen(FILE_PATH) + strlen(EXTN) + 1) * sizeof(char); + logFile = (char*)malloc(str_size); + logFileBackup = (char*)malloc(str_size); + + // add path to log file name + strcpy(logFile, FILE_PATH); + strcat(logFile, fileName); + + // create backup file name + strcpy(logFileBackup, logFile); + strcpy(logFileBackup, strtok(logFileBackup, ".")); + strcat(logFileBackup, EXTN); +} + +DebugTrace::~DebugTrace() +{ + // dust to dust, ashes to ashes + if (logFile != NULL) free(logFile); + if (logFileBackup != NULL) free(logFileBackup); +} + +void DebugTrace::clear() +{ + // don't care about whether these fail + remove(logFile); + remove(logFileBackup); +} + +void DebugTrace::backupLog() +{ + // delete previous backup file + if (remove(logFileBackup)) + { + // standard copy stuff + char ch; + FILE* to = fopen(logFileBackup, "wb"); + if (NULL != to) + { + FILE* from = fopen(logFile, "rb"); + if (NULL != from) + { + while(!feof(from)) + { + ch = fgetc(from); + if (ferror(from)) break; + + if(!feof(from)) fputc(ch, to); + if (ferror(to)) break; + } + } + + if (NULL != from) fclose(from); + if (NULL != to) fclose(to); + } + } + + // now delete the log file, so we are ready to start again + // even if backup creation failed - the show must go on! + logFileStatus = remove(logFile); +} + +void DebugTrace::traceOut(const char* fmt, ...) +{ + if (enabled) + { + va_list ap; // argument list pointer + va_start(ap, fmt); + + if (TO_SERIAL == logMode) + { + vfprintf(logSerial, fmt, ap); + } + else // TO_FILE + { + if (0 == logFileStatus) // otherwise we failed to remove a full log file + { + // Write data to file. Note the file size may go over limit + // as we check total size afterwards, using the size written to file. + // This is not a big issue, as this mechanism is only here + // to stop the file growing unchecked. Just remember log file sizes may + // be some what over (as apposed to some what under), so don't push it + // with the max file size. + FILE* fp = fopen(logFile, "a"); + if (NULL == fp) + { + va_end(ap); + return; + } + int size_written = vfprintf(fp, fmt, ap); + fclose(fp); + + // check if we are over the max file size + // if so backup file and start again + currentFileSize += size_written; + if (currentFileSize >= maxFileSize) + { + backupLog(); + currentFileSize = 0; + } + } + } + + va_end(ap); + } +}
diff -r 000000000000 -r 153a2086d828 DebugTrace.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebugTrace.h Sun Jan 03 11:51:51 2010 +0000 @@ -0,0 +1,50 @@ +/* +* DebugTrace. Allows dumping debug messages/values to serial or +* to file. +* +* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> +* +* This file is part of DebugTrace. +* +* DebugTrace is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DebugTrace is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef SNATCH59_DEBUGTRACE_H +#define SNATCH59_DEBUGTRACE_H + +enum eLog {OFF, ON}; +enum eLogTarget {TO_SERIAL, TO_FILE}; + +class DebugTrace +{ +public: + DebugTrace(eLog on, eLogTarget mode, const char* fileName = "log.txt", const int maxSize = 1024); + ~DebugTrace(); + + void clear(); + void traceOut(const char* fmt, ...); + +private: + eLog enabled; + eLogTarget logMode; + int maxFileSize; + int currentFileSize; + char* logFile; + char* logFileBackup; + int logFileStatus; // if things go wrong, don't write any more data to file + + void backupLog(); +}; + +#endif
diff -r 000000000000 -r 153a2086d828 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jan 03 11:51:51 2010 +0000 @@ -0,0 +1,36 @@ +////////////////////////////////////////////// +// DebugTrace demo. +// The log file is written to /local/log.txt +// by default, max size 1024 bytes. +// When the log file is full, it is deleted +// and started again. +////////////////////////////////////////////// + +#include "DebugTrace.h" +#include <mbed.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 + + while(true) + { + 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); + + wait(2); + } +}
diff -r 000000000000 -r 153a2086d828 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jan 03 11:51:51 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0