如何从config设置boost日志记录严重性级别?

时间:2015-05-18 00:04:28

标签: c++ boost boost-log

我希望能够从config中指定日志记录严重性级别:

# my config file
LogSeverity = info

我该怎么做?目前我的main函数中有这样的东西:

// 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;

该程序的输出是:

  

[2015-05-18 09:58:40.783298] [0x000007f017445078] [info]严重性追踪

但是,我在配置中将严重性设置为info(如上所述),那么为什么将其设置为trace

2 个答案:

答案 0 :(得分:6)

完全正常的例子:

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;
}

输出:

[2015-05-19 01:22:57.666571] [0xc000027d] [info]    severity info

答案 1 :(得分:0)

问题似乎是您无法直接从program_options分配boost::log::trivia::severity类型。为了实现这一点,我将变量存储在std::string中,然后使用>>运算符将值提供给severity类型:

// 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;