Boost logo

Boost :

Subject: Re: [boost] [log] Can Boost.Log be used in clean-up functions?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2010-06-15 17:00:36


On 06/16/2010 12:24 AM, Andrzej Krzemienski wrote:
> Hi,
> By 'clean-up functions' I mean destructors or automatic objects,
> destructors of static objects, functions registered with std::atexit
> and std::terminate.
> So, what I want to ask is:
> Can I use Boost.Log for logging inside destructors of automatic
> objects? Destructors are exected not to throw by STL containers and
> algorithms, also it is often taught that a destructor throwing during
> stack unwinding ...
> Can the logging throw an exception? I guess it cn, and I guess, it my
> be unsafe to log in the destructor. Am I right?

Yes, the library may throw. But it is possible to set up exception
handlers that will suppress exceptions.

There is one nuance with regard to logging from destructors. Currently,
the library will silently suppress any log records if stack unwind is in
progress. This was done to reduce the probability of unwanted program
termination if the library throws. In v2 the default throwing behavior
of the library will change, so this precaution feature will be removed.

> Can I use Boost.Log in functions that are run after function main
> finishes? I.e. can I safely log in destructors of static objects, or
> functions registered with std::atexit? Similarly, can I log in the
> std::terminate (if I have installed my custom handler)? When are log
> sources or sinks destroyed?

The sinks and the core are not destroyed as long as there are references
to it. All loggers keep a reference to the logging core, so if you keep
a logger alive long enough, you should be able to write logs at
deinitialization stage.

There are limitations, though. For instance, you cannot reliably create
loggers or acquire new references to the logging core, because static
pointers to singletons the library uses may already be destroyed. In
general, you should try to use as little of the library as possible at
that stage.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk