如何自定义Boost.Log的“TimeStamp”格式

2024-01-24

我想得到年-月-日时:分:秒.分数(2位数字),如果我使用“%Y-%m-%d %H:%M:%S.%f”,我几乎得到了什么我想要秒的小数部分(最后一部分)例外,它在我的 Windows XP 上显示 6 位数字,我不知道如何只获取 2 位数字,知道吗?


我正在用这个

namespace boost
{
    BOOST_LOG_OPEN_NAMESPACE

    namespace attributes
    {
        template <typename date_time_type>
        void format_time_ms( std::ostringstream& formatter, const date_time_type& date_time)
        {
            auto time = date_time.time_of_day();
            using namespace std;
            formatter
                << setfill( '0') << setw( 2) << time.hours()   << ':'
                << setfill( '0') << setw( 2) << time.minutes() << ':'
                << setfill( '0') << setw( 2) << time.seconds() << ','
                << setfill( '0') << setw( 3) << time.fractional_seconds() / 1000;
        }

        template <typename date_time_type>
        std::string format_time_ms( const date_time_type& date_time)
        {
            std::ostringstream formatter;
            format_time_ms( formatter, date_time);
            auto time = date_time.time_of_day();
            return formatter.str();
        }

        template <typename date_time_type>
        std::string format_date_time_ms( const date_time_type& date_time, const char date_time_sep = ' ')
        {
            using namespace std;
            ostringstream formatter;
            auto date = date_time.date();
            formatter
                << date.year() << '-'
                << setfill( '0') << setw( 2) << int( date.month()) << '-'
                << setfill( '0') << setw( 2) << date.day() << date_time_sep;
            format_time_ms( formatter, date_time);
            return formatter.str();
        }

        template <typename date_time_type, const char date_time_sep = ' '>
        struct date_time_ms_formatter
        {           
            std::string operator () ( const date_time_type& date_time) { return format_date_time_ms( date_time, date_time_sep); }
        };

        struct time_ms_formatter
        {
            template <typename date_time_type>
            std::string operator () ( const date_time_type& date_time) { return format_time_ms( date_time); }
        };

        template <typename time_type>
        struct local_clock_source
        {
            time_type operator () () const
            {
                return date_time::microsec_clock<time_type>::local_time();
            }
        };

        template <typename time_type>
        struct universal_clock_source
        {
            time_type operator () () const
            {
                return date_time::microsec_clock<time_type>::universal_time();
            }
        };

        template <typename time_type, typename clock_source_type, typename formater_type>
        class custom_clock: public attribute
        {
        public:
            class impl: public attribute::impl
            {
            public:
                attribute_value get_value()
                {
                    auto str = formater_type()( clock_source_type()());
                    return make_attribute_value( str);
                }
            };

            custom_clock(): attribute( new impl()) {}

            explicit custom_clock( const cast_source& source): attribute( source.as<impl>()) {}
        };


        typedef custom_clock<boost::posix_time::ptime, local_clock_source<boost::posix_time::ptime>, date_time_ms_formatter<boost::posix_time::ptime, '\t'> >       local_date_time_ms_clock;
        typedef custom_clock<boost::posix_time::ptime, universal_clock_source<boost::posix_time::ptime>, date_time_ms_formatter<boost::posix_time::ptime, '\t'> >   universal_date_time_ms_clock;

        typedef custom_clock<boost::posix_time::ptime, local_clock_source<boost::posix_time::ptime>, time_ms_formatter>     local_time_ms_clock;
        typedef custom_clock<boost::posix_time::ptime, universal_clock_source<boost::posix_time::ptime>, time_ms_formatter> universal_time_ms_clock;
    }

    BOOST_LOG_CLOSE_NAMESPACE // namespace log
}

初始化为

