在boost::log
进入官方 boost 库之前(从 1.54 开始),我使用的是sourceforge 版本,代码如下:
boost::log::formatters::fmt_format<char> simpleFormat(
boost::log::formatters::format("%1% %2%") %
boost::log::formatters::date_time<boost::posix_time::ptime>(
"TimeStamp", boost::log::keywords::format = "%H:%M:%S") %
boost::log::formatters::message());
和后来的:
log_sink->locked_backend()->set_formatter(simpleFormat)
但是,我根本无法弄清楚如何在不boost::log
使用流语法的情况下使用新API表达上述日志记录格式(我想坚持上面的格式化样式语法)。有任何想法吗?
更新:这是我得到的编译器错误(使用 gcc 4.6.3):
> boost/boost/log/expressions/formatters/date_time.hpp: In constructor
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::format_date_time_terminal(const
> boost::log::v2s_mt_posix::attribute_name&, const fallback_policy&,
> const string_type&) [with T = boost::posix_time::ptime,
> FallbackPolicyT = boost::log::v2s_mt_posix::fallback_to_none, CharT =
> char,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::fallback_policy =
> boost::log::v2s_mt_posix::fallback_to_none,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::string_type = std::basic_string<char>]’:
> /boost/boost/log/expressions/formatters/date_time.hpp:229:94:
> instantiated from
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_actor<AttributeValueT,
> boost::log::v2s_mt_posix::fallback_to_none, CharT>
> boost::log::v2s_mt_posix::expressions::format_date_time(const
> boost::log::v2s_mt_posix::attribute_name&, const CharT*) [with
> AttributeValueT = boost::posix_time::ptime, CharT = char]’
> /src/log.cc:169:102: instantiated from here
> /boost/boost/log/expressions/formatters/date_time.hpp:94:98: error:
> incomplete type
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<boost::posix_time::ptime,
> boost::log::v2s_mt_posix::fallback_to_none, char>::formatter_generator
> {aka
> boost::log::v2s_mt_posix::expressions::aux::date_time_formatter_generator_traits<boost::posix_time::ptime,
> char, void>}’ used in nested name specifier compilation terminated due
> to -Wfatal-errors.
更新 2:我必须包括
#include <boost/log/support/date_time.hpp>
为了使它工作。