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.

Dependencies:   mbed

Committer:
snatch59
Date:
Sun Jan 03 11:51:51 2010 +0000
Revision:
0:153a2086d828

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
snatch59 0:153a2086d828 1 /*
snatch59 0:153a2086d828 2 * DebugTrace. Allows dumping debug messages/values to serial or
snatch59 0:153a2086d828 3 * to file.
snatch59 0:153a2086d828 4 *
snatch59 0:153a2086d828 5 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
snatch59 0:153a2086d828 6 *
snatch59 0:153a2086d828 7 * This file is part of DebugTrace.
snatch59 0:153a2086d828 8 *
snatch59 0:153a2086d828 9 * DebugTrace is free software: you can redistribute it and/or modify
snatch59 0:153a2086d828 10 * it under the terms of the GNU General Public License as published by
snatch59 0:153a2086d828 11 * the Free Software Foundation, either version 3 of the License, or
snatch59 0:153a2086d828 12 * (at your option) any later version.
snatch59 0:153a2086d828 13 *
snatch59 0:153a2086d828 14 * DebugTrace is distributed in the hope that it will be useful,
snatch59 0:153a2086d828 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
snatch59 0:153a2086d828 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
snatch59 0:153a2086d828 17 * GNU General Public License for more details.
snatch59 0:153a2086d828 18 *
snatch59 0:153a2086d828 19 * You should have received a copy of the GNU General Public License
snatch59 0:153a2086d828 20 * along with DebugTrace. If not, see <http://www.gnu.org/licenses/>.
snatch59 0:153a2086d828 21 */
snatch59 0:153a2086d828 22
snatch59 0:153a2086d828 23 #include "DebugTrace.h"
snatch59 0:153a2086d828 24 #include <mbed.h>
snatch59 0:153a2086d828 25 #include <stdarg.h>
snatch59 0:153a2086d828 26 #include <string.h>
snatch59 0:153a2086d828 27
snatch59 0:153a2086d828 28 Serial logSerial(USBTX, USBRX);
snatch59 0:153a2086d828 29 LocalFileSystem local("local");
snatch59 0:153a2086d828 30
snatch59 0:153a2086d828 31 const char* FILE_PATH = "/local/";
snatch59 0:153a2086d828 32 const char* EXTN = ".bak";
snatch59 0:153a2086d828 33
snatch59 0:153a2086d828 34 DebugTrace::DebugTrace(eLog on, eLogTarget mode, const char* fileName, int maxSize) :
snatch59 0:153a2086d828 35 enabled(on), logMode(mode), maxFileSize(maxSize), currentFileSize(0),
snatch59 0:153a2086d828 36 logFileStatus(0)
snatch59 0:153a2086d828 37 {
snatch59 0:153a2086d828 38 // allocate memory for file name strings
snatch59 0:153a2086d828 39 int str_size = (strlen(fileName) + strlen(FILE_PATH) + strlen(EXTN) + 1) * sizeof(char);
snatch59 0:153a2086d828 40 logFile = (char*)malloc(str_size);
snatch59 0:153a2086d828 41 logFileBackup = (char*)malloc(str_size);
snatch59 0:153a2086d828 42
snatch59 0:153a2086d828 43 // add path to log file name
snatch59 0:153a2086d828 44 strcpy(logFile, FILE_PATH);
snatch59 0:153a2086d828 45 strcat(logFile, fileName);
snatch59 0:153a2086d828 46
snatch59 0:153a2086d828 47 // create backup file name
snatch59 0:153a2086d828 48 strcpy(logFileBackup, logFile);
snatch59 0:153a2086d828 49 strcpy(logFileBackup, strtok(logFileBackup, "."));
snatch59 0:153a2086d828 50 strcat(logFileBackup, EXTN);
snatch59 0:153a2086d828 51 }
snatch59 0:153a2086d828 52
snatch59 0:153a2086d828 53 DebugTrace::~DebugTrace()
snatch59 0:153a2086d828 54 {
snatch59 0:153a2086d828 55 // dust to dust, ashes to ashes
snatch59 0:153a2086d828 56 if (logFile != NULL) free(logFile);
snatch59 0:153a2086d828 57 if (logFileBackup != NULL) free(logFileBackup);
snatch59 0:153a2086d828 58 }
snatch59 0:153a2086d828 59
snatch59 0:153a2086d828 60 void DebugTrace::clear()
snatch59 0:153a2086d828 61 {
snatch59 0:153a2086d828 62 // don't care about whether these fail
snatch59 0:153a2086d828 63 remove(logFile);
snatch59 0:153a2086d828 64 remove(logFileBackup);
snatch59 0:153a2086d828 65 }
snatch59 0:153a2086d828 66
snatch59 0:153a2086d828 67 void DebugTrace::backupLog()
snatch59 0:153a2086d828 68 {
snatch59 0:153a2086d828 69 // delete previous backup file
snatch59 0:153a2086d828 70 if (remove(logFileBackup))
snatch59 0:153a2086d828 71 {
snatch59 0:153a2086d828 72 // standard copy stuff
snatch59 0:153a2086d828 73 char ch;
snatch59 0:153a2086d828 74 FILE* to = fopen(logFileBackup, "wb");
snatch59 0:153a2086d828 75 if (NULL != to)
snatch59 0:153a2086d828 76 {
snatch59 0:153a2086d828 77 FILE* from = fopen(logFile, "rb");
snatch59 0:153a2086d828 78 if (NULL != from)
snatch59 0:153a2086d828 79 {
snatch59 0:153a2086d828 80 while(!feof(from))
snatch59 0:153a2086d828 81 {
snatch59 0:153a2086d828 82 ch = fgetc(from);
snatch59 0:153a2086d828 83 if (ferror(from)) break;
snatch59 0:153a2086d828 84
snatch59 0:153a2086d828 85 if(!feof(from)) fputc(ch, to);
snatch59 0:153a2086d828 86 if (ferror(to)) break;
snatch59 0:153a2086d828 87 }
snatch59 0:153a2086d828 88 }
snatch59 0:153a2086d828 89
snatch59 0:153a2086d828 90 if (NULL != from) fclose(from);
snatch59 0:153a2086d828 91 if (NULL != to) fclose(to);
snatch59 0:153a2086d828 92 }
snatch59 0:153a2086d828 93 }
snatch59 0:153a2086d828 94
snatch59 0:153a2086d828 95 // now delete the log file, so we are ready to start again
snatch59 0:153a2086d828 96 // even if backup creation failed - the show must go on!
snatch59 0:153a2086d828 97 logFileStatus = remove(logFile);
snatch59 0:153a2086d828 98 }
snatch59 0:153a2086d828 99
snatch59 0:153a2086d828 100 void DebugTrace::traceOut(const char* fmt, ...)
snatch59 0:153a2086d828 101 {
snatch59 0:153a2086d828 102 if (enabled)
snatch59 0:153a2086d828 103 {
snatch59 0:153a2086d828 104 va_list ap; // argument list pointer
snatch59 0:153a2086d828 105 va_start(ap, fmt);
snatch59 0:153a2086d828 106
snatch59 0:153a2086d828 107 if (TO_SERIAL == logMode)
snatch59 0:153a2086d828 108 {
snatch59 0:153a2086d828 109 vfprintf(logSerial, fmt, ap);
snatch59 0:153a2086d828 110 }
snatch59 0:153a2086d828 111 else // TO_FILE
snatch59 0:153a2086d828 112 {
snatch59 0:153a2086d828 113 if (0 == logFileStatus) // otherwise we failed to remove a full log file
snatch59 0:153a2086d828 114 {
snatch59 0:153a2086d828 115 // Write data to file. Note the file size may go over limit
snatch59 0:153a2086d828 116 // as we check total size afterwards, using the size written to file.
snatch59 0:153a2086d828 117 // This is not a big issue, as this mechanism is only here
snatch59 0:153a2086d828 118 // to stop the file growing unchecked. Just remember log file sizes may
snatch59 0:153a2086d828 119 // be some what over (as apposed to some what under), so don't push it
snatch59 0:153a2086d828 120 // with the max file size.
snatch59 0:153a2086d828 121 FILE* fp = fopen(logFile, "a");
snatch59 0:153a2086d828 122 if (NULL == fp)
snatch59 0:153a2086d828 123 {
snatch59 0:153a2086d828 124 va_end(ap);
snatch59 0:153a2086d828 125 return;
snatch59 0:153a2086d828 126 }
snatch59 0:153a2086d828 127 int size_written = vfprintf(fp, fmt, ap);
snatch59 0:153a2086d828 128 fclose(fp);
snatch59 0:153a2086d828 129
snatch59 0:153a2086d828 130 // check if we are over the max file size
snatch59 0:153a2086d828 131 // if so backup file and start again
snatch59 0:153a2086d828 132 currentFileSize += size_written;
snatch59 0:153a2086d828 133 if (currentFileSize >= maxFileSize)
snatch59 0:153a2086d828 134 {
snatch59 0:153a2086d828 135 backupLog();
snatch59 0:153a2086d828 136 currentFileSize = 0;
snatch59 0:153a2086d828 137 }
snatch59 0:153a2086d828 138 }
snatch59 0:153a2086d828 139 }
snatch59 0:153a2086d828 140
snatch59 0:153a2086d828 141 va_end(ap);
snatch59 0:153a2086d828 142 }
snatch59 0:153a2086d828 143 }