I'm working with the Boost log library (version 1.55) and am trying to customize the output format. Unfortunately I can't seem to make a custom format to show the TimeStamp
attribute.
Example program, mostly taken from the examples in the Boost log documentation:
#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";
}
Output from this program is
[] - Hello world
The formatting works, except the TimeStamp
part which should be inside the square brackets.
Am I using the formatting functions wrong, or am I missing something else?
You should include file <boost/log/utility/setup/common_attributes.hpp>
and call function
logging::add_common_attributes()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With