scclib
Stable Cloud Computing C++ Library
|
Thread-safe stream logger. More...
#include <logger.h>
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. | |
Logger & | operator= (const Logger &) |
Copy assign logger, using original streams and settings. | |
Logger (Logger &&) | |
Move construct logger. More... | |
Logger & | operator= (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) |
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.
Logger::Logger | ( | unsigned int | max = 256 | ) |
Logger::Logger | ( | Logger && | b | ) |
void Logger::add | ( | const std::shared_ptr< std::ostream > & | os | ) |
void Logger::clear | ( | ) |
void Logger::id | ( | const std::string & | id = "" | ) |
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.
unsigned Logger::multiline | ( | ) | const |
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.
void Logger::timestamp | ( | const std::string & | ts = "" | ) |
|
inline |
|
inline |