以下を使用して、Boost Log ライブラリを構成ファイルで動作させようとしています。
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");
期待どおりに動作します。つまり、上記のようにログに記録されますが、重大度レベルが設定されています。ただし、構成ファイルで重大度によるフィルターを試行しても機能せず、ファイルに何も書き込まれません。これは、フィルターが「重大度」を認識していないため、このフィルターに一致するレコードがないことを意味します。
Boost Log を重大度で動作させ、init_from_stream メソッドを使用して重大度でフィルタリングするにはどうすればよいですか?
完全なソース コードは次のとおりです。
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;
}
構成ファイルは次のようになります。
[シンク.フル]
宛先=テキストファイル
FileName=full.log
Format="%LineID%.[%Severity%] %Message%"
Filter="%Severity% > 3" # Filter = "%Severity% > info/error なども試しました...