2013-07-29 12 views
6

Używam logu boost w moim programie C++, a ja mam niestandardowy severity_logger<severity_level> przy użyciu wyliczenia zdefiniowanego przez severity_level, które zdefiniowałem. Następnie tworzę mój zlew log z ciągiem formatu "%TimeStamp% [%ThreadID%] %Severity% %Module% - %Message%", ale nie wyświetla on wagi, gdzie mam %Severity%, ale zamiast tego jest pusty w tej pozycji. Na przykład 2013-07-29 10:31 [0xDEADBEEF] my.Module - Hello World. Co muszę zrobić w swoim ciągu formatów, aby wyświetlał poziom istotności?W Boost Log, w jaki sposób sformatować niestandardowy poziom ważności za pomocą ciągu formatu?

Oto fragment mojego kodu:

#define NUM_SEVERITY_LEVELS 6 
enum severity_level 
{ 
    // These are deliberately the same levels that log4j uses 
    trace = 0, 
    debug = 1, 
    info = 2, 
    warning = 3, 
    error = 4,     
    fatal = 5     
}; 

typedef src::severity_logger<severity_level> logger_t; 

const char* severity_level_str[NUM_SEVERITY_LEVELS] = { 
    "TRACE", 
    "DEBUG", 
    "INFO", 
    "WARNING", 
    "ERROR", 
    "FATAL" 
}; 

template< typename CharT, typename TraitsT > 
std::basic_ostream< CharT, TraitsT >& 
operator<< (
    std::basic_ostream< CharT, TraitsT >& strm, 
    severity_level lvl 
) 
{ 
    const char* str = severity_level_str[lvl]; 
    if (lvl < NUM_SEVERITY_LEVELS && lvl >= 0) 
     strm << str; 
    else 
     strm << static_cast<int>(lvl); 
    return strm; 
} 

#define FORMAT_STRING "%TimeStamp% [%ThreadID%] %Severity% %Module% - %Message%" 

boost::shared_ptr< sinks::synchronous_sink<sinks::text_file_backend> > 
LOG_CREATE_SINK(const std::string& strLogFilename, bool fAutoFlush) 
{ 
    return logging::add_file_log(
    keywords::file_name = strLogFilename, 
    keywords::open_mode = (std::ios_base::app | std::ios_base::out) & ~std::ios_base::in, 
    keywords::auto_flush = fAutoFlush, 
    keywords::format = FORMAT_STRING); 
} 
+0

Pytanie wydaje się być związane z http://stackoverflow.com/questions/15853981/boost-log-2-0-empty-severity-level- in-logs. Mam ten sam problem, pod warunkiem, że%% Severity% w ciągu formatu razem z severity_logger nie działa. – SebastianK

Odpowiedz

0

Należy dodać

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

przed wywołaniem metody LOG_CREATE_SINK. Tak:

int main(int argc, char* argv[]) 
{ 
    boost::log::register_simple_formatter_factory< severity_level, char >("Severity"); 
    LOG_CREATE_SINK("log_file.txt", true); 
    logger_t logger; 
    BOOST_LOG_SEV(logger, trace) 
      << "text message"; 
    return 0; 
} 

Wynik:

[] TRACE - text message 
Powiązane problemy