Boost日志,无法记录作用域信息(文件名,行)

时间:2018-07-27 10:27:39

标签: logging boost

我尝试通过修改logging :: formatter来记录src的作用域信息(文件名和行)。(我只想修改formatter,因为我想有两个接收器,一个用于控制台,而不会显示范围信息,另外一个用于显示范围信息的文件)

,我通过以下代码对其进行配置,它符合要求。 但范围未在控制台上显示

namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
enum SeverityLevel
{
trace_c = 0,
debug_c,
info_c,
warn_c,
error_c,
fatal_c
};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, SeverityLevel lvl)
{
    static const char* const str[] =
   {
    "TRACE",
    "DEBUG",
    "INFO",
    "WARN",
    "ERROR",
    "FATAL"
   };
   if (static_cast<std::size_t>(lvl) < (sizeof(str) / sizeof(*str)))
      strm << str[lvl];
   else
      strm << static_cast<int>(lvl);
  return strm;
}
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(vdsu_logger, src::severity_logger_mt<SeverityLevel>);
BOOST_LOG_ATTRIBUTE_KEYWORD(log_severity, "Severity", SeverityLevel)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_uptime, "Uptime", attrs::timer::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_scope, "Scope", attrs::named_scope::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_threadId, "ThreadID", attrs::current_thread_id::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_processId, "ProcessID ", attrs::current_process_id::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type)
// BOOST_LOG_ATTRIBUTE_KEYWORD(log_scope, "Scope", attrs::named_scope_list::value_type)
typedef src::severity_logger_mt<SeverityLevel> vdsu_logger_mt;
void g_InitLog(SeverityLevel console_level)
{
    logging::formatter formatter_console =
        expr::stream
        << "[" << expr::format_date_time(log_timestamp, "%H:%M:%S")<< "]" 
        << "[" << expr::format_named_scope("Scope",    boost::log::keywords::format =  "%n (%f : ") <<"]"
        << "[" << log_severity << "]:" << expr::message;
    logging::add_common_attributes();
    auto console_sink = logging::add_console_log();

    console_sink->set_formatter(formatter_console);
    console_sink->set_filter(log_severity >= console_level);

    logging::core::get()->add_global_attribute("Scope", attrs::named_scope());

    logging::core::get()->add_sink(console_sink);
};  

主要:

g_InitLog(trace_c);
vdsu_logger_mt& vlg = vdsu_logger::get();
BOOST_LOG_SCOPED_LOGGER_ATTR(vlg, "RunTime", attrs::timer());
BOOST_LOG_SEV(vlg, trace_c) << "Hello World! trace";
BOOST_LOG_SEV(vlg, debug_c) << "Hello World! debug";
BOOST_LOG_SEV(vlg, info_c) << "Hello World! info";
BOOST_LOG_SEV(vlg, warn_c) << "Hello World! warn";
BOOST_LOG_SEV(vlg, error_c) << "Hello World! error";

控制台看起来像这样:

[17:50:10][][TRACE]:Hello World! trace
[17:50:10][][DEBUG]:Hello World! debug
[17:50:10][][INFO]:Hello World! info
[17:50:10][][WARN]:Hello World! warn
[17:50:10][][ERROR]:Hello World! error

0 个答案:

没有答案