DS18B20 helloWorld

Dependents:   DS1820_HelloWorld

Committer:
jack__zen
Date:
Wed Sep 06 05:34:36 2017 +0000
Revision:
0:1934d9b9fd80
debug

Who changed what in which revision?

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