2016-05-23 531 views
2

我正試圖優化我的C++程序。它使用咖啡。
當執行我的程序時,caffe每15分鐘輸出大約1GB(!)的信息日誌。我懷疑這會顯着影響效率。但我還沒有找到如何關閉登錄。在this question有人建議手動設置FLAGS_v禁用glog的「LOG(INFO)」日誌

通過以下代碼,我可以逐級禁用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++解決方案)。

回答

3

你需要設置環境變量

GLOG_minloglevel=2 

然後運行可執行文件。

你可以找到更多的信息here(在這個頁面的底部有一段關於從你的代碼使用宏定義剝離LOG())。

+0

有沒有辦法從代碼而不是設置環境變量呢?謝謝。 – rkellerm

4

這適用於C++源代碼。

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

環境變量「GLOG_minloglevel」將過濾一些日誌,但它們已被編譯到您的可執行文件中。如果你想在編譯時期間禁用它們,定義一個宏:

「的#define GOOGLE_STRIP_LOG 1」

這是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 
0

中只需添加下面的一行在Init方法在的src/CAFFE/net.cpp C++代碼,並建立CAFFE:

fLI::FLAGS_minloglevel=3; 

的函數的的局部視圖n應在此處添加此行:

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

     . 
     . 
     . 
     . 

根據您的需要設置日誌級別。