glog verbose logging & Boost program options

2019-08-29 11:27发布

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...

标签: c++ boost glog
2条回答
聊天终结者
2楼-- · 2019-08-29 11:51

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("...");
}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-29 12:03

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.

查看更多
登录 后发表回答