Wiki

Clone wiki

streamlog / Home

Streamlog

Streamlog is a simple and lightweight library that maintains an array of user-defined C++ standard output streams. Every time you write to the log, it automatically writes to all of the added streams.

Installation

Source Code

To install the library from source code, clone the git repository and use CMake.

$ clone git@bitbucket.org:nwehr/streamlog.git
$ cd streamlog
$ cmake ./
$ make && sudo make install

MacPorts

$ sudo port install libstreamlog

Usage

Including the Library

The streamlog library can be used one of two ways. You can either link to the shared library (libstreamlog), or you can use the header-only implementation. If you decide to use the header-only implementation, you must define the symbol STREAMLOG_IMPL before including the header file to get the implementation of the streamlog classes and functions.

The following code example is header-only.

Adding Streams

Streamlog lets you write to as many output streams as you like. These could be console streams (e.g. std::cout), file streams (e.g. std::ofstream), or your own custom output streams that inherit from std::ostream.

#define STREAMLOG_IMPL
#include <streamlog/streamlog.h>

int main( int argc, const char* argv[] ) {
    //////////////////////////////////////////////////////////
    // Using Basic Log
    //////////////////////////////////////////////////////////
    streamlog::basic_log myBasicLog; 

    myBasicLog.add_stream( &std::cout ); 
    myBasicLog.add_stream( new std::ofstream( "path/to/mybasiclog.log", std::ios_base::out ), true ); 

    myBasicLog << "Hello, World!" << std::endl; 

    //////////////////////////////////////////////////////////
    // Using Severity Log
    //////////////////////////////////////////////////////////
    streamlog::severity_log mySeverityLog; 

    mySeverityLog.set_severity( 2 ); 

    mySeverityLog.add_stream( &std::cout ); 
    mySeverityLog.add_stream( new std::ofstream( "path/to/myseveritylog.log", std::ios_base::out ), true ); 

    // This message won't get logged, the severity is too low..
    mySeverityLog( 1 ) << "Message with severity level 1" << std::endl; 

    // These message will get logged, the severity is greater than or equal to what we've set
    mySeverityLog( 2 ) << "Message with severity level 2" << std::endl; 
    mySeverityLog( 3 ) << "Message with severity level 3" << std::endl; 

}

Updated