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