mr-edd.co.uk :: horsing around with the C++ programming language

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

Tom

[29/10/2010 at 09:47:22]

Hi Edd,

Light & nice logger.
But I found a mess in the log when tested with multi thread applications.
Edd, what do you think about adding something what solve this ?
(I dont know what could it be - dedicated writing thread or something else)

Edd

[29/10/2010 at 17:35:57]

Hey Tom,

To have blah work sensibly in the presence of threads, the bottom of the filter stack could be a function that locks a mutex while writing to the outside world. This approach has worked fine for me so far. Your suggestion of a dedicated logging thread would also work just fine, I think.

However, I'd like to avoid having the library do that automatically as it would impose threading/locking on clients that wouldn't otherwise need it. I'd also have to add another dependency for a threading library too, which seems a shame for a tiny stand-alone library.

I guess I could create a very small internal pthread_mutex/CRITICAL_SECTION wrapper and provide a locking filter that isn't installed by default. I'll have a think about it.

But at the very least, I'll add something to the wiki about the issue. Thanks for mentioning it!

Tom

[02/11/2010 at 08:17:46]

>However, I'd like to avoid having the library do that automatically as it would impose >threading/locking on clients that wouldn't otherwise need it. I'd also have to add another >dependency for a threading library too, which seems a shame for a tiny stand-alone library.

of course

>But at the very least, I'll add something to the wiki about the issue.

It would be nice (and perfectly enough) if You find time to put on wiki some working & tested example.

(optional)
(optional)
(required, hint)

Links can be added like [this one -> http://www.mr-edd.co.uk], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.