gpt4 book ai didi

c++ - 如何从配置中设置 boost 日志记录严重性级别?

转载 作者:搜寻专家 更新时间:2023-10-31 00:12:25 25 4
gpt4 key购买 nike

我希望能够从配置中指定日志严重级别:

# 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] severity trace

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

最佳答案

完整的工作示例:

默认配置文件:

# my config file
LogSeverity = info

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

关于c++ - 如何从配置中设置 boost 日志记录严重性级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com