3

I would like to be able to specify the logging severity level from config:

# my config file
LogSeverity = info

How can I do this? Currently I've got something like this in my main function:

// logging settings
boost::log::trivial::severity_level logSeverity;
boost::program_options::options_description loggingSettings                     
    ("Logging settings");                                                       
loggingSettings.add_options()                                                   
("LogSeverity", value<boost::log::trivial::severity_level>(&logSeverity)       
    ->required(),                                                               
        "log level to output");

variables_map vm;
store(parse_config_file(configFilestream, loggingSettings), vm);
notify(vm);

boost::log::core::get()->set_filter(                                         
    boost::log::trivial::severity >= logSeverity);
BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;

The output of this program is:

[2015-05-18 09:58:40.783298] [0x000007f017445078] [info] severity trace

However, I set the severity to info in my config (as above), so why is it being set to trace?

quant
  • 21,507
  • 32
  • 115
  • 211
  • What do you mean it's not "being recognised"? – Lightness Races in Orbit May 18 '15 at 00:10
  • @LightnessRacesinOrbit I set it to `info` in the config, but it's coming up as `trace` in the output. I changed the wording in the question to make this clearer. – quant May 18 '15 at 00:12
  • @quant What's up? Now you've edited your question and added what's in my answer, any comment? Sill not working? – doqtor May 18 '15 at 23:00
  • @doqtor I already had this in my code, sorry I didn't read your answer - just read it. The problem was not that I wasn't storing it, it was to do with the fact that a string can't be cast to the logging severity type. Will add my own answer. – quant May 18 '15 at 23:02

2 Answers2

7

Fully working example:

default.conf:

# my config file
LogSeverity = info

main.cpp:

#include <string>
#include <fstream>

#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/program_options.hpp>

int main()
{
    // logging settings
    boost::log::trivial::severity_level logSeverity;
    boost::program_options::options_description loggingSettings
    ("Logging settings");
    loggingSettings.add_options()
    ("LogSeverity", boost::program_options::value<boost::log::trivial::severity_level>(&logSeverity)
    ->required(),
    "log level to output");

    std::ifstream conf_file("./default.conf");
    if (!conf_file)
        return 1;

    boost::program_options::variables_map variables_map;
    boost::program_options::store(boost::program_options::parse_config_file(conf_file, loggingSettings), variables_map);
    boost::program_options::notify(variables_map);

    boost::log::core::get()->set_filter(
    boost::log::trivial::severity >= logSeverity);
    BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;

    return 0;
}

output:

[2015-05-19 01:22:57.666571] [0xc000027d] [info]    severity info
doqtor
  • 8,414
  • 2
  • 20
  • 36
0

The problem seems to have been that you can't directly assign to the boost::log::trivia::severity type from program_options. To get this to work I stored the variable in an std::string and then used the >> operator to feed the value into the severity type:

// logging settings
std::string logSeverityString;
boost::log::trivial::severity_level logSeverity;
boost::program_options::options_description loggingSettings                     
    ("Logging settings");                                                       
loggingSettings.add_options()                                                   
("LogSeverity", value<boost::log::trivial::severity_level>(&logSeverityString)       
    ->required(),                                                               
        "log level to output");

variables_map vm;
store(parse_config_file(configFilestream, loggingSettings), vm);
notify(vm);

std::istringstream{logSeverityString} >> logSeverity;
boost::log::core::get()->set_filter(                                         
    boost::log::trivial::severity >= logSeverity);
BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;
quant
  • 21,507
  • 32
  • 115
  • 211
  • I've attached fully working example, it's working for me. logSeverity should be set after `notify` call. If that's not the case then something is wrong. – doqtor May 18 '15 at 23:31
  • @doqtor interesting, that doesn't work for me. What's your boost version? I'm using 1.55. – quant May 18 '15 at 23:33