Search code examples
c++caffeglog

strip caffe's logging message from my own c++ program


I am making my own c++ classification program using caffe library. I want to hide all logging messages during caffe's model initialization step.

According to Disable glog's "LOG(INFO)" logging, I could disable most of logs by setting environment variable

GLOG_minloglevel=2

from command line.

But, What I really want is to remove all logs from the executable itself, so the user can not turn on the logs by resetting GLOG_minloglevel value.

I could find a way to strip glog's logging message on compile time from http://rpg.ifi.uzh.ch/docs/glog.html. It says I can remove logs like this:

> #define GOOGLE_STRIP_LOG 1    // this must go before the #include!   
> #include <glog/logging.h>

Since my application uses caffe's c++ library, I needed to rebuild caffe library with adding following option add_definitions(-DGOOGLE_STRIP_LOG=2) to caffe's CMakeLists.txt. The compile was successful, but when I ran my application with new caffe library, it stops with segmentation fault error during model initialization step. I could get a bit more detailed error message by running with gdb like this:

Program received signal SIGSEGV, Segmentation fault. __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:153 153 ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S: No such file or directory

When I roll back to the original caffe library without add_definitions(-DGOOGLE_STRIP_LOG=2) in caffe's CMakeLists.txt, my application runs fine.

Can anyone give me a hint for solving this problem?

Thank you in advance.


Solution

  • Consider that google::LogToStderr() is described as: "Make it so that all log messages go only to stderr." (see glog/logging.h).

    So, whatever it's doing will probably give us a clue how we might disable logging to files. It turns out its implementation is simply:

    SetStderrLogging(0);            // thus everything is "also" logged to stderr
    for ( int i = 0; i < NUM_SEVERITIES; ++i ) {
      SetLogDestination(i, "");     // "" turns off logging to a logfile
    }
    

    So, to disable logging to files, you need only SetLogDestination() to "", for all severities.

    You'll probably also want to disable all logging to stderr (it seems to default to GLOG_ERROR). This can be accomplished with addition of:

    google::SetStderrLogging( google::NUM_SEVERITIES );
    

    BTW, my rationale for doing this is that I want to redirect GLOG messages to a different logging framework already in use by the app. I've found I can accomplish this by additionally calling google::AddLogSink(), with my own sink.