First Publish

Dependencies:   BridgeDriver2 FrontPanelButtons MAX31855 MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Mon Nov 10 22:56:20 2014 +0000
Revision:
0:20e78c9d2ea9
First Commit of Falcon Wing

Who changed what in which revision?

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