A copy of very incomplete program for forum

Dependencies:   mbed SDFileSystem

Committer:
roselea
Date:
Sat Mar 17 14:15:20 2012 +0000
Revision:
0:bfcb5b67b1d6

        

Who changed what in which revision?

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