在boost :: log进入官方的boost库之前(从1.54开始),我使用的是
sourceforge version,代码如下:
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>
为了使它工作.
最佳答案 那是你要的吗?
你的案例
logging::formatter simpleFormat(expr::format("%1% %2%") %
expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S") %
expr::smessage);