StreamLog 1.0.0
A lightweight C++11 logging library with stream-based API
Loading...
Searching...
No Matches
StreamLog Class Reference

Main logging class with singleton pattern. More...

#include <streamlog.hpp>

Classes

class  LogStatement
 RAII wrapper for building and committing log messages. More...

Public Member Functions

LogStatement getLogStatement (LogLevel level)
 Create a LogStatement for the given log level.
LogStatement operator<< (std::ostream &(*manipulator)(std::ostream &))
 Stream manipulator support (e.g., std::endl).
virtual ~StreamLog ()
 Virtual destructor for inheritance support.
 StreamLog (const StreamLog &)=delete
 No copy constructor.
StreamLogoperator= (const StreamLog &)=delete
 No copy assignment.
 StreamLog (StreamLog &&)=delete
 No move constructor.
StreamLogoperator= (StreamLog &&)=delete
 No move assignment.

Static Public Member Functions

static StreamLoginstance (const std::string &fileName="output.log", bool consoleOutput=false)
 Get the singleton logger instance.

Protected Member Functions

 StreamLog (const std::string &fileName, bool consoleOutput=false)
 Construct logger with file and console settings.
std::string levelToString (const LogLevel &level) const
 Convert LogLevel enum to string.
std::string getColor () const
 Get ANSI color code for current log level.
virtual std::string getTimestamp () const
 Get current timestamp.
virtual std::stringstream buildLog (const std::string &message) const
 Build formatted log message with timestamp and level.
void writeLog (const std::string &message)
 Write formatted message to file and optionally console.
void commitLog (const std::string &message)
 Commit message if it meets threshold.

Protected Attributes

LogLevel m_level
 Current log level being written.
LogLevel m_threshold
 Minimum level to actually write (compile-time).
std::string m_fileName
 Path to log file.
bool m_consoleOutput
 Whether to also write to stderr.

Detailed Description

Main logging class with singleton pattern.

StreamLog provides a thread-safe singleton logger that writes to both file and optionally console. Uses RAII pattern via LogStatement to ensure messages are committed when the statement goes out of scope.

Usage Patterns

Standard Usage (Singleton):

log(INFO) << "Using global singleton";
StreamLog::LogStatement log(LogLevel level)
Global logging function.
@ INFO
Informational messages.
Definition streamlog.hpp:74

Custom Logger (Inheritance): Derive from StreamLog and override virtual methods for custom behavior. Derived classes are instantiated directly, NOT as singletons.

class CustomLogger : public StreamLog {
std::string getTimestamp() const override { return "[NOW]"; }
};
CustomLogger myLogger; // Direct instantiation
myLogger.getLogStatement(INFO) << "Custom formatting";
StreamLog(const StreamLog &)=delete
No copy constructor.
virtual std::string getTimestamp() const
Get current timestamp.
Note
This class uses Meyer's singleton (C++11 thread-safe static local). Copy and move operations are explicitly deleted.
Singleton pattern applies only to base StreamLog class. Derived classes should be instantiated directly.

Constructor & Destructor Documentation

◆ ~StreamLog()

virtual StreamLog::~StreamLog ( )
virtual

Virtual destructor for inheritance support.

◆ StreamLog() [1/3]

StreamLog::StreamLog ( const StreamLog & )
delete

No copy constructor.

◆ StreamLog() [2/3]

StreamLog::StreamLog ( StreamLog && )
delete

No move constructor.

◆ StreamLog() [3/3]

StreamLog::StreamLog ( const std::string & fileName,
bool consoleOutput = false )
explicitprotected

Construct logger with file and console settings.

Parameters
fileNamePath to log file
consoleOutputtrue to also write to stderr
Note
Protected to allow inheritance. Use instance() for normal usage.

Member Function Documentation

◆ buildLog()

virtual std::stringstream StreamLog::buildLog ( const std::string & message) const
protectedvirtual

Build formatted log message with timestamp and level.

Parameters
messageRaw message content
Returns
Formatted log line with timestamp, color, and level
Note
Virtual to allow custom formatting via inheritance

◆ commitLog()

void StreamLog::commitLog ( const std::string & message)
protected

Commit message if it meets threshold.

Parameters
messageMessage to commit

◆ getColor()

std::string StreamLog::getColor ( ) const
protected

Get ANSI color code for current log level.

Returns
Color code string

◆ getLogStatement()

LogStatement StreamLog::getLogStatement ( LogLevel level)

Create a LogStatement for the given log level.

Parameters
levelLog severity level
Returns
LogStatement ready for message streaming

◆ getTimestamp()

virtual std::string StreamLog::getTimestamp ( ) const
protectedvirtual

Get current timestamp.

Returns
Unix timestamp as string (seconds since epoch)
Note
Virtual to allow custom timestamp formatting via inheritance

◆ instance()

StreamLog & StreamLog::instance ( const std::string & fileName = "output.log",
bool consoleOutput = false )
static

Get the singleton logger instance.

Uses Meyer's singleton pattern (thread-safe in C++11+).

Parameters
fileNameLog file path (only used on first call)
consoleOutputEnable console output (only used on first call)
Returns
Reference to singleton StreamLog instance
Note
Subsequent calls ignore fileName and consoleOutput parameters. The instance retains configuration from the first call.
Warning
Do not call with different parameters in the same program.

◆ levelToString()

std::string StreamLog::levelToString ( const LogLevel & level) const
protected

Convert LogLevel enum to string.

Parameters
levelLog level to convert
Returns
String representation ("TRACE", "DEBUG", etc.)

◆ operator<<()

LogStatement StreamLog::operator<< ( std::ostream &(* manipulator )(std::ostream &))

Stream manipulator support (e.g., std::endl).

Parameters
manipulatorStream manipulator function
Returns
LogStatement for chaining

◆ operator=() [1/2]

StreamLog & StreamLog::operator= ( const StreamLog & )
delete

No copy assignment.

◆ operator=() [2/2]

StreamLog & StreamLog::operator= ( StreamLog && )
delete

No move assignment.

◆ writeLog()

void StreamLog::writeLog ( const std::string & message)
protected

Write formatted message to file and optionally console.

Parameters
messageMessage to write

Member Data Documentation

◆ m_consoleOutput

bool StreamLog::m_consoleOutput
protected

Whether to also write to stderr.

◆ m_fileName

std::string StreamLog::m_fileName
protected

Path to log file.

◆ m_level

LogLevel StreamLog::m_level
protected

Current log level being written.

◆ m_threshold

LogLevel StreamLog::m_threshold
protected

Minimum level to actually write (compile-time).


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