【问题标题】:Boost.Log: how to filter by current thread idBoost.Log:如何按当前线程 id 过滤
【发布时间】:2013-09-30 00:38:28
【问题描述】:

我正在使用 Boost.Log,它是 Boost v1.54 的一部分。我有一个接收器,我只想接受来自当前线程的日志消息。我怎样才能做到这一点?

BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", logging::attributes::current_thread_id::value_type)
std::stringstream stream;
logging::add_common_attributes();

boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
sink->locked_backend()->add_stream(stream);
logging::core::get()->add_sink(sink);

boost::thread::id currentThreadId = boost::this_thread::get_id();

// At this line compiler complains about the '==' operator.
sink->set_filter(thread_id == currentThreadId);

没有最后一行一切正常,当我配置接收器格式化程序时,它会输出调用线程 ID。将 thread_id 属性与 currentThreadId 进行比较的正确方法是什么? 我知道我可以使用一些自定义属性来用当前线程 ID 标记消息,然后按该属性过滤它们,但是默认 boost 的 current_thread_id 属性呢?它可以用于这样的目的吗?

【问题讨论】:

    标签: c++ multithreading logging boost filtering


    【解决方案1】:

    好吧,在花了半个晚上挖掘之后,我遇到了(未记录的?)boost::log::aux 命名空间,在那里我找到了boost::log::aux::this_thread::get_id() 函数。它返回一个正确类型的对象,我现在可以将其与thread_id 关键字进行比较。 现在我想知道 boost::log::aux 命名空间是否仅用于提升内部使用?前段时间,我曾经使用 boost 互斥锁/锁的一些内部特性,在下一次库更新后,它们改变了一切,我什至无法针对新版本的库编译我的代码。所以现在我不想重复我过去的错误:)

    【讨论】:

      猜你喜欢
      • 2011-04-22
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 2020-07-30
      • 1970-01-01
      • 2019-07-15
      • 2018-12-10
      相关资源
      最近更新 更多