Search code examples
c++windowsloggingvisual-studio-2012glog

GLOG saves to file only one, first message


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?


Solution

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