I am using Boost.Log to log various sets of data to different files. I would like to have the auto_flush feature enabled for some files, but disabled for others (so that a newline is not inserted on each consecutive log statement). I couldn't get this to work in my larger project. So I simplified the problem down to just one file, but it appears that auto_flush is still cannot be disabled. Here is code for a minimal example:
test.hpp:
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/trivial.hpp>
void init();
test.cpp:
#include "test.hpp"
void init() {
// Initialize sink.
typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> text_sink;
// Grab the Boost Log core.
auto coreHandle = boost::log::core::get();
// Add stream 1.
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
sink->locked_backend()->add_stream(
boost::make_shared<std::ofstream>("category1.log"));
sink->locked_backend()->auto_flush(false);
coreHandle->add_sink(sink);
}
main.cpp:
#include <iostream>
#include <string>
#include "test.hpp"
// Main entry point to program.
int main(int numArg, char const * const arguments[]) {
double number1 = 42;
init();
BOOST_LOG_TRIVIAL(info) << "This message should go to category 1 log..." << number1;
BOOST_LOG_TRIVIAL(info) << "This message should also go to category 1 log on the same line..." << number1;
return EXIT_SUCCESS;
}
The output written to category1.log shows that a newline was applied between the two calls to BOOST_LOG_TRIVIAL, even though I explicitly set auto_flush to false in the init() function:
This message should go to category 1 log...42
This message should also go to category 1 log on the same line...42
I also use BOOST_LOG_CHANNEL_SEV to log to multiple files, but setting auto_flush to false still appears to have no effect...
- How do you disable
auto_flush, and have consecutive log statements print to the same line in the log file? - Can a solution be scaled to multiple log files, such that you can enable
auto_flushfor some files, but not others?
First,
auto_flushhas no relation to the trailing newline after each log record. It makes the sink flush its buffers after each log record is written, whether or not it contains a newline. Second,auto_flushcan only be enabled or disabled on per-sink basis. In the particular case oftext_ostream_backendit means either all streams attached to the sink will be flushed, or none of them. Third, the trailing newline is output by the sink backend internally, and this behavior cannot be disabled currently.If you want to flush the log file only after select log records, your best approach would be to use an attribute to indicate when flushing is needed. Then you would have to create your own sink backend that would examine that attribute in the log records it processes and act appropriately. Creating sinks is described here.
Yes, of course. You have to create a sink per each file and set up
auto_flushaccordingly in those sinks.Update 2019-10-17:
There has been development regarding the trailing newline insertion. In Boost 1.71, in text-based sinks there appeared an option to disable automatic trailing newline insertion. See
auto_newline_modeenum andset_auto_newline_modemethods in the sink backends.