RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::Logger Class Reference

A singleton that subscribes to the AlertManager and writes alert events to a per-session log file (and the console) from a background worker thread. More...

#include <Logger.h>

Public Member Functions

void Initialize (const std::string &logDirectory="")
 Opens a new, per-session timestamped log file inside the given directory (created if it doesn't exist, defaulting to ~/rumpi_logs), prunes old logs, and starts the worker thread. More...
 
void Shutdown ()
 Signals shutdown, wakes the worker thread to drain any remaining events, joins it, and closes the log file. More...
 
const std::string & GetLogFilePath () const
 Gets the path the logger was initialized with (empty until Initialize() has run). More...
 
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 not running. More...
 
 Logger (const Logger &)=delete
 
Loggeroperator= (const Logger &)=delete
 

Static Public Member Functions

static LoggerGetInstance ()
 Gets the singleton instance, constructing it thread-safely on first call. More...
 

Protected Attributes

std::string myLogFilePath
 The path of the current session's log file. More...
 
std::ofstream myLogFile
 The open log file stream. More...
 
std::queue< AlertEventmyEventQueue
 Alert events waiting to be written. More...
 
std::mutex myQueueMutex
 Guards the event queue. More...
 
std::condition_variable myQueueCondition
 Signals the worker thread that events are queued (or shutdown). More...
 
std::thread myWorkerThread
 The background thread that drains the queue to the log. More...
 
std::atomic< bool > myIsRunning
 True while the logger is initialized and its worker is running. More...
 

Private Member Functions

 Logger ()
 Builds the logger in the stopped state. More...
 
virtual ~Logger ()
 Destroys the logger, shutting it down first. More...
 
void ProcessQueue ()
 Worker loop: waits for queued events (or shutdown), drains them in one swap, writes each outside the lock, and flushes the log per batch. More...
 
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 console. More...
 
void CleanUpOldLogs (const std::string &directory, const size_t maxFilesToKeep)
 Deletes the oldest session log files in the given directory, keeping at most maxFilesToKeep. More...
 

Detailed Description

A singleton that subscribes to the AlertManager and writes alert events to a per-session log file (and the console) from a background worker thread.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ Logger() [1/2]

RumPi::Logger::Logger ( )
private

Builds the logger in the stopped state.

Private so only GetInstance can construct it.

Author
Eddie O'Hagan
Date
8/13/2026
//Constructed once by Logger::GetInstance().

◆ ~Logger()

RumPi::Logger::~Logger ( )
privatevirtual

Destroys the logger, shutting it down first.

Author
Eddie O'Hagan
Date
8/13/2026
//Destroyed at program exit (singleton).

◆ Logger() [2/2]

RumPi::Logger::Logger ( const Logger )
delete

Member Function Documentation

◆ CleanUpOldLogs()

void RumPi::Logger::CleanUpOldLogs ( const std::string &  directory,
const size_t  maxFilesToKeep 
)
private

Deletes the oldest session log files in the given directory, keeping at most maxFilesToKeep.

Called on startup so ~/rumpi_logs doesn't grow without bound.

Author
Eddie O'Hagan
Date
8/13/2026
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
constexpr size_t RUMPI_MAX_LOG_FILES_TO_KEEP
Definition: Common.h:62
Parameters
[in]directoryThe directory to prune.
[in]maxFilesToKeepThe maximum number of session log files to keep.

◆ GetInstance()

Logger & RumPi::Logger::GetInstance ( )
static

Gets the singleton instance, constructing it thread-safely on first call.

Author
Eddie O'Hagan
Date
8/13/2026
Logger()
Builds the logger in the stopped state.
Definition: Logger.cpp:21
static Logger & GetInstance()
Gets the singleton instance, constructing it thread-safely on first call.
Definition: Logger.cpp:224
Returns
The single Logger instance.

◆ GetLogFilePath()

const std::string & RumPi::Logger::GetLogFilePath ( ) const

Gets the path the logger was initialized with (empty until Initialize() has run).

Handy for a client that wants to reveal or open the log file.

Author
Eddie O'Hagan
Date
8/13/2026
std::string path = Logger::GetInstance().GetLogFilePath();
const std::string & GetLogFilePath() const
Gets the path the logger was initialized with (empty until Initialize() has run).
Definition: Logger.cpp:172
Returns
The current log file path, or empty if not yet initialized.

◆ HandleAlert()

void RumPi::Logger::HandleAlert ( const AlertEvent theAlertEvent)

Queues an alert event for the worker thread to write and wakes it; writes to stderr if the logger is not running.

Author
Eddie O'Hagan
Date
8/13/2026
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
Parameters
[in]theAlertEventThe alert event to queue.

◆ Initialize()

void RumPi::Logger::Initialize ( const std::string &  logDirectory = "")

Opens a new, per-session timestamped log file inside the given directory (created if it doesn't exist, defaulting to ~/rumpi_logs), prunes old logs, and starts the worker thread.

Author
Eddie O'Hagan
Date
8/13/2026
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
Parameters
[in]logDirectoryThe directory to write logs to, or empty to default to ~/rumpi_logs.

◆ operator=()

Logger & RumPi::Logger::operator= ( const Logger )
delete

◆ ProcessQueue()

void RumPi::Logger::ProcessQueue ( )
private

Worker loop: waits for queued events (or shutdown), drains them in one swap, writes each outside the lock, and flushes the log per batch.

Author
Eddie O'Hagan
Date
8/13/2026
//Started on its own thread by Initialize().

◆ Shutdown()

void RumPi::Logger::Shutdown ( )

Signals shutdown, wakes the worker thread to drain any remaining events, joins it, and closes the log file.

Author
Eddie O'Hagan
Date
8/13/2026
void Shutdown()
Signals shutdown, wakes the worker thread to drain any remaining events, joins it,...
Definition: Logger.cpp:187

◆ WriteEvent()

void RumPi::Logger::WriteEvent ( const AlertEvent theAlertEvent)
private

Writes a single alert event as a timestamped line to the log file (if open) and echoes it to the console.

Author
Eddie O'Hagan
Date
8/13/2026
WriteEvent(theAlertEvent);
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
Parameters
[in]theAlertEventThe alert event to write.

Member Data Documentation

◆ myEventQueue

std::queue<AlertEvent> RumPi::Logger::myEventQueue
protected

Alert events waiting to be written.

◆ myIsRunning

std::atomic<bool> RumPi::Logger::myIsRunning
protected

True while the logger is initialized and its worker is running.

◆ myLogFile

std::ofstream RumPi::Logger::myLogFile
protected

The open log file stream.

◆ myLogFilePath

std::string RumPi::Logger::myLogFilePath
protected

The path of the current session's log file.

◆ myQueueCondition

std::condition_variable RumPi::Logger::myQueueCondition
protected

Signals the worker thread that events are queued (or shutdown).

◆ myQueueMutex

std::mutex RumPi::Logger::myQueueMutex
protected

Guards the event queue.

◆ myWorkerThread

std::thread RumPi::Logger::myWorkerThread
protected

The background thread that drains the queue to the log.


The documentation for this class was generated from the following files: