Boost logo

Boost Users :

Subject: [Boost-users] [Log] fork safety and core::set_logging_enabled(false)
From: Алексей Филиппов (sarum9in_at_[hidden])
Date: 2015-03-11 09:51:34

I am developing a project which executes some non-trivial code
after fork and before exec. This code contains logging statements
"BOOST_LOG_TRIVIAL(..)". I know that Boost.Log is not safe to use
in child created by fork, but it this particular case it is fine to just
logging in child process to make it safe. However I would prefer
to keep these logging statements and disable logging somehow else,
like setting some global flag.

There is a boost::log::core::set_logging_enabled() function
which seems to be the solution to this problem. Documentation says:
"Setting this status to false allows you to completely wipe out
any logging activity, including filtering and generation of attribute
Unfortunately there is no note about fork safety.

The question is:
Is it safe to use it to disable logging in child process,
even if application will try to execute log statements?

Below is simple code sample.

#include <boost/log/trivial.hpp>
#include <sys/wait.h>
#include <unistd.h>
void f(const char *const key)
    BOOST_LOG_TRIVIAL(info) << "Some logic: " << key;
int main()
    BOOST_LOG_TRIVIAL(info) << "Begin of app";
    const pid_t pid = fork();
    if (pid < 0) _exit(127);
    if (pid > 0) {
        // parent
        BOOST_LOG_TRIVIAL(info) << "Child: " << pid;
        f("after-fork main");
    } else {
        // child
        // logging by other functions, is it safe?
        f("after-fork child");
    int stat_loc;
    const pid_t rpid = ::wait(&stat_loc);
    BOOST_LOG_TRIVIAL(info) << "End of app, reaped " << rpid;
Any help would be greatly appreciated,

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at