Help end child hunger

VSLogLib – Very Simple Log Library

 

Logging is one of those features that we keep using when debugging. The Very Simple Log Library provides basic logging facilities such as adding messages to the log, and writing it to a file or a string.

VSLogLib In Action

Using the log is a very simple procedure. Start off by including the log class header file and declaring an instance of the log as

#include "vsLogLib.h"

VSLogLib myLog;

Afterwards we may start adding messages to the log. The addMessage behaves like printf, but redirected to the log. For instance, all the uses below are possible:

myLog.addMessage("This is my first message");
int line;
...
myLog.addMessage("Error in line %d", line);

char fileName[128];
...
myLog.addMessage("Error in line %d in file %s", line, fileName);

To clear the log just write:

myLog.clear();

To output the logged messages to a file:

myLog.dumpToFile("myLogFile.txt");

And finally if we want a string with all the logged messages:

std::string logMessages;
logMessages = myLog.dumpToString();

If instead of having our messages stored in a log, we want to output them directly to a stream we may use the function enableStream. Once called the output is directed to the stream and messages are no longer stored in the log. If the param is NULL then std::cout will be used. To disable the stream call disableStream.

VSLogLib log;
std::ostream *f;
f = new std::ofstream("log.txt");
// this enables stream output
// output from the log will be directed to the log
// writing messages as they arrive
myLog.enableStream(f);
...
// setting the stream to std::cout
myLog.enableStream(NULL);
...
// disable streaming
// from this point onwards messages will
// be stored in the log
myLog.disableStream();
...

One more thing, as we start adding logging to our apps, we’ll soon have a lot of calls to addMessage in the code. Now, we may want to get rid of some of these calls in the final version, and hunting down every call is hard and error prone.

The library provides a macro, which can be used instead of addMessage, that makes it really easy for all these calls to go away when we want.

To use this feature we call VSLOG instead of addMessage, as in the following example:

// calling VSLOG instead of addMessage
// the following line is equivalent to
// myLog.addMessage("test 1,2,3");
VSLOG(myLog, "test 1,2,3");

With this approach, to get rid of all the messages, we just set VSL_MODE to VSL_RELEASE in vsFontLib.h. Setting VSL_MODE to VSL_DEBUG enables logging again.

And that’s it.

  4 Responses to “VSLogLib – Very Simple Log Library”

  1. The following code is not safe.
    >> vsprintf(m,s.c_str(), args);
    It could easily result in a bof.

    You should replace it with:
    vsnprintf( char* buf, size_t count, const char* format, va_list arg );
    which was introduced in the new standard.

  2. after writing the log file you don’t close it?

Leave a Reply to lars Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: