RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
Logger.h
Go to the documentation of this file.
1#pragma once
2#include "Data/AlertEvent.h"
3#include "Common.h"
4
5namespace RumPi
6{
14 class Logger
15 {
16 protected:
17 std::string myLogFilePath;
18 std::ofstream myLogFile;
19 std::queue<AlertEvent> myEventQueue;
20 std::mutex myQueueMutex;
21 std::condition_variable myQueueCondition;
22 std::thread myWorkerThread;
23 std::atomic<bool> myIsRunning;
24
25 private:
36 Logger();
37
48 virtual ~Logger();
49
61 void ProcessQueue();
62
75 void WriteEvent(const AlertEvent& theAlertEvent);
76
91 void CleanUpOldLogs(const std::string& directory, const size_t maxFilesToKeep);
92
93 public:
107 void Initialize(const std::string& logDirectory = "");
108
119 void Shutdown();
120
134 const std::string& GetLogFilePath() const;
135
148 static Logger& GetInstance();
149
163 void HandleAlert(const AlertEvent& theAlertEvent);
164
165 Logger(const Logger&) = delete;
166 Logger& operator=(const Logger&) = delete;
167 };
168} // namespace RumPi
A single alert event: a metric crossing (or recovering from) a threshold, with the reading that trigg...
Definition: AlertEvent.h:13
A singleton that subscribes to the AlertManager and writes alert events to a per-session log file (an...
Definition: Logger.h:15
std::condition_variable myQueueCondition
Signals the worker thread that events are queued (or shutdown).
Definition: Logger.h:21
std::atomic< bool > myIsRunning
True while the logger is initialized and its worker is running.
Definition: Logger.h:23
const std::string & GetLogFilePath() const
Gets the path the logger was initialized with (empty until Initialize() has run).
Definition: Logger.cpp:172
std::mutex myQueueMutex
Guards the event queue.
Definition: Logger.h:20
std::thread myWorkerThread
The background thread that drains the queue to the log.
Definition: Logger.h:22
Logger()
Builds the logger in the stopped state.
Definition: Logger.cpp:21
void CleanUpOldLogs(const std::string &directory, const size_t maxFilesToKeep)
Deletes the oldest session log files in the given directory, keeping at most maxFilesToKeep.
Definition: Logger.cpp:125
Logger & operator=(const Logger &)=delete
std::string myLogFilePath
The path of the current session's log file.
Definition: Logger.h:17
std::ofstream myLogFile
The open log file stream.
Definition: Logger.h:18
void WriteEvent(const AlertEvent &theAlertEvent)
Writes a single alert event as a timestamped line to the log file (if open) and echoes it to the cons...
Definition: Logger.cpp:291
void Initialize(const std::string &logDirectory="")
Opens a new, per-session timestamped log file inside the given directory (created if it doesn't exist...
Definition: Logger.cpp:54
void HandleAlert(const AlertEvent &theAlertEvent)
Queues an alert event for the worker thread to write and wakes it; writes to stderr if the logger is ...
Definition: Logger.cpp:321
virtual ~Logger()
Destroys the logger, shutting it down first.
Definition: Logger.cpp:36
void Shutdown()
Signals shutdown, wakes the worker thread to drain any remaining events, joins it,...
Definition: Logger.cpp:187
Logger(const Logger &)=delete
void ProcessQueue()
Worker loop: waits for queued events (or shutdown), drains them in one swap, writes each outside the ...
Definition: Logger.cpp:243
std::queue< AlertEvent > myEventQueue
Alert events waiting to be written.
Definition: Logger.h:19
static Logger & GetInstance()
Gets the singleton instance, constructing it thread-safely on first call.
Definition: Logger.cpp:224
RumPi is the library's top-level facade.
Definition: AlertManager.h:6