Using <io/log/stdlog.h>

The following code includes the stdlog header and uses some of the log outputs. If an output is presently not active then nothing will be written to that output (by default all outputs are to STDOUT or STDERR).

#include <io/log/stdlog.h>
#include <iomanip>

using namespace std;
using namespace io;

int main()
{
	logInfo << "Your output is: " << setprecision(20) << (4.0/5.0) << endl;
	
	logProgressLow << "Off by default, won't see anything" << endl;
	logProgressLow.SetOn( true );
	logProgressLow << "Now you should see something" << endl;
	
	return 0;
}

Note that the insertion operator operands will only be evaluated if the logging is turned on. However, functions are naturally called before the insertion operator, so if you have an unavoidable calculation to perform before insertion you can use the if notation.

if( logDebug )
		logDebug << expensiveFunction() << endl;

Refer to the file <include/io/log/stdlog.h> for a list of all the standard logs and descriptions of their purpose. Feel free to create your own set for your own purposes, the actual logic is contained within LogStream.

The Log Planner

Obviously using the LogStream items is not so interesting unless there is an easy way to control them. For this the LogPlan was constructed. It has a standard instance logPlan coming from <stdlog.h>, but it can be used for your custom set of LogStreams as well.

The LogPlan is a maintainer of all the logs you have in your program. Other than simple things like providing enumeration of the items it offers the very useful ParseSettings function.

//turn on only the info log
logPlan.ParseSettings( "none,info=on" );

//turn on all except warning and error
logPlan.ParseSettings( "all,warning=off,error=off" );

//turn on up to level 2
logPlan.ParseSettings( "level=2" );

The ParseSettings function accepts a string so that it can be directly passed from a command-line argument. This allows you to give all your programs the same log controlling syntax.

The names and levels for the standard logPlan are set in <stdlog.cc>.