glog verbose logging & Boost program options

2019-08-29 12:09发布

问题:

I am using boost program options in my code and trying to add verbose logging using glog (google logging library).

The problem is that boost captures the command line options and I can not use the --v flag for controlling the verbose logging. Is there a method for setting the minloglevel from the code? I failed locating a function or a macro for doing that programatically...

回答1:

I had the same problem and am managing to set glog flags in my main function as follows:

namespace po = boost::program_options;

int main(int ac, char **av) {
    po::options_description desc("...");
    desc.add_options()
    ("verbosity,v", po::value<int>(), "set verbose logging level, defaults to 0")
    ;

    po::variables_map vm;
    try{
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);
    }
    catch (po::required_option& e){
        ...
    }
    ...
    if (vm.count("verbosity")){
        FLAGS_v = vm["verbosity"].as<int>();
    }
    else{
        FLAGS_v = 0;
    }
    google::InitGoogleLogging("...");
}


回答2:

I found one work around and one answer that should but doesn't work. You can use the environment variable GLOG_v to set the verbosity level

(on linux)GLOG_v=2 ./your_binary This works well but is not ideal

I also found the not very well documented function google::SetVLOGLevel(char*, int) that is exactly what I was looking for, but unfortunately using it throws an exception.



标签: c++ boost glog