I'm using glog library, but I have problem with printing more than one message to file.

When I use this code:

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";

I'm getting this log file (you can see that it lacks in all messages except first one):

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

However, when I uncomment LOG(FATAL), it prints all the messages:

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

And I have completely no idea what may cause it. It's simple as that - when I print fatal log message, it (and everything before it) is printed to file. But when there's no fatal message, only the first one is printed.

Did anyone maybe encounter similar problem, or know how to solve it?

有帮助吗?

解决方案

As any asynchronous logger, it will flush only for priority messages, you must call google::LogMessage::Flush() to write all messages to the output.

其他提示

FLAGS_logbufsecs = 0; solve the problem for me. But it may have some performance issues. https://code.google.com/p/google-glog/issues/detail?id=52&can=1&q=flush

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top