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

Boost.Log: how to filter by current thread id

本文关键字:ID 过滤 线程 前线 Log 何按当 Boost      更新时间:2023-10-16

我正在使用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属性与当前线程 ID 进行比较的正确方法是什么?我知道我可以使用一些自定义属性来标记具有当前线程 ID 的消息,然后按该属性过滤它们,但是默认 boost 的 current_thread_id 属性呢?它可用于此目的吗?

好吧

,在花了半个晚上四处挖掘之后,我遇到了(无证? boost::log::aux命名空间,我在其中找到了boost::log::aux::this_thread::get_id()函数。它返回一个正确类型的对象,我现在能够将其与 thread_id 关键字进行比较。现在我想知道 boost::log::aux 命名空间是否仅用于内部使用?前段时间,我曾经使用boost互斥锁/锁的一些内部功能,在下一次库更新后,它们更改了所有内容,我什至无法针对新版本的库编译代码。所以现在我不想重蹈覆辙:)