如何使用 boost.log 以 dec 格式打印进程 ID 和线程 ID
how to print ProcessID and ThreadID in dec-format with boost.log
我在程序中使用 boost.log,默认格式化程序以十六进制格式输出 ProcessID 和 ThreadID,任何人都知道如何以 dec 格式打印它们,谢谢。
这是我代码的 GitHub:https://github.com/owenliang/boost_asio,谢谢。
boost::log::formatter scope_formatter = boost::log::expressions::stream << "[" <<
boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") <<
"] [" << boost::log::expressions::attr<boost::log::attributes::current_process_id::value_type>("ProcessID") <<
"-" << boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID") << "] [" <<
boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity") <<
"] " << boost::log::expressions::format_named_scope("Scope", boost::log::keywords::format = "%c[%F:%l] ",
boost::log::keywords::depth = 1) << boost::log::expressions::smessage;
Boost Log 提供线程和进程 ID 作为类类型。如果要获取它们的整数值,则需要先获取它们的本机 ID,如下所示:
#include <boost/phoenix.hpp>
/* Define place holder attributes */
BOOST_LOG_ATTRIBUTE_KEYWORD(process_id, "ProcessID", attrs::current_process_id::value_type )
BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", attrs::current_thread_id::value_type )
// Get Process native ID
attrs::current_process_id::value_type::native_type get_native_process_id(
logging::value_ref<attrs::current_process_id::value_type,
tag::process_id> const& pid)
{
if (pid)
return pid->native_id();
return 0;
}
// Get Thread native ID
attrs::current_thread_id::value_type::native_type get_native_thread_id(
logging::value_ref<attrs::current_thread_id::value_type,
tag::thread_id> const& tid)
{
if (tid)
return tid->native_id();
return 0;
}
然后在您的 set_formatter(( 中,例如:
sink->set_formatter
(
expr::stream
<< boost::phoenix::bind(&get_native_process_id, process_id.or_none()) << ":"
<< boost::phoenix::bind(&get_native_thread_id, thread_id.or_none()) << ":"
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y%m%d %H:%M:%S")
<< "]:*" << severity << "*:"
<< expr::smessage
);
输出:
39157:140229314553664:[20170710 15:32:15]:*INF*:Log message
相关文章:
- 同一线程 ID 是否可以在同一进程中重复使用?
- 无法获取进程 ID 4 (ntoskrnl.exe) 的可执行路径
- 为什么if(fork()==0){getpid()}和popen()进程返回相同的进程id
- 从 C++ 中的二进制路径(命令行语句)获取进程 ID
- 使用进程 ID 终止进程
- C++ 通过进程 ID 获取进程的名称
- 按名称查找进程 ID 的快速方法
- 如何在 Windows 进程回调中断开连接时获取设备信息(硬件 ID)
- WinEventHook未捕获特定进程id中的事件
- 使用 fork 创建子进程时,父 ID 与父 ID 不同
- 视觉C++从父进程 ID 获取子进程 ID
- 确定进程的输入线程 ID
- 获取 Winlogon.exe 的会话 ID 和进程 ID
- 如何在浏览器子进程主机Impl->启动后获取浏览器子进程的进程ID
- 如何在进程间通信中获取其他终结点的进程 ID
- 父级的父级的进程 ID - Linux
- 在检查进程 ID 时使用 WaitForSingleObject 的好处
- C++:通过进程id、进程句柄和标题名称从进程中获取唯一窗口的窗口句柄的最佳方法
- 如何找到运行进程的物理套接字 ID/编号
- PIN - 获取正在检测的二进制文件的进程 ID