BOOST_LOG_ATTRIBUTE_KEYWORD( dateTimeStamp,     "DateTime",     boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD( timeStamp,         "Time",         boost::posix_time::ptime)

core->add_global_attribute( dateTimeStamp.get_name(),   attrs::local_date_time_ms_clock());
core->add_global_attribute( timeStamp.get_name(),       attrs::local_time_ms_clock());

并用作

expr::stream << expr::attr<std::string>( dateTimeStamp.get_name())
expr::stream << expr::attr<std::string>( timeStamp.get_name())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何自定义Boost.Log的“TimeStamp”格式 的相关文章

随机推荐

  • 如何在 CarPlay 模拟器上测试 iOS 应用程序

    我正在尝试开发一个与 CarPlay 兼容的 iOS 应用程序 根据这篇文章iOS CarPlay API 是公开的吗 如何集成CarPlay https stackoverflow com questions 22372095 is io
  • Android 中缩小(或裁剪)对话框背景

    我有一个高分辨率图像 假设为 1900x1200 我想将其用作对话框的背景 它被创建了 Dialog dialog new Dialog this dialog setContentView R layout dialog 对话框布局
  • 始终在 UWP 中的 FlipView 控件上显示导航箭头

    使用鼠标并将鼠标悬停在通用 Windows 平台 UWP 上时FlipView控制显示上一个 下一个导航按钮 然而 当使用触摸或笔时 它们仍然隐藏 这可能会让用户感到困惑 不期待额外的内容 我希望导航按钮始终可见 我怎样才能做到这一点 我们
  • 将第二个全局管理员添加到 Azure AD B2C?

    如何为 Azure AD B2C 添加第二个全局管理员 我们是否将它们添加为访客帐户并分配该访客 GA 添加普通用户时 它说我们无法添加 dot state fl us 电子邮件 我们应该授权它 还是会搞砸我们的主要租户 Azure AD
  • 防止用户生成的内容破坏布局?

    我有一个包含一些用户生成的内容的网站 我希望能够将布局的标记以及用户生成的内容的标记分开 这样 u g 内容就不会破坏网站布局 用户生成的内容是可信的 因为它来自我网络上的已知用户组 但尽管如此 只允许一小部分 html 标签 p ul o
  • 使用 TypeScript 编写 React 高阶组件

    我正在写一个反应高阶组件 HOC https facebook github io react docs higher order components html使用 TypeScript HOC 应该比包装组件多接受一个 prop 所以我
  • 将变量传递到新页面

    我试图从一个页面传递一个变量 加载另一个页面并输入该信息 像这样的东西 当127 0 0 1 test html ID 1234时 location href 127 0 0 1 newpage html if location href
  • Swift - uiDelegate 必须是 UIViewController 或实现 signIn:presentViewController: & signIn:dismissViewController: 方法

    来自Crashlytics 我们在 Google 登录过程中收到大量崩溃报告 崩溃报告如下 Fatal Exception NSInvalidArgumentException uiDelegate must either be a UIV
  • require_once被忽略

    Windows 上的 php 出现奇怪的问题 我的应用程序加载一个 核心 文件 该文件加载设置文件 注册自动加载 进行初始化等 在核心文件的顶部我有 include once config php 这对于当前目录中的任何内容都适用 如果我包
  • TableViewController 或具有用于解析 API 查询的表视图的视图控制器

    我想显示一个姓名列表和列表下的按钮 但不知道该怎么做 创建一个空白视图控制器 然后将带有表视图单元格和按钮的表视图拖到其中是否正确 该按钮将导航至添加名称功能 或者可以直接使用表视图控制器吗 如果我使用空白视图控制器执行此操作 我必须为拖动
  • 在 MonoDroid 上实现 IJavaObject

    我目前正在尝试在类上实现 ILocationListener 以便接收 GPS 更新 这不是在 Activity 上 而是在普通的 C 类上 作为 ILocationListener 合约的一部分 我需要支持 I JavaObject pu
  • Blackberry BrowserField白色空页问题

    你好 我想在 BrowserField 中显示 html 内容 我使用代码 blove 来执行此操作 但我只看到白色的空白页面 BrowserField demo new BrowserField String res p demo p d
  • 异步和等待 - 控制台、Windows 窗体和 ASP.NET 之间的区别

    我一直在学习异步 等待的使用 我想我理解了底层的概念 然而 大多数关于 async await 的 Channel 9 教程 MSDN 文章和 Stack Overflow 答案都使用基于 GUI 的应用程序 Windows 窗体应用程序
  • JDBC 和 MySql 安装和配置

    我必须使用 JDBC 和 MySql 做一个项目 有人可以指导我尝试安装和运行这些该死的东西吗 我不知道该怎么做以及做什么 我一生中从未使用过数据库 所以我不知道 我需要安装 配置什么等 有人可以提供一些有用的链接供我开始吗 Vogella
  • UIButton的addtarget:触摸后未调用!

    我有以下代码 当我按下 UIButton 时 不会调用任何内容 并且不会崩溃 calloutButton UIView alloc initWithFrame CGRectMake left width2 2 3 5 230 230 UIB
  • 使用 Hibernate 登录并存储记录的用户名

    我正在开发一个简单的项目云 其中注册用户登录后可以下载和上传文件 我正在使用 Eclipse Java EE JSF 和 Hibernate 进行开发 在我的项目中 我可以登录用户 如果数据正确 我会打开用户菜单 但无法弄清楚在哪里存储有关
  • 使用 UIAppearance 更改标签高度

    有没有办法使用 UIAppearance 更改 UINavigationBar 内标签的高度 这是代码和正在发生的情况的图像 以便您可以了解问题所在 UINavigationBar appearance setTitleVerticalPo
  • iPhone - UIView addSubview 顶部间隙

    我有一个加载到 MainWindow xib 中的视图 它只是一个带有 uiimageview 的视图 在整个屏幕 320 X 480 上显示图像 当应用程序加载时 我显示此视图 然后执行 self view addSubview tabb
  • WP7应用程序版本

    Windows Phone 7 应用程序似乎有两个带有版本号的位置 一个位于 AssemblyInfo cs 中 通过AssemblyVersion AssemblyFileVersion属性 另一个是 WMAppManifest xml
  • 如何自定义Boost.Log的“TimeStamp”格式

    我想得到年 月 日时 分 秒 分数 2位数字 如果我使用 Y m d H M S f 我几乎得到了什么我想要秒的小数部分 最后一部分 例外 它在我的 Windows XP 上显示 6 位数字 我不知道如何只获取 2 位数字 知道吗 我正在用