2014-06-06 50 views
1

時,我一直在試圖讓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"); 

它按預期工作,即它如上記錄但具有嚴重性級別。但是,當我嘗試在配置文件中按嚴重程度進行篩選時,它不起作用,並且沒有任何內容寫入該文件,這意味着篩選器不知道「嚴重性」是什麼,因此沒有記錄與此篩選器匹配。

如何讓Boost Log使用嚴重級別並使用init_from_stream方法過濾嚴重級別?

以下是完整的源代碼:(改編自安德烈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; 
} 

配置文件看起來像這樣:

[Sinks.Full]

目的地= TEXTFILE

FileName = full.log

Format =「%LineID%。[%Severity%]%Message%」

Filter =「%Severity%> 3」#也試過Filter =「%Severity%> info/error etc ...

回答

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。我認爲可能需要其他操作員過載,其他過濾可能需要,可以在這裏找到有關信息: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

2

你應該在之前調用add_common_attributes方法主要功能添加

boost::log::register_simple_formatter_factory< severity_level, char >("Severity"); 

。 此致敬禮。

+0

嘿非常感謝回覆。我試過了,我得到以下錯誤:錯誤:'sizeof'的不完整應用程序'boost :: STATIC_ASSERTION_FAILURE'。你有什麼想法可能意味着什麼。我一直在谷歌搜索,並沒有找到任何解決方案。 – DHinch