5

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>

为了使它工作。

4

2 回答 2

3

那是你要的吗?

http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/tutorial/formatters.html#log.tutorial.formatters.boost_format_style_formatters

您的案例示例

logging::formatter simpleFormat(expr::format("%1% %2%") %
expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S") %
expr::smessage);
于 2013-10-11T12:23:37.427 回答
1

您还可以使用以下代码像这样格式化日志:

namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;

logging::add_file_log
(
    keywords::file_name = "Sample_%N.log",  /*< file name pattern >*/
    keywords::rotation_size = 1 * 1024,     /*< rotate files every 1 KiB >*/
    /*< log record format >*/
    keywords::format =
    (
        expr::stream
        //<< std::hex   //To print the LineID in Hexadecimal format
        << std::setw(8) << std::setfill('0') 
        << expr::attr< unsigned int >("LineID")
        << "\t"
        << expr::format_date_time<boost::posix_time::ptime>("TimeStamp","%H:%M:%S.%f")
        << "\t: <" << logging::trivial::severity
        << "> \t" << expr::smessage
    )
);

输出如下:

00000741    15:50:11.636997 : <>    Logger Starts
00000742    15:50:11.660012 : <>    Thread74
00000743    15:50:11.682028 : <trace>   Trace message new 
00000744    15:50:11.703041 : <debug>   Debug Message new
00000745    15:50:11.725055 : <info>    Info  message
00000746    15:50:11.748088 : <warning>     warning     Warning  message new
00000747    15:50:11.770083 : <error>   error   An error  message new
00000748    15:50:11.792100 : <fatal>   fatal   A fatal  message new
00000749    15:50:11.818117 : <>    Thread74
00000750    15:50:11.841148 : <>    Logger Stops
于 2016-02-10T10:37:52.960 回答