Library for Programmable Resolution 1-Wire Digital ThermometerDS18B20

Dependents:   DS18B20_HGC

Committer:
kriedel
Date:
Thu Feb 03 12:10:20 2011 +0000
Revision:
0:21c0c97a5d7c

        

Who changed what in which revision?

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