我的程序有不同的“模块”。模块由一些属于一起的类组成。举一些例子,模块名称是 network , file 或 renderer 。如何添加模块信息以提升日志,以便过滤这些模块?类似下面的内容(注意:这种方法不起作用):
配置:
enum modules {
network,
file,
renderer
};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, modules lvl)
{
static const char* const str[] =
{
"network",
"file",
"renderer"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) {
strm << str[lvl];
} else {
strm << static_cast< int >(lvl);
}
return strm;
}
// Formatting this somehow
keywords::format = expr::stream
<< [" << expr::attr< severity_level >("Severity")
<< "] [" << expr::attr< modules >("Modules")
<< "] " << expr::message
用法:
logging::core::get()->set_filter(expr::attr< modules >("Modules") = network | renderer);
// Will be visible
BOOST_LOG_SEV(slg, lvl)<< network << "Network down!";
// Will not be visible since set_filter has been configured to drop all "file" messages
BOOST_LOG_SEV(slg, lvl)<< file << "Hard disk problem!";
这当然行不通。但是设置这个的正确方法是什么。