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
Diff: Logger.cpp
- Revision:
- 20:7933076df5af
- Parent:
- 12:a52996515063
--- a/Logger.cpp Mon Oct 05 14:05:33 2015 +0000 +++ b/Logger.cpp Tue Oct 13 18:35:20 2015 +0000 @@ -3,10 +3,13 @@ #include "Logger.h" -Logger::Logger(const char* eventLogFileName, const char* dataLogFileBase) +Logger::Logger(const char* eventLogFileName, const char* dataLogFileBase, Mutex &sdCardMutex) : + _sdCardMutex(sdCardMutex) { _eventLogFileName = eventLogFileName; _dataLogFileBase = dataLogFileBase; + _logDebugToFile = false; + _logDebugToConsole = false; } void Logger::LogEvent(const char* format, ...) @@ -15,6 +18,11 @@ time_t seconds = time(NULL); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds)); + // Obtain lock to access sd card - if unable then abort the logging process + if (!_sdCardMutex.trylock()) + return; + + // Open file FILE* fp = fopen(_eventLogFileName, "a"); if (fp == NULL) { @@ -30,6 +38,10 @@ fprintf(fp, "\r\n"); fclose(fp); } + + // Release lock on sd card + _sdCardMutex.unlock(); + } // Utility function to log data @@ -40,6 +52,10 @@ time_t seconds = time(NULL); strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Data_%Y%m%d.txt", localtime(&seconds)); + // Obtain lock to access sd card - if unable then abort the logging process + if (!_sdCardMutex.trylock()) + return; + FILE* fp = fopen(fileNameBuf, "a"); if (fp == NULL) { @@ -54,4 +70,56 @@ fprintf(fp, "\r\n"); fclose(fp); } + + // Release lock on sd card + _sdCardMutex.unlock(); } + +// Utility function to log data +void Logger::LogDebug(const char* format, ...) +{ + char fileNameBuf[60]; + strcpy(fileNameBuf, _dataLogFileBase); + time_t seconds = time(NULL); + strftime(fileNameBuf+strlen(fileNameBuf), sizeof(fileNameBuf)-strlen(fileNameBuf), "Dbg_%Y%m%d.txt", localtime(&seconds)); + + // Log to file if enabled + if (_logDebugToFile) + { + // Obtain lock to access sd card - if unable then abort the logging process + if (_sdCardMutex.trylock()) + { + FILE* fp = fopen(fileNameBuf, "a"); + if (fp == NULL) + { + printf ("Data Log ... Filename %s not found\r\n", _eventLogFileName); + } + else + { + char timeBuf[40]; + time_t seconds = time(NULL); + strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d\t%H:%M:%S\t", localtime(&seconds)); + fprintf(fp, timeBuf); + va_list argptr; + va_start(argptr, format); + vfprintf(fp, format, argptr); + va_end(argptr); + fprintf(fp, "\r\n"); + } + fclose(fp); + + // Release lock on sd card + _sdCardMutex.unlock(); + } + } + + // Print to terminal if enabled + if (_logDebugToConsole) + { + va_list argptr; + va_start(argptr, format); + vprintf(format, argptr); + va_end(argptr); + printf("\r\n"); + } +}