Search code examples
c++boostglog

glog verbose logging & Boost program options


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


Solution

  • 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("...");
    }