Monitor for central heating system (e.g. 2zones+hw) Supports up to 15 temp probes (DS18B20/DS18S20) 3 valve monitors Gas pulse meter recording Use stand-alone or with nodeEnergyServer See http://robdobson.com/2015/09/central-heating-monitor
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
Logger.cpp@23:fd5a5a9f30bc, 2015-10-16 (annotated)
- Committer:
- Bobty
- Date:
- Fri Oct 16 09:07:04 2015 +0000
- Revision:
- 23:fd5a5a9f30bc
- Parent:
- 20:7933076df5af
Added index.html file to project for completeness
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 12:a52996515063 | 1 | // Log to SD |
Bobty | 12:a52996515063 | 2 | // Rob Dobson, 2015 |
Bobty | 12:a52996515063 | 3 | |
Bobty | 12:a52996515063 | 4 | #include "Logger.h" |
Bobty | 12:a52996515063 | 5 | |
Bobty | 20:7933076df5af | 6 | Logger::Logger(const char* eventLogFileName, const char* dataLogFileBase, Mutex &sdCardMutex) : |
Bobty | 20:7933076df5af | 7 | _sdCardMutex(sdCardMutex) |
Bobty | 12:a52996515063 | 8 | { |
Bobty | 12:a52996515063 | 9 | _eventLogFileName = eventLogFileName; |
Bobty | 12:a52996515063 | 10 | _dataLogFileBase = dataLogFileBase; |
Bobty | 20:7933076df5af | 11 | _logDebugToFile = false; |
Bobty | 20:7933076df5af | 12 | _logDebugToConsole = false; |
Bobty | 12:a52996515063 | 13 | } |
Bobty | 12:a52996515063 | 14 | |
Bobty | 12:a52996515063 | 15 | void Logger::LogEvent(const char* format, ...) |
Bobty | 12:a52996515063 | 16 | { |
Bobty | 12:a52996515063 | 17 | char timeBuf[40]; |
Bobty | 12:a52996515063 | 18 | time_t seconds = time(NULL); |
Bobty | 12:a52996515063 | 19 | strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds)); |
Bobty | 12:a52996515063 | 20 | |
Bobty | 20:7933076df5af | 21 | // Obtain lock to access sd card - if unable then abort the logging process |
Bobty | 20:7933076df5af | 22 | if (!_sdCardMutex.trylock()) |
Bobty | 20:7933076df5af | 23 | return; |
Bobty | 20:7933076df5af | 24 | |
Bobty | 20:7933076df5af | 25 | // Open file |
Bobty | 12:a52996515063 | 26 | FILE* fp = fopen(_eventLogFileName, "a"); |
Bobty | 12:a52996515063 | 27 | if (fp == NULL) |
Bobty | 12:a52996515063 | 28 | { |
Bobty | 12:a52996515063 | 29 | printf ("Event Log ... Filename %s not found\r\n", _eventLogFileName); |
Bobty | 12:a52996515063 | 30 | } |
Bobty | 12:a52996515063 | 31 | else |
Bobty | 12:a52996515063 | 32 | { |
Bobty | 12:a52996515063 | 33 | fprintf(fp, timeBuf); |
Bobty | 12:a52996515063 | 34 | va_list argptr; |
Bobty | 12:a52996515063 | 35 | va_start(argptr, format); |
Bobty | 12:a52996515063 | 36 | vfprintf(fp, format, argptr); |
Bobty | 12:a52996515063 | 37 | va_end(argptr); |
Bobty | 12:a52996515063 | 38 | fprintf(fp, "\r\n"); |
Bobty | 12:a52996515063 | 39 | fclose(fp); |
Bobty | 12:a52996515063 | 40 | } |
Bobty | 20:7933076df5af | 41 | |
Bobty | 20:7933076df5af | 42 | // Release lock on sd card |
Bobty | 20:7933076df5af | 43 | _sdCardMutex.unlock(); |
Bobty | 20:7933076df5af | 44 | |
Bobty | 12:a52996515063 | 45 | } |
Bobty | 12:a52996515063 | 46 | |
Bobty | 12:a52996515063 | 47 | // Utility function to log data |
Bobty | 12:a52996515063 | 48 | void Logger::LogData(const char* format, ...) |
Bobty | 12:a52996515063 | 49 | { |
Bobty | 12:a52996515063 | 50 | char fileNameBuf[60]; |
Bobty | 12:a52996515063 | 51 | strcpy(fileNameBuf, _dataLogFileBase); |
Bobty | 12:a52996515063 | 52 | time_t seconds = time(NULL); |
Bobty | 12:a52996515063 | 53 | strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Data_%Y%m%d.txt", localtime(&seconds)); |
Bobty | 12:a52996515063 | 54 | |
Bobty | 20:7933076df5af | 55 | // Obtain lock to access sd card - if unable then abort the logging process |
Bobty | 20:7933076df5af | 56 | if (!_sdCardMutex.trylock()) |
Bobty | 20:7933076df5af | 57 | return; |
Bobty | 20:7933076df5af | 58 | |
Bobty | 12:a52996515063 | 59 | FILE* fp = fopen(fileNameBuf, "a"); |
Bobty | 12:a52996515063 | 60 | if (fp == NULL) |
Bobty | 12:a52996515063 | 61 | { |
Bobty | 12:a52996515063 | 62 | printf ("Data Log ... Filename %s not found\r\n", _eventLogFileName); |
Bobty | 12:a52996515063 | 63 | } |
Bobty | 12:a52996515063 | 64 | else |
Bobty | 12:a52996515063 | 65 | { |
Bobty | 12:a52996515063 | 66 | va_list argptr; |
Bobty | 12:a52996515063 | 67 | va_start(argptr, format); |
Bobty | 12:a52996515063 | 68 | vfprintf(fp, format, argptr); |
Bobty | 12:a52996515063 | 69 | va_end(argptr); |
Bobty | 12:a52996515063 | 70 | fprintf(fp, "\r\n"); |
Bobty | 12:a52996515063 | 71 | fclose(fp); |
Bobty | 12:a52996515063 | 72 | } |
Bobty | 20:7933076df5af | 73 | |
Bobty | 20:7933076df5af | 74 | // Release lock on sd card |
Bobty | 20:7933076df5af | 75 | _sdCardMutex.unlock(); |
Bobty | 12:a52996515063 | 76 | } |
Bobty | 20:7933076df5af | 77 | |
Bobty | 20:7933076df5af | 78 | // Utility function to log data |
Bobty | 20:7933076df5af | 79 | void Logger::LogDebug(const char* format, ...) |
Bobty | 20:7933076df5af | 80 | { |
Bobty | 20:7933076df5af | 81 | char fileNameBuf[60]; |
Bobty | 20:7933076df5af | 82 | strcpy(fileNameBuf, _dataLogFileBase); |
Bobty | 20:7933076df5af | 83 | time_t seconds = time(NULL); |
Bobty | 20:7933076df5af | 84 | strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Dbg_%Y%m%d.txt", localtime(&seconds)); |
Bobty | 20:7933076df5af | 85 | |
Bobty | 20:7933076df5af | 86 | // Log to file if enabled |
Bobty | 20:7933076df5af | 87 | if (_logDebugToFile) |
Bobty | 20:7933076df5af | 88 | { |
Bobty | 20:7933076df5af | 89 | // Obtain lock to access sd card - if unable then abort the logging process |
Bobty | 20:7933076df5af | 90 | if (_sdCardMutex.trylock()) |
Bobty | 20:7933076df5af | 91 | { |
Bobty | 20:7933076df5af | 92 | FILE* fp = fopen(fileNameBuf, "a"); |
Bobty | 20:7933076df5af | 93 | if (fp == NULL) |
Bobty | 20:7933076df5af | 94 | { |
Bobty | 20:7933076df5af | 95 | printf ("Data Log ... Filename %s not found\r\n", _eventLogFileName); |
Bobty | 20:7933076df5af | 96 | } |
Bobty | 20:7933076df5af | 97 | else |
Bobty | 20:7933076df5af | 98 | { |
Bobty | 20:7933076df5af | 99 | char timeBuf[40]; |
Bobty | 20:7933076df5af | 100 | time_t seconds = time(NULL); |
Bobty | 20:7933076df5af | 101 | strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds)); |
Bobty | 20:7933076df5af | 102 | fprintf(fp, timeBuf); |
Bobty | 20:7933076df5af | 103 | va_list argptr; |
Bobty | 20:7933076df5af | 104 | va_start(argptr, format); |
Bobty | 20:7933076df5af | 105 | vfprintf(fp, format, argptr); |
Bobty | 20:7933076df5af | 106 | va_end(argptr); |
Bobty | 20:7933076df5af | 107 | fprintf(fp, "\r\n"); |
Bobty | 20:7933076df5af | 108 | } |
Bobty | 20:7933076df5af | 109 | fclose(fp); |
Bobty | 20:7933076df5af | 110 | |
Bobty | 20:7933076df5af | 111 | // Release lock on sd card |
Bobty | 20:7933076df5af | 112 | _sdCardMutex.unlock(); |
Bobty | 20:7933076df5af | 113 | } |
Bobty | 20:7933076df5af | 114 | } |
Bobty | 20:7933076df5af | 115 | |
Bobty | 20:7933076df5af | 116 | // Print to terminal if enabled |
Bobty | 20:7933076df5af | 117 | if (_logDebugToConsole) |
Bobty | 20:7933076df5af | 118 | { |
Bobty | 20:7933076df5af | 119 | va_list argptr; |
Bobty | 20:7933076df5af | 120 | va_start(argptr, format); |
Bobty | 20:7933076df5af | 121 | vprintf(format, argptr); |
Bobty | 20:7933076df5af | 122 | va_end(argptr); |
Bobty | 20:7933076df5af | 123 | printf("\r\n"); |
Bobty | 20:7933076df5af | 124 | } |
Bobty | 20:7933076df5af | 125 | } |