我對Boost.Log庫有點新,第一印象真的很好,但有一件事情已經花了很多小時,我無法解決它。我想讓Boost.Log立即將每條消息寫入日誌文件。我知道其他問題(I,II,III),但他們沒有幫助。考慮這個example從升壓文檔,下一個代碼是不同的,我已經設置相同auto_flush
到true
:Boost.Log刷新後每個日誌語句
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;
}
調試時,一個空sample.log
是第一個命令(// 1)執行後創建的,但執行BOOST_LOG後,只有在return
語句,Hello world!
寫入日誌文件之後,日誌文件纔會保持爲空。
感謝您的幫助!