Public Member Functions | Private Attributes | List of all members
ecl::LogStream Class Reference

A customised textstream for fast, multithreaded logging to file. More...

#include <log_stream.hpp>

Inheritance diagram for ecl::LogStream:
Inheritance graph
[legend]

Public Member Functions

void disableHeader ()
 Disables header information. More...
 
void disableMode (int mode)
 Disable the given log mode. More...
 
void disableTimeStamp ()
 Disable time stamps. More...
 
void enableHeader ()
 Enable header information. More...
 
void enableMode (int mode, const std::string header="")
 Enable the given mode and associate the specified header. More...
 
void enableTimeStamp ()
 Enable time stamps. More...
 
bool isEnabled ()
 Check to see if any modes are enabled. More...
 
bool isModeEnabled (int mode)
 Check to see if a particular mode is enabled. More...
 
LogStreamlog (int mode)
 Log streaming function. More...
 
 LogStream ()
 Default constructor, underlying device must be manually opened. More...
 
 LogStream (const std::string &file_name, const WriteMode &mode=New)
 Convenience constructor for logstreams. More...
 
virtual ~LogStream ()
 
- Public Member Functions inherited from ecl::TextStream< SharedFile >
virtual ~TextStream ()
 

Private Attributes

std::map< int, std::string > modes
 
TimeStamp timestamp
 
bool write_header
 
bool write_stamp
 

Detailed Description

A customised textstream for fast, multithreaded logging to file.

Together with the LOG and FLUSH macros, this class enables fast, multithreaded logging with multiple modes, customisable headers and timestamps.

Usage:

Multiple modes are most conveniently utilsed via customised enums. For example:

enum LogModes {
};

Note that this is an advantage over alot of other loggers in that it gives you the freedom to define your own error logging levels. Each mode can then be associated inside the log stream with its own customised header.

LogStream log_stream("test.log")
log_stream.enableMode(Warning,"WARNING");
log_stream.enableMode(Error,"ERROR");
log_stream.enableMode(Debug,"DEBUG");

This process can be repeated from multiple threads each with its own instance of the log stream attached to the single file. Using the log stream is then done via the macros LOG and FLUSH

LOG(log_stream, Warning) << "This is a log message from main().\n";
FLUSH(log_stream)

By default this will automatically add header and timestamp information. You can manually disable these if you prefer.

Definition at line 112 of file log_stream.hpp.

Constructor & Destructor Documentation

◆ LogStream() [1/2]

ecl::LogStream::LogStream ( )
inline

Default constructor, underlying device must be manually opened.

This must open the underlying shared file device manually via device().open() as you would do if using a TextStream.

Definition at line 124 of file log_stream.hpp.

◆ LogStream() [2/2]

ecl::LogStream::LogStream ( const std::string &  file_name,
const WriteMode mode = New 
)
inline

Convenience constructor for logstreams.

This constructor enables RAII style construction of the underlying shared file device.

Parameters
file_name: output file name.
mode: mode for writing (New, Append).
Exceptions
StandardException: throws if the connection failed to open.

Definition at line 136 of file log_stream.hpp.

◆ ~LogStream()

virtual ecl::LogStream::~LogStream ( )
inlinevirtual

Definition at line 149 of file log_stream.hpp.

Member Function Documentation

◆ disableHeader()

void ecl::LogStream::disableHeader ( )

Disables header information.

This turns off printing of the header at the beginning of any logging message.

Definition at line 37 of file log_stream.cpp.

◆ disableMode()

void ecl::LogStream::disableMode ( int  mode)

Disable the given log mode.

Disable the given log mode.

Parameters
mode: the log mode.

Definition at line 42 of file log_stream.cpp.

◆ disableTimeStamp()

void ecl::LogStream::disableTimeStamp ( )

Disable time stamps.

This disables timestamps at the beginning of any logging message. The timestamp is in the format seconds.nanoseconds (think unix time).

Definition at line 39 of file log_stream.cpp.

◆ enableHeader()

void ecl::LogStream::enableHeader ( )

Enable header information.

This prints the header at the beginning of any logging message. The header is a string that is specified when calling the enableMode() function.

Definition at line 36 of file log_stream.cpp.

◆ enableMode()

void ecl::LogStream::enableMode ( int  mode,
const std::string  header = "" 
)

Enable the given mode and associate the specified header.

Enable the given log mode and associate it with the specified header string.

Parameters
mode: the log mode.
header: the string to precede the log message if required.

Definition at line 41 of file log_stream.cpp.

◆ enableTimeStamp()

void ecl::LogStream::enableTimeStamp ( )

Enable time stamps.

This prints a timestamp at the beginning of any logging message. The timestamp is in the format seconds.nanoseconds (think unix time).

Definition at line 38 of file log_stream.cpp.

◆ isEnabled()

bool ecl::LogStream::isEnabled ( )

Check to see if any modes are enabled.

Check to see if any modes are enabled.

Returns
bool : true if any modes are enabled, false otherwise.

Definition at line 44 of file log_stream.cpp.

◆ isModeEnabled()

bool ecl::LogStream::isModeEnabled ( int  mode)

Check to see if a particular mode is enabled.

Check to see if the specified mode is enabled.

Returns
bool : true if the modes is enabled, false otherwise.

Definition at line 52 of file log_stream.cpp.

◆ log()

LogStream & ecl::LogStream::log ( int  mode)

Log streaming function.

Do not use this directly, rather it is indirectly utilised by the LOG macro, which should only reach here once isModeEnabled() is checked. It adds a header and timestamp if it is configured to do so and then passes the stream to the program for further additions.

Parameters
mode: log mode that is being logged.
Returns
OutputTextStream : log stream's output streaming parent.

Definition at line 61 of file log_stream.cpp.

Member Data Documentation

◆ modes

std::map<int,std::string> ecl::LogStream::modes
private

Definition at line 227 of file log_stream.hpp.

◆ timestamp

TimeStamp ecl::LogStream::timestamp
private

Definition at line 228 of file log_stream.hpp.

◆ write_header

bool ecl::LogStream::write_header
private

Definition at line 225 of file log_stream.hpp.

◆ write_stamp

bool ecl::LogStream::write_stamp
private

Definition at line 226 of file log_stream.hpp.


The documentation for this class was generated from the following files:
ecl::demos::Debug
@ Debug
Definition: log_streams.cpp:48
ecl::demos::Warning
@ Warning
Definition: log_streams.cpp:46
ecl::demos::LogModes
LogModes
Definition: log_streams.cpp:41
ecl::LogStream::enableMode
void enableMode(int mode, const std::string header="")
Enable the given mode and associate the specified header.
Definition: log_stream.cpp:41
ecl::LogStream::LogStream
LogStream()
Default constructor, underlying device must be manually opened.
Definition: log_stream.hpp:124
ecl::demos::Error
@ Error
Definition: log_streams.cpp:47


ecl_streams
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:48