How to use boost::log::expressions::format_date_time in a custom formatting function?
Asked Answered
S

1

1

To format the timestamp in a formatter one can simply write

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

But how can I use the boost::log::expressions::format_date_time in a custom formatting function like this:

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];
}
Seedman answered 18/6, 2014 at 13:57 Comment(0)
M
4

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];
Mcneal answered 18/6, 2014 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.