Help those suffering in the Horn of Africa

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.

Version History

version 0.2.0

  • renamed from VSLL to VSLogLib
  • added streams
  • usage of a macro makes it possible to remove all logging from the application easily

version 0.1.0

  • Initial Release

Download

To download go to VSL Downloads page.

Header File (vsll.h)

#ifndef __VSLogLib__
#define __VSLogLib__

#ifndef VSL_MODE
#define VSL_DEBUG 1
#define VSL_RELEASE 0
// set this value to VS_RELEASE to disable logging
#define VSL_MODE VSL_DEBUG
#endif

#include <vector>
#include <string>
#include <iostream>
#include <ostream>
#include <fstream>
#include <stdarg.h>

class VSLogLib {

public:

	VSLogLib();
	~VSLogLib();

	/// set an output stream
	void enableStream(std::ostream *outStream);
	/// disable output stream, keep messages in the log
	void disableStream();

	/**  Add a message, printf style
	  * \param format the same as the first parameter of printf
	  * \param ... the remaining params of printf
	*/
	void addMessage(std::string format, ...);

	/// Writes the log to a file
	void dumpToFile(std::string filename);

	/// returns a string with the logs contents
	std::string dumpToString();

	/// clear the log
	void clear();

private:

	/// The log itself
	std::vector<std::string> pLogVector;
	/// just a string to return values
	std::string pRes;
	/// aux string to avoid malloc/dealloc
	char pAux[256];
	/// the output stream
	std::ostream *pOuts;
	/// stream enabled status
	bool pStreamEnabled;

};

// This macro allow a simple usage of any log
// and when undefined it will remove all calls
// from the application
#if VSL_MODE == VSL_DEBUG
#define VSLOG(log, message, ...) \
{\
	(log.addMessage(message, ## __VA_ARGS__));\
};
#else
#define VSLOG(log, message, ...)
#endif

#endif

Source File (vsll.cpp)

#include "vsLogLib.h"

VSLogLib::VSLogLib(): pStreamEnabled(false) {

}

// cleans up
VSLogLib::~VSLogLib() {

	pLogVector.clear();
}

// clears the log
void
VSLogLib::clear() {

	pLogVector.clear();
}

// adds a message, printf style
void
VSLogLib::addMessage(std::string s, ...) {

	va_list args;
	va_start(args,s);
	vsnprintf( pAux, 256, s.c_str(), args );
	//vsprintf(pAux,s.c_str(), args);
	va_end(args);
	if (pStreamEnabled)
		*pOuts << pAux << "\n";
	else
		pLogVector.push_back(pAux);
}

// dumps the log contents to a file
void
VSLogLib::dumpToFile(std::string filename) {

	std::ofstream file;
	file.open(filename.c_str());

	for (unsigned int i = 0; i < pLogVector.size(); ++i) {
		file << pLogVector[i] << "\n";
	}
	file.close();
}

// dumps the log contents to a string
std::string
VSLogLib::dumpToString() {

	pRes = "";

	for (unsigned int i = 0; i < pLogVector.size(); ++i) {

		pRes += pLogVector[i] + "\n";
	}

	return pRes;
}

void
VSLogLib::disableStream() {

	pStreamEnabled = false;
}

void
VSLogLib::enableStream(std::ostream *outStream) {

	// set the output stream
	if (!outStream)
		pOuts = (std::iostream *)&std::cout;
	else
		pOuts = outStream;

	pStreamEnabled = true;
}

4 comments on “VSLogLib – Very Simple Log Library

  1. 4rknova on said:

    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. lars on said:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

83,986 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

© 2013 Lighthouse3d.com Suffusion theme by Sayontan Sinha