I'm working on an application that uses an custom, platform-dependent logger. The application defines some printf-style macros:
#define LOG_DEBUG(format, ...) \
logger.log(DEBUG, __FUNCTION__, format, ##__VA_ARGS__)
...
The past few days I've been working on moving the application to use boost.log
. The biggest problem I'm having is trying to retain this macro format so that only the logger internals need to be changed, since boost's logging API is implemented in iostream-style, i.e.
BOOST_LOG(logger) << "something";
- Is there an easy way to provide a macro that takes printf-style args and prints them to the boost logger without having to use string buffers? (I would think that having to format to a string would be a performance impact)
- If not, is there a way to format a string using
va_args
without having to#ifdef
for different platform implementations of formatting functions? (this was the whole point of moving to boost.log in the first place)
Unfortunately, there are no clean implementations without the
#ifdef
statement. I know you want to port your existing logging to boost log as-is. That would be an incorrect way of doing things. printf was designed to be used for C, while boost log was designed for C++. So, my suggestion would be to use it the right way. You can make your logging macros painless than what you've shown in your code sample.Here's my implementation of boost log where instead of having
BOOST_LOG(logger) << "something";
, I have it asroLOG_INFO << "something";
. I believe this example comes from one of boost log's samples.RoLog.h
RoLog.cpp
DISCLAIMER: I'm not claiming this to be a perfect piece of code. It works for me and I'm happy with it.
Let's say you would still like the option of logging the printf style, then consider the following code:
DISCLAIMER: I haven't tested the above code. You might need to tweak it to make it work for you.
Or just use Boost Format to deal with the printf format string. For example,
Creating
logger
and extending this to handle multiple arguments (most probably with variadic template arguments) is left as an exercise for the reader.