1

以下を使用して、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 なども試しました...

4

2 に答える 2

1

したがって、答えは最終的に次の追加から得られました。

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 . 他のフィルタリングには他の演算子のオーバーロードが必要になる可能性があると思います . /settings.html#log.extension.settings.adding_support_for_user_defined_types_to_the_filter_parser

于 2014-06-10T09:59:40.607 に答える