我正在使用自定义boost::log格式化程序对输出日志消息进行颜色编码,但我找不到将TimeStamp和ThreadID属性添加到日志的正确方法。当我使用文件日志时,我只是写keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%]: %Message%"
为logging::add_file_log
参数。我想在以下自定义格式化程序中产生类似的效果:
void coloring_formatter(const logging::record_view& record,
logging::formatting_ostream& stream)
{
auto severity = record[logging::trivial::severity];
assert(severity);
stream << "\e[1m";
switch (severity.get())
{
case logging::trivial::severity_level::trace:
stream << "\e[97m";
break;
case logging::trivial::severity_level::debug:
stream << "\e[34m";
break;
case logging::trivial::severity_level::info:
stream << "\e[32m";
break;
case logging::trivial::severity_level::warning:
stream << "\e[93m";
break;
case logging::trivial::severity_level::error:
stream << "\e[91m";
break;
case logging::trivial::severity_level::fatal:
stream << "\e[41m";
break;
}
stream // << output TimeStamp
// << output ThreadID
<< "[" << severity << "] "
<< record[logging::expressions::smessage]
<< "\e[0m";
}