1

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

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];
}
4

1 回答 1

3

The formatter lambda-style expression is invocable as a function, so you can just delegate to it:

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];
于 2014-06-18T16:32:42.160 回答