4

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?

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65

2 Answers2

2

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

Iuri Covalisin
  • 645
  • 4
  • 7
  • Wow, thank you very much for a simple and very quick answer... it solved my problem. By the way, where may I read more about it? I can see only this http://google-glog.googlecode.com/svn/trunk/doc/glog.html Is there anything more than that? – Piotr Chojnacki Sep 22 '13 at 19:11
  • 1
    Unfortunately that's the only doc available as far as I know, missing flush is a very common issue while using buffered I/O. – Iuri Covalisin Sep 23 '13 at 00:29
2

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

meraj
  • 195
  • 2
  • 3
  • 14