qua*_*ant 2 c++ boost boost-log
我希望能够从config指定日志记录严重性级别:
# my config file
LogSeverity = info
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?目前我的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;
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
[2015-05-18 09:58:40.783298] [0x000007f017445078] [info]严重性追踪
但是,我info在我的配置中设置了严重性(如上所述),为什么要设置为trace?
完全工作的例子:
default.conf:
# my config file
LogSeverity = info
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
[2015-05-19 01:22:57.666571] [0xc000027d] [info] severity info
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2394 次 |
| 最近记录: |