Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost log not showing custom TimeStamp

Tags:

c++

logging

boost

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?

like image 340
Some programmer dude Avatar asked Aug 01 '14 08:08

Some programmer dude


1 Answers

You should include file <boost/log/utility/setup/common_attributes.hpp> and call function

logging::add_common_attributes()
like image 143
ForEveR Avatar answered Oct 10 '22 21:10

ForEveR