【问题标题】:How to reinitialize Boost Log library on fork?如何在 fork 上重新初始化 Boost Log 库?
【发布时间】:2013-08-31 16:04:28
【问题描述】:

Boost.Log does not support fork()。这有点令人难以置信,但ticket comment 描述了一种解决方法:

[..] 所以现在由用户在 fork 重新初始化库。您可以使用 pthread_atfork 进行此类重新初始化。

因此我的问题是:我该如何在 fork() 之后重新初始化 Boost.Log?

非常感谢代码示例。

【问题讨论】:

  • 您找到解决方案了吗?谢谢。
  • @marathon,不幸的是,不,我没有。

标签: c++ logging boost fork boost-log


【解决方案1】:

您必须处理所有接收器,并在子进程中的pthread_atfork 处理程序 中重新创建它们。 IE。 add_console_logadd_file_log 函数将 boost::shared_ptr 返回到接收器。重置它,然后初始化 再说一遍。

...
boost::shared_ptr<
    sinks::synchronous_sink< sinks::text_ostream_backend >
> console_sink = logging::add_console_log();
...
void fork_child_handler(void)
{
    console_sink = logging::add_console_log();
    return;
}

// in some global setup code of your application
pthread_atfork(NULL /*prepare*/, 
               NULL /* parent */, 
               &fork_child_handler);

小心,fork 可能会留下更多的东西,而不仅仅是破碎 日志接收器。一定要远离多线程和fork (具有讽刺意味的是,pthread 库为 fork 提供了处理程序, 如果有线程,您要避免...)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2012-08-24
    相关资源
    最近更新 更多