blah
blah is a stupid little C++ logging library. But that's fine. I tend to like my libraries stupid and little. Compare and contrast with log4cxx, for example.
It's really quite customizable, though. You'll like it.
Recent activity
Code
Clone the repository using mercurial:
> hg clone http://bitbucket.org/edd/blah
Or get a zip file of the code.
Quick start
Typical use would be to create a header that uses blah to define your logs. For example:
// mylogs.hpp
#include <blah.hpp>
#define DBG BLAH_TRANSIENT_LOG("dbg")
#define MSG BLAH_PERMANENT_LOG("msg")
Now in your code:
#include "mylogs.hpp"
DBG << "something that might be useful for debugging" << 42 << "etc";
MSG << "Status message, perhaps recorded somewhere for customer support";
As you can see, DBG and MSG can be treated like std::ostreams for the most part. Usually, that's what they are. The only thing you can't do is call their member functions:
DBG.precision(4); // please don't
When BLAH_DISABLE_TRANSIENT_LOGS is defined, DBG is no longer an std::ostream, it's just a black hole whose operator<< calls can be optimized away to nothing.
Note that DBG was defined using BLAH_TRANSIENT_LOG while MSG was defined using BLAH_PERMANENT_LOG. Stuff written to MSG will always be recorded, regardless of whether or not BLAH_DISABLE_TRANSIENT_LOGS is defined.
Perhaps we could change mylogs.hpp to this:
// mylogs.hpp
#if defined(NDEBUG)
#define BLAH_DISABLE_TRANSIENT_LOGS
#endif
#include <blah.hpp>
#define DBG BLAH_TRANSIENT_LOG("dbg")
#define MSG BLAH_PERMANENT_LOG("msg")
Now in release builds (those that define NDEBUG while compiling), any statement that writes to DBG will be removed.
On good compilers such as recent versions of Microsoft Visual C++ and g++, removed
means such statements will be compiled out completely. The assembly code will be the same as if the statement wasn't there in the first place, provided you turn on your compiler's optimiz0r. On lesser compilers, it means that a no-op function will be called.
Sample output
By default, writing to our DBG and MSG logs will write stuff to std::clog with some useful decorations. Consider:
#include <blah.hpp>
#define MSG BLAH_PERMANENT_LOG("msg")
int main()
{
MSG << "Hello, world! " << 42;
MSG << "Toodle-oo, world!";
return 0;
}
The output would be something like:
[27 Mar 2009 20:51:44 | msg]: Hello, world! 42 [27 Mar 2009 20:51:44 | msg]: Toodle-oo, world!
The output filtering and redirection is entirely customisable.
Further reading
Comments
All original content copyright© Edd Dawson.
All source code appearing on this website that was written by Edd Dawson is made available under the terms of the Boost software license version 1.0 unless otherwise stated or implied by the license associated with the work from which the code is derived.
