【问题标题】:How to use boost::log::expressions::format_date_time in a custom formatting function?如何在自定义格式化函数中使用 boost::log::expressions::format_date_time?
【发布时间】:2014-06-18 13:57:56
【问题描述】:

要在格式化程序中格式化时间戳,可以简单地编写

sink->set_formatter(expr::stream << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S"));

但是我怎样才能在这样的自定义格式化函数中使用 boost::log::expressions::format_date_time:

void MyFormatter(boost::log::record_view const &rec, boost::log::formatting_ostream &stream)
{
  stream << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S"); // <-- does not work!
  stream << rec[expr::smessage];
}

【问题讨论】:

    标签: c++ boost-log


    【解决方案1】:

    格式化程序 lambda 样式表达式可作为函数调用,因此您可以委托给它:

    auto date_time_formatter = expr::stream << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S");
    date_time_formatter(rec, stream);
    stream << rec[expr::smessage];
    

    【讨论】:

      猜你喜欢
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      相关资源
      最近更新 更多