无法通过 BOOST_LOG_SEV 打印其他级别

Unable to print other levels via BOOST_LOG_SEV

我正在尝试使用 Boost.Log 但是当我使用时:

BOOST_LOG_SEV(slg, level)//Doesn't matter the level here

即使手动通过 error 我总是得到:

[2021-11-05 12:07:01.305178] [0x00007ffff21589c0] [info]

我希望 [info] 反映我正在通过的严重性级别。

[编辑]

这是代码(完整代码,来自 .hpp):

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>

enum severity_level { trace, debug, info, warning, error, fatal };
inline boost::log::sources::severity_logger<severity_level> slg;

template <class First> std::string extract_args(First first) {
  if constexpr (std::is_integral_v<First>) {
    return std::to_string(first);
  } else {
    return first;
  }
}

template <class First, class... Args> First extract_args(First first, Args... args) {
  first += extract_args(args...);
  return first;
}

template <class First, class... Args> std::string logging_helper(First msg, Args... args) {
  std::string s_msg = extract_args(msg);

  if constexpr (sizeof...(args)) {
    s_msg += extract_args(args...);
  }

  return s_msg;
}

template <class First, class... Args> inline void log(severity_level level, First msg, Args... args) {
  BOOST_LOG_SEV(slg, level) << logging_helper(msg, args...);
}

如果我按照@Andrey Semashev 的建议使用:

boost::log::trivial::severity_level

我收到以下错误:

cannot convert ‘const boost::log::v2_mt_posix::trivial::severity_level’ to ‘boost::log::v2_mt_posix::sources::aux::severity_level<severity_level>::value_type’ {aka ‘severity_level’}

我怀疑,您使用的严重级别类型与 boost::log::trivial::severity_level 不同,同时仍然依赖默认接收器。如果默认接收器无法从日志记录中提取严重性级别,它将使用 boost::log::trivial::severity_level::info 作为默认值。

您应该在所有地方使用 boost::log::trivial::severity_level,或者显式配置接收器,必要时使用过滤器和格式化程序。在后一种情况下,您可以使用自己的严重性级别枚举:

enum severity_level { trace, debug, info, warning, error, fatal };

// Formatting for severity levels
template< typename Char, typename Traits >
inline std::basic_ostream< Char, Traits >& operator<<(
    std::basic_ostream< Char, Traits >& strm, severity_level level)
{
    const char* str;
    switch (level)
    {
    case trace: str = "trace"; break;
    case debug: str = "debug"; break;
    default:
    case info: str = "info"; break;
    case warning: str = "warning"; break;
    case error: str = "error"; break;
    case fatal: str = "fatal"; break;
    }
    strm << str;
    return strm;
}

inline boost::log::sources::severity_logger<severity_level> slg;

BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp",
    boost::log::attributes::utc_clock::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID",
    boost::log::attributes::current_thread_id::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_severity, "Severity", severity_level)

// Logging initialization. To be called early in main().
void init_logging()
{
    auto core = boost::log::core::get();

    // Add commonly used attributes, such as timestamp, thread id, etc.
    core->add_global_attribute("TimeStamp", boost::log::attributes::utc_clock());
    core->add_global_attribute(
        "ThreadID", boost::log::attributes::current_thread_id());

    // Construct the sink
    typedef boost::log::sinks::synchronous_sink<
        boost::log::sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();

    boost::shared_ptr< std::ostream > stream(&std::clog, boost::null_deleter());
    sink->locked_backend()->add_stream(stream);

    // Set formatter
    sink->set_formatter(
        boost::log::expressions::stream << "["
            << boost::log::expressions::format_date_time(a_timestamp, "%Y-%M-%d %H:%M:S.%f")
            << "] [" << a_thread_id
            << "] [" << a_severity
            << "] " << boost::log::expressions::smessage);

    // Add the sink to core
    core->add_sink(sink);
}

// ...

template <class First, class... Args> inline void log(severity_level level, First msg, Args... args) {
  BOOST_LOG_SEV(slg, level) << logging_helper(msg, args...);
}