Apo*_*kal 4 c++ logging boost boost-log
我对Boost.Log库有点新意,第一印象非常好,但有一件事已经耗费了很多时间,我无法解决.我想让Boost.Log立即将每条消息写入日志文件.我知道其他问题(I,II,III),但是他们没有帮助.考虑来自boost docs的这个例子,下一个代码是相同的,除了我设置auto_flush为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;
}
Run Code Online (Sandbox Code Playgroud)
在调试时,sample.log在执行第一个命令(// 1)后会创建一个空,但是在执行BOOST_LOG之后,日志文件保持为空,只有在return语句之后Hello world!才会写入日志文件.
感谢帮助!
我做了一些研究.考虑下一个更改main功能:
int main(int, char*[])
{
init();
src::logger lg;
BOOST_LOG(lg) << "Hello world #1!";
BOOST_LOG(lg) << "Hello world #2!";
std::cin.get();
BOOST_LOG(lg) << "Hello world #3!";
BOOST_LOG(lg) << "Hello world #4!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此std::cin.get()充当暂停,在正常模式下启动应用程序(无需调试,从VS2008按Ctrl + F5,或只是*.exe从Debug文件夹执行),当您到达输入部分(std::cin.get())时,只需转到任务管理器并终止该过程.取决于auto_flush结果的价值是下一个:
auto_flush(false) - 日志文件为空!auto_flush(true) - 日志文件将包含前两个记录 std::cin.get()改变std::cin.get()以throw 1使总前两条记录被写入日志文件中,关于如果auto_flush设置true或false在这两个Release和Debug构建.
因此,结论是auto_flush工作正常,直接从Visual Studio调试时,它只是有点奇怪的行为.
| 归档时间: |
|
| 查看次数: |
2939 次 |
| 最近记录: |