BOOST_LOG_TRIVIAL和boost :: posix_time :: ptime输出格式 [英] BOOST_LOG_TRIVIAL and boost::posix_time::ptime output formatting

查看:1702
本文介绍了BOOST_LOG_TRIVIAL和boost :: posix_time :: ptime输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要为posix_time :: ptime设置自定义构面,并使用BOOST_LOG_TRIVIAL打印。说,将默认构面更改为%Y - %m - %d%H:%M:%S。
我试图灌输日志接收器与新的语言环境,但没有成功。
当应用于std :: cout时,同样的imbueing技巧完美地工作。
可能是什么问题,是否有办法处理它?<​​/ p>

Need to set custom facet for posix_time::ptime which are printed with BOOST_LOG_TRIVIAL. Say, change default facet to "%Y--%m--%d %H:%M:%S". I've tried to imbue log sink with new locale but had no success. The same imbueing trick works perfectly well when applied to std::cout. What could be the problem and is there a way to deal with it?

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <locale>

namespace logging  = boost::log;
namespace pt       = boost::posix_time;

void main(void)
{
    // console sink
    auto sink = logging::add_console_log(std::cout);

    // standart output
    boost::posix_time::ptime t1(boost::posix_time::time_from_string("2014-11-23 23:59:59.117"));
    BOOST_LOG_TRIVIAL(info) << "before " << t1;

    // adding custom facet...
    pt::time_facet *facet = new boost::posix_time::time_facet("%Y--%m--%d %H:%M:%S");
    sink->imbue(std::locale(sink->getloc(), facet));

    // ... but output doesn't change
    BOOST_LOG_TRIVIAL(info) << "after  " << t1;
}


推荐答案

问题是,在sink中不用于日志消息字符串格式化。它不能使用,因为可以有多个汇,选择是不明确的。

The problem is that the locale in the sink is not used for log message string formatting. It can't be used because there can be multiple sinks and the choice is ambiguous. That locale is used for log record formatting, which is performed for every sink individually.

日志消息字符串由为每个日志消息初始化的流组成,因此,您不能以保留用于多个记录的区域设置的方式向其添加自定义区域设置。

The log message string is composed with a stream that is initialized for every log message, so you can't imbue it with a custom locale in a way that keeps the locale used for multiple records.

您可以做的是将您的自定义区域设置为全局区域设置

What you can do is set your custom locale as the global one somewhere in the initialization code of your application, before you use Boost.Log.

void main(void)
{
    pt::time_facet *facet = new boost::posix_time::time_facet("%Y--%m--%d %H:%M:%S");
    std::locale::global(std::locale(std::locale(), facet));

    // console sink
    auto sink = logging::add_console_log(std::cout);

    // ...

    BOOST_LOG_TRIVIAL(info) << "after  " << t1;
}

请注意,这将影响应用程序中的所有格式, 。

Note that this will affect all formatting in your application, for logging purposes and not.

如果不能接受,您可以尝试不同的方法。首先,您可以编写一个流操纵器,它将在格式化日期/时间对象之前向流中添加一个语言环境。要自动使用此操纵器,您可以定义自己的日志宏。

If that is not acceptable you can try different approaches. First, you can write a stream manipulator that will imbue the stream with a locale before formatting date/time objects. To automate the use of this manipulator you can define your own logging macro.

struct locale_manip {};

std::ostream& operator<< (std::ostream& strm, locale_manip)
{
    pt::time_facet *facet = new boost::posix_time::time_facet("%Y--%m--%d %H:%M:%S");
    strm.imbue(std::locale(std::locale(), facet));
    return strm;
}

#define MY_LOG(sev) BOOST_LOG_TRIVIAL(sev) << locale_manip()

void main(void)
{
    // ...

    MY_LOG(info) << "after  " << t1;
}

另一个解决方案是转换您的日志语句,使日期/时间对象添加到日志记录作为属性。然后,您可以使用格式化程序和特定于接收器的区域设置来自定义输出。

Another solution is to transform your logging statements so that the date/time objects are added to log records as attributes. Then you can use the formatters and the sink-specific locale to customize the output.

这篇关于BOOST_LOG_TRIVIAL和boost :: posix_time :: ptime输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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