glog verbose logging & Boost program options

人走茶凉 提交于 2019-12-12 03:26:31

问题


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.



来源:https://stackoverflow.com/questions/28345346/glog-verbose-logging-boost-program-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!