DHT testprogram

Dependencies:   mbed

Fork of DHT_TestProgram by Wim De Roeve

Committer:
Wimpie
Date:
Tue Jul 10 13:11:23 2012 +0000
Revision:
0:9ef709bfeb08

        

Who changed what in which revision?

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