boost::log output to Visual Studio output console - adding extra LF/CR to format
I'm using boost 1.65.1 and I wanted to configure boost::log to output log messages to my Visual Studio 2015 output debug window. The problem is that the messages have unix line endings, so all my messages are in one (long) line in the output window.
I'd hate to have to add the extra CR/LF to every log message since that would muddy up the concurrent outputting of messages to a file that I'm using.
This is how I'm initializing boost::log to output messages VS can pick up:
#include <boost/log/trivial.hpp> #include <boost/log/expressions.hpp> #include <boost/log/sources/severity_logger.hpp> #include <boost/log/sources/record_ostream.hpp> #include <boost/log/utility/setup/file.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include <boost/log/support/date_time.hpp> #include <boost/log/utility/setup/console.hpp> #include <boost/log/sinks/debug_output_backend.hpp> #include <boost/exception/all.hpp> #include <exception> typedef sinks::synchronous_sink< sinks::debug_output_backend > sink_t; ... logging::add_console_log( <stuff> ); logging::add_file_log( <stuff> ); boost::shared_ptr< logging::core > core = logging::core::get(); // Create the sink. The backend requires synchronization in the frontend. boost::shared_ptr< sink_t > sink(new sink_t()); // Set the special filter to the frontend // in order to skip the sink when no debugger is available sink->set_filter(expr::is_debugger_present());
I do have a format specifier in the add_file_log and the add_console_log, but is it possible to add a format specifier specific to debug_output_backend to output the extra CR/LF I need. i.e. just for the output to Visual Studio (the other output methods stay unchanged)?
Note: this sink is 100% different from what boost::log docs call a 'console', which is simply a log message destination of 'standard out'.
Turns out it was an easy fix. I read down a little further in the Sink Backends docs and in the Windows Event Log sink section, they use set_formatter. So Just adding this after the call to set_filter fixed it:
sink->set_formatter ( expr::format("%1%: [%2%] - %3%\r\n") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") % logging::trivial::severity % expr::smessage );
Hope this helps someone.