Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging with glog is not working properly

Tags:

c++

glog

i am using glog for logging purpose when i use :

LOG(INFO) << "something";

it works as expected but when i use multiple logs like below it will not log until the program is stopped .when programm stops it will log everything as expected.

LOG(INFO) <<"111111111111111";
LOG(INFO) <<"222222222222222";
LOG(INFO) <<"333333333333333";
LOG(INFO) <<"444444444444444";

But what is confusing here is when i use LOG(WARNING) multiple times it works perfectly , i.e, it will log everything even when the program is running unlike the previous case when everything was logged when program stopped.

LOG(WARNING) <<"111111111111111";
LOG(WARNING) <<"222222222222222";
LOG(WARNING) <<"333333333333333";
LOG(WARNING) <<"444444444444444";

**any help on this behavior is greatly appreciated **

like image 536
kshitij singh Avatar asked Dec 10 '22 18:12

kshitij singh


1 Answers

The Problem is fairly simple. glog by default uses one log file for each severity to prevent two streams opening the same file. If you open the same file in c++ by different streams one of those (the first one to open the file) gets prioritized to write to the file. The other one can only start writing to that file, when the first stream was closed.

You either have to declare different log files for each severity or to have all log messages in one file you could simply write your own little logging library.


It seems that especially the INFO stream needs to be flushed using google::FlushLogFiles(google::INFO). To do that after each info you want to log I would define myself a macro to call the flush function like so:

#define log(severity, msg) LOG(severity) << msg; google::FlushLogFiles(google::severity); 

This ensures that the stream will be flushed and all your messages will appear in the log file

like image 179
muXXmit2X Avatar answered Dec 31 '22 01:12

muXXmit2X