Boost日志在使用配置文件时不显示严重性或按严重性过滤
Boost Log not displaying severity or filtering by severity when using config file
我一直在尝试使用:
让Boost日志库与配置文件一起工作boost::log::init_from_stream();
方法。我使用:
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
方法来注册严重性,但这似乎没有做任何事情。当我运行代码时,得到以下输出:
1。[]常规消息
2。[]警告严重性消息
3。[]错误严重性消息
。这种严肃性是缺失的。当我添加这行代码时:
boost::log::register_simple_formatter_factory< severity_level, char >("Severity");
它按预期工作,即它像上面一样记录日志,但具有严重性级别。但是,当我尝试在配置文件中按严重性过滤时,它不起作用,并且没有任何内容写入文件,这意味着过滤器不知道"严重性"是什么,因此没有记录与此过滤器匹配。
如何使用init_from_stream方法使Boost日志与严重性和过滤严重性一起工作?
这里是完整的源代码:(改编自Andrey Semashev, http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_filtering.cpp)
enum severity_level
{
normal,
notification,
warning,
error,
critical
};
std::ostream& operator<< (std::ostream& strm, severity_level level)
{
static const char* strings[] =
{
"normal",
"notification",
"warning",
"error",
"critical"
};
if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
strm << strings[level];
else
strm << static_cast< int >(level);
return strm;
}
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
int main(int, char*[])
{
logging::add_common_attributes();
// boost::log::register_simple_formatter_factory< severity_level, char >("Severity"); // when commented out severities are not shown in the log file
std::ifstream configFile_l("config.ini");
boost::log::init_from_stream(configFile_l);
src::severity_logger< severity_level > lg_l;
BOOST_LOG_SEV(lg_l, normal) << "A regular message";
BOOST_LOG_SEV(lg_l, warning) << "A warning severity message";
BOOST_LOG_SEV(lg_l, error) << "An error severity message";
return 0;
}
配置文件是这样的:
[下沉。满)
目标=文本文件文件名= full.log
= " % LineID %格式。(%严重性%)%消息%"
Filter="%Severity%> 3" #也尝试过Filter="%Severity%> info/error等…
你应该加上
boost::log::register_simple_formatter_factory< severity_level, char >("Severity");
在调用add_common_attributes方法之前在main函数中。问候。
所以答案最终来自于添加:
boost::log::register_simple_formatter_factory< severity_level, char >("Severity");
aleksandrm8建议。
然后是错误:
‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE
通过添加以下操作符重载来解决:
template< typename CharT, typename TraitsT >
inline std::basic_istream< CharT, TraitsT >& operator>> (
std::basic_istream< CharT, TraitsT >& strm, severity_level& lvl)
{
int n = normal;
strm >> n;
if (n >= normal && n <= critical)
lvl = static_cast< severity_level >(n);
else
lvl = normal;
return strm;
}
在这里找到:http://sourceforge.net/p/boost-log/discussion/710021/thread/2b0325f8。我认为可能其他操作符重载可能需要其他过滤,信息可以在这里找到:http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/extension/settings.html#log.extension.settings.adding_support_for_user_defined_types_to_the_filter_parser
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- Boost日志在使用配置文件时不显示严重性或按严重性过滤