2

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'.

pathrider
  • 844
  • 2
  • 12
  • 27
  • I don't know how to fix this within Boost, but at the bottom of it all must be a `fopen` call. Opening the file in "text" mode (add "t" to mode string) solves this issue. – Cris Luengo Jan 13 '18 at 17:33
  • Possible duplicate of [Simultaneous Logging to Console and File using Boost](https://stackoverflow.com/questions/24170577/simultaneous-logging-to-console-and-file-using-boost) – SoronelHaetir Jan 13 '18 at 17:39
  • 1
    Not a duplicate at all. The console is essentially cout. Windows console is a completely different output 'destination'. It's got it's own sink in boost::log, just like cout does. – pathrider Jan 13 '18 at 17:41

1 Answers1

2

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.

pathrider
  • 844
  • 2
  • 12
  • 27
  • for reference, an example of windows debugger logging can be found [here](https://www.boost.org/doc/libs/1_73_0/libs/log/doc/html/log/detailed/sink_backends.html#log.detailed.sink_backends.debugger) – default May 29 '20 at 09:58