Using the logger class
The logger class and associated logdestination classes provide a framework for generating log messages from applications. An application can define a set of logdestinations and attach them to an instance of the logger class. Then, when the application calls one of the write() methods of the logger class, the log message is written to each of the logdestinations. For example, an application could simultaneously log to stderr and to a file. Currently stdout, stderr, file and syslog logdestinations are supported. If an application needs to send one set of log messages to one destination and another set to a different destinations, it can create two instances of the logger class and use one for each set of messages.
The following example creates stdout and file logdestinations, adds them to a logger, sets the log level, writes some structured log entries using log headers, writes a simple string, and then removes the file destination so that only stdout receives subsequent messages.
#include <rudiments/logger.h>
#include <rudiments/permissions.h>
int main(int argc, const char **argv) {
// create log destinations
stdoutdestination sod;
filedestination fd;
fd.open("test.log",permissions::parsePermString("rw-rw-r--"));
// create a logger and add both destinations
logger log;
log.addLogDestination(&sod);
log.addLogDestination(&fd);
// set the logging level
log.setLogLevel(1);
// get a log header
char *header=logger::getLogHeader("myprogram");
// write some structured log entries
log.start(1,header,0,"starting up");
log.write(1,header,1,"initializing: %s","done");
log.write(1,header,1,"connecting to: %s:%d","localhost",9000);
log.end(1,header,0);
delete[] header;
// write a simple string to the log
log.write("simple log message\n");
// remove a log destination
log.removeLogDestination(&fd);
fd.close();
// this will only go to stdout now
header=logger::getLogHeader("myprogram");
log.write(1,header,0,"stdout only message");
delete[] header;
}