Lukas Formanek / Mbed 2 deprecated OneWireCRC_LoRa_Node

Dependencies:   mbed

Fork of OneWireCRC by Petras Saduikis

Committer:
lukas_formanek
Date:
Mon Apr 30 17:09:56 2018 +0000
Revision:
2:d52f2e7dc8c0
Parent:
1:c4ccd94081d4
30.4.2018

Who changed what in which revision?

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