Known Issues

Note:
This section assumes you're quite familiar with Boost Logging Lib v2, thus the concepts used here are not explained.

Modifying a manipulator while it's used


Sample code

#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), lvl )

destination::stream out(std::cout);
g_l()->writer().add_destination(out);
...


// Thread 1
out.stream(std::cerr);

// Thread 2
L_ << "whatever";


Resolution

If the above code executes concurrently on Threads 1 and 2, we could get in trouble.

This happens because the thread-safe access is guaranteed :

If we were to allow modifying manipulators, we'd have to:

  1. either allow a way to pause()/resume() the logger(s) that use a given manipulator or
  2. allow thread-safe access to the manipulator objects (meaning each public method would use a mutex, and keep it locked)

In case of the former, pitfalls:

In case of the latter, pitfalls:

As a side-note, if I were a well known company, I'd just say "This behavior is by design".


When does it happen?

I would say seldom. This can happen to you only if you want to modify loggers after you've initialized - thus, while you're logging.

The usual scenario is :

In the above scenario, this issue will never happen. However, if you do run into it, see below.


Solution

The solution is dead-simple. Just delete this manipulator, create one of the same type, modify that one, and then add it to your logger(s). In the original scenario, you'd do this:

#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), lvl )

destination::stream out(std::cout);
g_l()->writer().add_destination(out);
...


// Thread 1
destination::stream out2(std::cerr);
g_l()->writer().del_destination(out);
g_l()->writer().add_destination(out2);

// Thread 2 - all good
L_ << "whatever";


Copyright John Torjo © 2007
Have a question/ suggestion/ comment? Send me feedback