Search code examples
c++multithreadingboostboost-logboost-logging

boost::log (boost logging): BOOST_LOG_FUNCTION only works in main thread


Consider the following function:

void thread() 
{ 
    BOOST_LOG_FUNCTION(); 

    while(true) {
        // Create log entry
    }
} 

If I just call "thread()" within "main" my log entries created in "thread()" look as expected:

 [void __cdecl thread(void) (c:\...\maintest.cpp:16)] 

However if you use "thread()" within a function:

 boost::thread t(thread); 

the appropriate log-entry is empty:

[] 

How can i fix that?


Solution

  • I got feedback from the boost log author. To make the scope available within the loggin process you have to add "Scope" as an attribute. It did this:

    logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());
    

    However, this is only related to the current thread as indicated. If you are using multiple threads you have to call:

    logging::core::get()->add_global_attribute("Scope", attrs::named_scope());