Boost.Log:如何按当前线程ID过滤

时间:2013-09-30 00:38:28

标签: c++ multithreading logging boost filtering

我正在使用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属性呢?它是否可用于此目的?

1 个答案:

答案 0 :(得分:1)

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