Boost日志未显示自定义TimeStamp

时间:2014-08-01 08:20:17

标签: c++ logging boost

我正在使用Boost日志库(版本1.55),我正在尝试自定义输出格式。很遗憾,我似乎无法制作自定义格式来显示TimeStamp属性。

示例程序,主要取自Boost日志文档中的示例:

#include <boost/log/trivial.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/utility/empty_deleter.hpp>

namespace logging = boost::log;
namespace sinks = logging::sinks;
namespace expr = logging::expressions;
namespace attrs = logging::attributes;

int main()
{
    using text_sink = sinks::synchronous_sink<sinks::text_ostream_backend>;
    auto sink = boost::make_shared<text_sink>();

    // Add standard output as destination for the sink
    boost::shared_ptr<std::ostream> stream(&std::cout, boost::empty_deleter());
    sink->locked_backend()->add_stream(stream);

    // Set output format
    sink->set_formatter(
        expr::stream
            << '['
            << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
            << "] - "
            << expr::smessage
        );

    logging::core::get()->add_sink(sink);

    // Test logging
    BOOST_LOG_TRIVIAL(info) << "Hello world";
}

此程序的输出

[] - Hello world

格式化工作正常,但TimeStamp部分应该在方括号内。

我是否使用格式化功能错误,或者我错过了其他内容?

1 个答案:

答案 0 :(得分:16)

您应该包含文件<boost/log/utility/setup/common_attributes.hpp>并调用函数

logging::add_common_attributes()