2013-09-22 78 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時丟失刷新是一個非常常見的問題。 –