scclib
Stable Cloud Computing C++ Library
Public Member Functions | Protected Member Functions | List of all members
scc::util::Logger Class Reference

Thread-safe stream logger. More...

#include <logger.h>

Inheritance diagram for scc::util::Logger:
Inheritance graph
[legend]
Collaboration diagram for scc::util::Logger:
Collaboration graph
[legend]

Public Member Functions

 Logger (unsigned int=256)
 Create a logger with maximum line length (default 256). More...
 
 Logger (const std::shared_ptr< std::ostream > &os, unsigned int ll=256)
 Create a logger with an attached stream.
 
 Logger (const Logger &)
 Copy construct logger, using original streams and settings.
 
Loggeroperator= (const Logger &)
 Copy assign logger, using original streams and settings.
 
 Logger (Logger &&)
 Move construct logger. More...
 
Loggeroperator= (Logger &&)
 Move assign logger. More...
 
void clear ()
 Clear the logger and reset to defaults. More...
 
void add (const std::shared_ptr< std::ostream > &)
 Add a shared stream. More...
 
void remove (const std::shared_ptr< std::ostream > &)
 Remove shared stream.
 
void add_cout ()
 Add std::cout console stream.
 
void remove_cout ()
 Remove std::cout console stream.
 
void add_clog ()
 Add std::clog (buffered error) console stream.
 
void remove_clog ()
 Remove std::clog (buffered error) console stream.
 
void add_cerr ()
 Add std::cerr (unbuffered error) console stream.
 
void remove_cerr ()
 Remove std::cerr (unbuffered error) console stream.
 
void id (const std::string &id="")
 Set the id. More...
 
std::string id () const
 Get the current id string.
 
void id (unsigned int i)
 Set a numeric id.
 
void timestamp (const std::string &ts="")
 Set the timestamp format to emit. More...
 
std::string timestamp () const
 Get the current timestamp format.
 
void timestamp_std (bool utc_on=false)
 Set to a standard timestamp. More...
 
void timestamp_iso (bool utc_on=true)
 Set iso 8601 timestamp. More...
 
void utc (bool on)
 Set utc mode.
 
bool utc () const
 Get utc mode.
 
void enable (bool on)
 Enable or disable this stream logger.
 
bool enable () const
 Is the logger enabled?
 
void max_line (unsigned int)
 Set the maximum line length (default is 256). More...
 
unsigned int max_line () const
 Get the maximum line length.
 
void multiline (unsigned int max=0)
 Set multiline mode. More...
 
unsigned int multiline () const
 Get maximum number of multiline lines. More...
 

Protected Member Functions

void dup (const Logger &b)
 

Detailed Description

Thread-safe stream logger.

Logger classes may be used as generic output streams, and shared streams attached to loggers will not overwrite (clobber) log lines from from other threads.

For example, one thread writing:

log line 1 from thread 1 log line 2 from thread 1

and another thread writing:

log line 1 from thread 2 log line 2 from thread 2

will result in exactly 4 log lines output to all attached streams.

Log lines have the form

[ID] TIMESTAMP <line>. Multiple log lines (beginning with spaces or tabs)

Multiline logs (beginning with space or tab) will be consolidated into a single log line up to a maximum number of lines.

Separate threads should each create their own logger (loggers themselves are not thread safe).

Example from scclib/net/unittest/logger.cc Write multithreaded logs to console and stringstream.

Examples
examples/net/netmain.cc, net/unittest/inet.cc, net/unittest/inet_stream.cc, net/unittest/unix.cc, and util/unittest/logger.cc.

Definition at line 85 of file logger.h.

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( unsigned int  max = 256)

Create a logger with maximum line length (default 256).

The minimum line length does not include the prefix, if any.

Definition at line 338 of file logger.cc.

◆ Logger() [2/2]

Logger::Logger ( Logger &&  b)

Move construct logger.

Original will be reset to defaults.

Definition at line 368 of file logger.cc.

Member Function Documentation

◆ add()

void Logger::add ( const std::shared_ptr< std::ostream > &  os)

Add a shared stream.

The stream should inherit from basic_ios (have a streambuf).

Definition at line 404 of file logger.cc.

◆ clear()

void Logger::clear ( )

Clear the logger and reset to defaults.

Defaults: enabled with no streams attached no id no timestamp multiline logging mode off

Definition at line 397 of file logger.cc.

◆ id()

void Logger::id ( const std::string &  id = "")

Set the id.

Setting to "" means do not emit an id.

Definition at line 425 of file logger.cc.

◆ max_line()

void scc::util::Logger::max_line ( unsigned int  )

Set the maximum line length (default is 256).

Lines that exceed this length will be emitted as separate lines.

◆ multiline() [1/2]

unsigned Logger::multiline ( ) const

Get maximum number of multiline lines.

0 means multiline mode is off.

Definition at line 488 of file logger.cc.

◆ multiline() [2/2]

void scc::util::Logger::multiline ( unsigned int  max = 0)

Set multiline mode.

Sets the maximum number of multilines allowed (including the first line). Setting to 0 disables multline mode.

◆ operator=()

Logger & Logger::operator= ( Logger &&  b)

Move assign logger.

Original will be reset to defaults.

Definition at line 379 of file logger.cc.

◆ timestamp()

void Logger::timestamp ( const std::string &  ts = "")

Set the timestamp format to emit.

Follows std::put_time format.

Setting to "" means do not emit a timestamp.

Definition at line 439 of file logger.cc.

◆ timestamp_iso()

void scc::util::Logger::timestamp_iso ( bool  utc_on = true)
inline

Set iso 8601 timestamp.

Example: 2020-10-01T12:00:00Z

Definition at line 188 of file logger.h.

◆ timestamp_std()

void scc::util::Logger::timestamp_std ( bool  utc_on = false)
inline

Set to a standard timestamp.

Example: Oct 1 2020 12:00:00 UTC

Definition at line 170 of file logger.h.


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