为什么它始终打印信息严重性级别的记录器?

时间:2014-11-12 13:43:23

标签: c++ logging boost

我想了解boost log tutorial,我写了以下代码:

#include <iostream>

#include <boost/log/keywords/severity.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/severity_logger.hpp>

namespace keywords = boost::log::keywords;
namespace src = boost::log::sources;

// We define our own severity levels
enum severity_level
{
    normal = 0,
    notification,
    warning,
    error,
    critical
};

void logging_function()
{
    // The logger implicitly adds a source-specific attribute 'Severity'
    // of type 'severity_level' on construction
    src::severity_logger< severity_level > slg;

    BOOST_LOG_SEV(slg, normal) << "A regular message";
    BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it";
    BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!";
}

void default_severity()
{
    // The default severity can be specified in constructor.
    src::severity_logger< severity_level > error_lg(keywords::severity = error);

    BOOST_LOG(error_lg) << "An error level log record (by default)";

    // The explicitly specified level overrides the default
    BOOST_LOG_SEV(error_lg, warning) << "A warning level log record (overrode the default)";
}

int main(int argc, char **argv) {
    logging_function();
    default_severity();

    std::cout << "Hello, world!" << std::endl;
    return 0;
}

但它总是打印info严重性级别的日志,如下所示:

[2014-11-12 14:38:23.476358] [0xbedaf780] [info]    A regular message
[2014-11-12 14:38:23.476967] [0xbedaf780] [info]    Something bad is going on but I can handle it
[2014-11-12 14:38:23.476978] [0xbedaf780] [info]    Everything crumbles, shoot me now!
[2014-11-12 14:38:23.476991] [0xbedaf780] [info]    An error level log record (by default)
[2014-11-12 14:38:23.477002] [0xbedaf780] [info]    A warning level log record (overrode the default)
Hello, world!

为什么?标准打印是否正常?为什么不打印所需的严重性级别呢?


即使我添加了这个:

std::ostream& operator<<(std::ostream& strm, severity_level level)
{
    static const char* strings[] =
    {
        "NORMAL",
        "NOTIFICATION",
        "WARNING",
        "ERROR",
        "CRITICAL"
    };

    if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
        strm << strings[level];
    else
        strm << static_cast< int >(level);

    return strm;
}

输出保持不变。还有什么我忘了?

0 个答案:

没有答案