禁用glog的“LOG(INFO)”日志记录

时间:2016-05-23 17:38:55

标签: c++ glog

我正在尝试优化我的c ++程序。它使用caffe 执行我的程序时,caffe每隔15分钟输出大约1GB(!)的信息日志。我怀疑这会显着影响效率。但我还没有找到如何关闭注销。在this question,有人建议手动设置FLAGS_v

使用以下代码,我可以按级别禁用VLOG个日志,但LOG(x)日志不受影响。

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

输出:

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)

还有其他flag我不知道吗?我正在考虑评论每个LOG(INFO)行,但我想要一个更优雅的解决方案。 (我更喜欢在命令行标志解决方案上使用c ++解决方案)。

6 个答案:

答案 0 :(得分:7)

这适用于C ++源代码。

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

答案 1 :(得分:5)

您需要设置环境变量

GLOG_minloglevel=2

然后运行您的可执行文件。

您可以找到更多信息here(在本页底部,有一节介绍如何使用宏定义从代码中删除LOG()

答案 2 :(得分:3)

如果要从代码级别关闭日志,可以使用此方法。

Init方法的 src / caffe / net.cpp 中添加c ++代码中的以下行并构建caffe:

fLI::FLAGS_minloglevel=3;

应该添加此行的函数的部分视图:

   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();

      .
      .
      .
      .

根据您的需要设置日志级别。

答案 3 :(得分:2)

环境变量&#34; GLOG_minloglevel&#34;将过滤一些日志,但它们已在您的可执行文件中编译。如果要在编译时禁用它们,请定义宏:

&#34; #define GOOGLE_STRIP_LOG 1&#34;

这是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

答案 4 :(得分:1)

要补充齐彩的答案,
如果对Gflags库有依赖性,则删除“ GLOG _”。

google::SetCommandLineOption("minloglevel", "2");   

每个级别都符合
信息:0
警告:1 ...

答案 5 :(得分:0)

glog 版本

<块引用>

提交 d4e8eb 日期:2021-03-02 09:59:36 +0100

#include <glog/logging.h>

FLAGS_minloglevel = 100;
相关问题