6
我在我的C++程序中使用boost日誌,並使用我定義的severity_level枚舉來定製severity_logger<severity_level>
。然後我使用格式字符串"%TimeStamp% [%ThreadID%] %Severity% %Module% - %Message%"
創建我的日誌接收器,但它不顯示嚴重程度,我有%Severity%
,但在該位置只是空白。例如,2013-07-29 10:31 [0xDEADBEEF] my.Module - Hello World
。我需要在格式字符串中做什麼來顯示嚴重性級別?在Boost Log中,如何使用格式字符串格式化自定義severity_level?
這裏是我的代碼的一部分:
#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);
}
問題似乎涉及到http://stackoverflow.com/questions/15853981/boost-log-2-0-empty-severity-level-在-日誌。我有同樣的問題,在格式字符串中提供%嚴重性%,並且severity_logger不起作用。 – SebastianK