I'm a bit new to Boost.Log library, first impressions are really good, but one thing is already took a lot of hours and I can't solve it. I want to make Boost.Log to write each message to a log file immediately. I'm aware of other questions (I, II, III), however they didn't help. Consider this example from boost docs, the next code is the same except that I've set auto_flush
to true
:
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
void init()
{
// Construct the sink
typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
// Add a stream to write log to
sink->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("sample.log")); //1
sink->locked_backend()->auto_flush(true);
// Register the sink in the logging core
logging::core::get()->add_sink(sink);
}
int main(int, char*[])
{
init();
src::logger lg;
BOOST_LOG(lg) << "Hello world!";
return 0;
}
While debugging, an empty sample.log
is created after execution of first command (//1), however after executing BOOST_LOG, log file remains empty, only after return
statement, Hello world!
is written to log file.
Thanks for help!