diff options
author | David Crocker <dcrocker@eschertech.com> | 2017-09-21 21:27:40 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2017-09-21 21:29:52 +0300 |
commit | 370060e84b9f30dd5d9edff817baaba154106c7c (patch) | |
tree | aa399d680427189e6a8c62572c304b9b16118406 /src/Logger.cpp | |
parent | 77d6718860bd6ad383d29e0708c600fcba855116 (diff) |
Version 1.20 alpha 2
Removed define of printf=iprintf in build settings
Removed Platform::Time() function, use millis() or millis64() instead
Added event logging to SD card. MessageType is now a bitmap. Implemented
M929.
Enable pullup resistors on endstops 10 and 11
SCARA printers can now use the manual bed levelling assistant
When using the manual bed levelling assitant, don't give an error if the
bed screw corrections were out of range, and always leave the first
screw alone
M552 now supports connection to a specified SSID
M572 now allows multiple D values
Thermocouple type letter in M305 command may now be in lower case
Bug fix: G29 bed probing on a SCARA printer gave spurious "not
reachable" warnings and skipped probe points
Protect against a dud command line containing the letter M being
interpreted as a M0 command
Fix reference to towers in the error message when trying to move a SCARA
printer before homing it
When updating firmware, warn that USB will disconnect
Fix duplicate error messatge when opening a gcode file fails
Diffstat (limited to 'src/Logger.cpp')
-rw-r--r-- | src/Logger.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/Logger.cpp b/src/Logger.cpp new file mode 100644 index 00000000..c944fe0c --- /dev/null +++ b/src/Logger.cpp @@ -0,0 +1,142 @@ +/* + * Logger.cpp + * + * Created on: 17 Sep 2017 + * Author: David + */ + +#include "RepRapFirmware.h" +#include "Logger.h" +#include "OutputMemory.h" +#include "RepRap.h" +#include "Platform.h" + +// Simple lock class that sets a variable true when it is created and makes sure it gets set false when it falls out of scope +class Lock +{ +public: + Lock(bool& pb) : b(pb) { b = true; } + ~Lock() { b = false; } + +private: + bool& b; +}; + +Logger::Logger() : logFile(), dirty(false), inLogger(false) +{ +} + +void Logger::Start(time_t time, const StringRef& filename) +{ + if (!inLogger) + { + Lock loggerLock(inLogger); + FileStore * const f = reprap.GetPlatform().GetFileStore(SYS_DIR, filename.Pointer(), OpenMode::append); + if (f != nullptr) + { + logFile.Set(f); + logFile.Seek(logFile.Length()); + InternalLogMessage(time, "Event logging started\n"); + } + } +} + +void Logger::Stop(time_t time) +{ + if (logFile.IsLive() && !inLogger) + { + Lock loggerLock(inLogger); + InternalLogMessage(time, "Event logging stopped\n"); + logFile.Close(); + } +} + +void Logger::LogMessage(time_t time, const char *message) +{ + if (logFile.IsLive() && !inLogger) + { + Lock loggerLock(inLogger); + InternalLogMessage(time, message); + } +} + +void Logger::LogMessage(time_t time, OutputBuffer *buf) +{ + if (logFile.IsLive() && !inLogger) + { + Lock loggerLock(inLogger); + bool ok = WriteDateTime(time); + if (ok) + { + ok = buf->WriteToFile(logFile); + } + + if (ok) + { + dirty = true; + } + else + { + logFile.Close(); + } + } +} + +// Version of LogMessage for when we already know we want to proceed and we have already set inLogger +void Logger::InternalLogMessage(time_t time, const char *message) +{ + bool ok = WriteDateTime(time); + if (ok) + { + const size_t len = strlen(message); + if (len != 0) + { + ok = logFile.Write(message, len); + } + if (ok && (len == 0 || message[len - 1] != '\n')) + { + ok = logFile.Write('\n'); + } + } + + if (ok) + { + dirty = true; + } + else + { + logFile.Close(); + } +} + +void Logger::Flush() +{ + if (logFile.IsLive() && dirty && !inLogger) + { + Lock loggerLock(inLogger); + logFile.Flush(); + dirty = false; + } +} + +// Write the data and time to the file followed by a space. +// Caller must already have checked and set inLogger. +bool Logger::WriteDateTime(time_t time) +{ + char bufferSpace[30]; + StringRef buf(bufferSpace, ARRAY_SIZE(bufferSpace)); + if (time == 0) + { + const uint32_t timeSincePowerUp = (uint32_t)(millis64()/1000u); + buf.printf("power up + %02u:%02u:%02u ", timeSincePowerUp/3600u, (timeSincePowerUp % 3600u)/60u, timeSincePowerUp % 60u); + } + else + { + const struct tm * const timeInfo = gmtime(&time); + buf.printf("%04u-%02u-%02u %02u:%02u:%02u ", + timeInfo->tm_year + 1900, timeInfo->tm_mon + 1, timeInfo->tm_mday, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec); + } + return logFile.Write(buf.Pointer()); +} + +// End |