Disable glog's “LOG(INFO)” logging

房东的猫 提交于 2020-01-14 19:04:46

问题


I'm trying to optimize my c++ program. It uses caffe.
When executing my program, caffe outputs around 1GB (!) of info logs every 15 mins. I suspect this impacts efficiency significantly. But I haven't found how to turn logging off. In this question someone suggested setting FLAGS_v manually.

With the following code I can disable VLOG logs by level, but LOG(x) logs are unaffected.

First lines in main():

FLAGS_v = 1; //disables vlog(2), vlog(3), vlog(4)
VLOG(0) << "Verbose 0";
VLOG(1) << "Verbose 1";
VLOG(2) << "Verbose 2";
VLOG(3) << "Verbose 3";
VLOG(4) << "Verbose 4";
LOG(INFO) << "LOG(INFO)";
LOG(WARNING) << "LOG(WARNING)";
LOG(ERROR) << "LOG(ERROR)";

Output:

WARNING: Logging before InitGoogleLogging() is written to STDERR
I0523 19:06:51.484634 14115 main.cpp:381] Verbose 0
I0523 19:06:51.484699 14115 main.cpp:382] Verbose 1
I0523 19:06:51.484705 14115 main.cpp:386] LOG(INFO)
W0523 19:06:51.484710 14115 main.cpp:387] LOG(WARNING)
E0523 19:06:51.484715 14115 main.cpp:388] LOG(ERROR)

Is there another flag I'm unaware of? I'm thinking of commenting every LOG(INFO) line out, but I would like a more elegant solution. (I'd prefer a c++ solution over a command line flag solution).


回答1:


you need to set your environment variable

GLOG_minloglevel=2

then run your executable.

You can find more information here (at the bottom of this page there is a section on stripping LOG()s from your code using a macro definition).




回答2:


This works in C++ source code.

google::InitGoogleLogging("XXX");
google::SetCommandLineOption("GLOG_minloglevel", "2");



回答3:


If you want to turn off log from code level, you can use this.

Just add below line in your c++ code at src/caffe/net.cpp in Init method and build caffe:

fLI::FLAGS_minloglevel=3;

Partial view of the function where this line should be added:

   template <typename Dtype>
    void Net<Dtype>::Init(const NetParameter& in_param) {

      fLI::FLAGS_minloglevel=3;


      // Set phase from the state.
      phase_ = in_param.state().phase();
      // Filter layers based on their include/exclude rules and
      // the current NetState.
      NetParameter filtered_param;
      FilterNet(in_param, &filtered_param);
      LOG(INFO) << "Initializing net from parameters: " << std::endl
                << filtered_param.DebugString();
      // Create a copy of filtered_param with splits added where necessary.
      NetParameter param;
      InsertSplits(filtered_param, &param);
      // Basically, build all the layers and set up their connections.
      name_ = param.name();

      .
      .
      .
      .

Set log level according to your necessity.




回答4:


The environment variable "GLOG_minloglevel" will filter some log but they have been compile in your executable file. If you want to disable them during compiling time, define a macro:

"#define GOOGLE_STRIP_LOG 1"

This is the comment in logging.h:

111 // The global value of GOOGLE_STRIP_LOG. All the messages logged to             
112 // LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.     
113 // If it can be determined at compile time that the message will not be         
114 // printed, the statement will be compiled out.                                 
115 //                                                                              
116 // Example: to strip out all INFO and WARNING messages, use the value           
117 // of 2 below. To make an exception for WARNING messages from a single          
118 // file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including       
119 // base/logging.h                                                               
120 #ifndef GOOGLE_STRIP_LOG                                                                                                                                                                                
121 #define GOOGLE_STRIP_LOG 0                                                      
122 #endif


来源:https://stackoverflow.com/questions/37397262/disable-glogs-loginfo-logging

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!