2013-09-22 79 views
4

我正在使用glog库,但我有打印多个邮件到文件的问题。GLOG保存到文件只有一个,第一条消息

当我使用此代码:

std::string appPath = TUtil::ExePath() + "logs\\"; 
google::SetLogDestination(google::GLOG_INFO, std::string(appPath + "INFO").c_str()); 
google::SetLogDestination(google::GLOG_ERROR, ""); 
google::SetLogDestination(google::GLOG_FATAL, ""); 
google::SetLogDestination(google::GLOG_WARNING, ""); 

google::InitGoogleLogging(""); 

LOG(INFO) << "Info1"; 
LOG(INFO) << "Info2"; 
LOG(WARNING) << "Warning1"; 
LOG(ERROR) << "ERROR1"; 
//LOG(FATAL) << "FATAL1"; 

我得到这个日志文件(你可以看到它在所有消息缺乏,除了第一个):

Log file created at: 2013/09/22 20:22:03 
Running on machine: XXX 
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg 
I0922 20:22:03.047548 9512 test.cpp:36] Info1 

然而,当我取消注释LOG(FATAL),它打印的所有消息:

Log file created at: 2013/09/22 20:39:52 
Running on machine: XXX 
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg 
I0922 20:39:52.060691 34104 test.cpp:36] Info1 
I0922 20:39:52.063691 34104 test.cpp:37] Info2 
W0922 20:39:52.063691 34104 test.cpp:38] Warning1 
E0922 20:39:52.063691 34104 test.cpp:39] ERROR1 
F0922 20:39:52.066692 34104 test.cpp:40] FATAL1 

而且我已经完全不知道发生什么事导致它。这很简单 - 当我打印fatal日志消息时,它(以及之前的所有内容)都会打印到文件中。但是,如果没有fatal消息,则只打印第一个。

有没有人可能会遇到类似的问题,或知道如何解决它?

回答

2

由于任何异步记录仪,它只会刷新了优先级的消息,则必须调用google::LogMessage::Flush()把所有信息都写入到输出。

+0

哇,非常感谢你一个简单而非常快速的答案... ...它解决了我的问题。顺便说一下,我可以在哪里阅读更多关于它的内容?我只能看到这个http://google-glog.googlecode.com/svn/trunk/doc/glog.html还有更多吗? –

+1

不幸的是,据我所知,这是唯一可用的文档,在使用缓冲I/O时丢失刷新是一个非常常见的问题。 –