如何使用boost.log以dec格式打印ProcessID和ThreadID [英] how to print ProcessID and ThreadID in dec-format with boost.log

查看:424
本文介绍了如何使用boost.log以dec格式打印ProcessID和ThreadID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中使用boost.log,默认格式化程序以十六进制格式输出ProcessID和ThreadID,任何人都知道如何以dec格式打印它们,谢谢。

I use boost.log in my program, and the default formatter outputs the ProcessID and ThreadID in hex-format, anyone knows how to print them in dec-format, thanks.

这是我的代码的github: https://github.com/owenliang/boost_asio

this is the github of my code : https://github.com/owenliang/boost_asio, thanks.

  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,如下所示:

Boost Log provides the thread and process IDs as class type. If you want to get their integral value you need to get their native ID first as follows:

#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()中,例如:

Then in your set_formatter(), e.g.:

   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

这篇关于如何使用boost.log以dec格式打印ProcessID和ThreadID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