如何自定义"TimeStamp" Boost.Log的格式 [英] how to customize "TimeStamp" format of Boost.Log

查看:160
本文介绍了如何自定义"TimeStamp" Boost.Log的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取年月日时:分:second.fraction(2位数字),如果我使用%Y-%m-%d%H:%M:%S.%f",我几乎得到了我想要的例外(秒)(秒),在Windows XP上显示的是6位数字,我不知道如何只获得2位数字,知道吗?

I want to get year-month-day hour:minute:second.fraction(2 digits), if I use "%Y-%m-%d %H:%M:%S.%f", I got almost what I want exception for the fraction( last part ) of seconds, it's showing 6 digits on my Windows XP, I don't know how to get 2 digits only, any idea?

推荐答案

Boost.DateTime(Boost.Log所依赖)似乎不支持专门的小数秒格式,因此唯一的方法是编写您自己的自定义属性格式化程序,或者(稍作修改,但效果不太好)稍微修改您的格式化代码.

Boost.DateTime (upon which Boost.Log relies) doesn't seem to support specialized fractional seconds formatting, so the only way to do that would be to write your own custom attribute formatter, or (the easier, but less nice way) to slightly modify your formatting code.

而不是像这样的东西:

backend->set_formatter
(
    fmt::stream <<
      fmt::date_time<boost::posix_time::ptime>
        ("TimeStamp", keywords::format = "%Y-%m-%d %H:%M:%S.%f"));

backend->set_formatter
(
    fmt::stream <<
    fmt::date_time<boost::posix_time::ptime>
        ("TimeStamp", keywords::format = %Y-%m-%d %H:%M:%S.") <<
    (fmt::format("%.2s") % fmt::date_time<boost::posix_time::ptime>("%f"))
);

我还没有亲自测试过,但是我相信它应该可以工作:第一个fmt::date_time()将返回时间戳,没有小数秒,而第二个fmt::date_time()将仅返回小数秒,该时间将减少为fmt::format()两位.

I haven't tested it myself, but I believe it should work: the first fmt::date_time() will return the timestamp without the fractional seconds, while the second fmt::date_time() will return just the fractional seconds, which will be cut to two digits by the fmt::format().

这篇关于如何自定义"TimeStamp" Boost.Log的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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