boost::log (boost logging):BOOST_LOG_FUNCTION只在主线程中工作

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

本文关键字:boost 线程 工作 FUNCTION logging BOOST log LOG      更新时间:2023-10-16

考虑以下函数:

void thread() 
{ 
    BOOST_LOG_FUNCTION(); 
    while(true) {
        // Create log entry
    }
} 

如果我只是在"main"中调用"thread()",我在"thread()"中创建的日志条目看起来符合预期:

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

但是,如果您在函数中使用"thread()":

 boost::thread t(thread); 

相应的日志条目为空:

[] 

我该如何解决这个问题?

我从提升日志作者那里得到了反馈。要使范围在登录过程中可用,您必须添加"范围"作为属性。它做到了:

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

但是,这仅与指示的当前线程相关。如果使用多个线程,则必须调用:

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