我正在使用 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
部分之外,格式有效。
我是不是使用了错误的格式化函数,还是我遗漏了其他东西?
最佳答案
您应该包含文件 <boost/log/utility/setup/common_attributes.hpp>
并调用函数
logging::add_common_attributes()
关于c++ - Boost 日志不显示自定义时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25075854/