Boost Log 2.0:empty日志中的严重性级别 [英] Boost Log 2.0 : empty Severity level in logs

查看:347
本文介绍了Boost Log 2.0:empty日志中的严重性级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Boost-Log 2.0,它与版本1有一些不同,我很难输出Severity属性。



m使用Boost.Format-style格式化程序:%TimeStamp%[%Uptime%](%LineID%)<%Severity%> ;:%Message%



TimeStamp LineID 消息 common_attributes Uptime 是我使用 attrs :: timer()添加的属性。我认为使用 severity_logger 时会自动添加严重性,但显然它不是,这是我的问题。我得到空的严重性,例如:



2013-Apr-06 19:21:52.408974 [00:00:00.001337] > ;:警告严重性消息



请注意空的<> 。我尝试使用 register_simple_formatter_factory 添加严重性,但后来得到编译器错误:



错误:没有匹配的函数调用'register_simple_formatter_factory(const char [9])'



,我不明白为什么。 / p>

这是我的代码:

  #include< iostream> 


#include< boost / log / common.hpp>
#include< boost / log / core.hpp>
#include< boost / log / trivial.hpp>
#include< boost / log / expressions.hpp>
#include< boost / log / sinks / text_file_backend.hpp>
#include< boost / log / sinks / sync_frontend.hpp>
#include< boost / log / sinks / text_ostream_backend.hpp>
#include< boost / log / utility / setup / file.hpp>
#include< boost / log / utility / setup / common_attributes.hpp>
#include< boost / log / utility / setup / formatter_parser.hpp>
#include< boost / log /源/ severity_logger.hpp>
#include< boost / log /源/ severity_feature.hpp>
#include< boost / log / sources / record_ostream.hpp>

#include< boost / log / attributes.hpp>

using namespace std;

命名空间logging = boost :: log;
namespace expr = boost :: log :: expressions;
namespace attrs = boost :: log :: attributes;
namespace src = boost :: log :: sources;
namespace keywords = boost :: log :: keywords;

枚举severity_level
{
DEBUG,
INFO,
警告,
错误,
CRITICAL
};

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger,src :: severity_logger_mt< severity_level>)

//严重性级别的格式化逻辑
模板< typename CharT,typename TraitsT>
inline std :: basic_ostream< CharT,TraitsT>&运算符<< (
std :: basic_ostream< CharT,TraitsT>& strm,severity_level lvl)
{
static const char * const str [] =
{
DEBUG,
INFO,
WARNING,
ERROR,
CRITICAL
};
if(static_cast< std :: size_t>(lvl)<(sizeof(str)/ sizeof(* str)))
strm< str [lvl];
else
strm<< static_cast< int>(lvl);
return strm;
}

void init(){
// logging :: register_simple_formatter_factory< severity_level>(Severity);
logging :: add_file_log(
keywords :: file_name =blop.log,
keywords :: auto_flush = true,
keywords :: open_mode =(std :: ios: :out | std :: ios :: app),
keywords :: format =%TimeStamp%[%Uptime%](%LineID%)<%Severity%> ;:%Message%
);
logging :: add_common_attributes();
logging :: core :: get() - > add_global_attribute(Uptime,attrs :: timer());
}

int main(int argc,const char * argv []){
init();
src :: severity_logger_mt< severity_level> lg = my_logger :: get();
BOOST_LOG_SEV(lg,DEBUG)< 调试严重性消息;
BOOST_LOG_SEV(lg,INFO)<< 信息严重性消息;
BOOST_LOG_SEV(lg,WARNING)<< 警告严重性消息;
BOOST_LOG_SEV(lg,ERROR)<< 错误严重性消息;
BOOST_LOG_SEV(lg,CRITICAL)<< 严重的严重性信息;
return 0;
}

注意注释掉的行:



// logging :: register_simple_formatter_factory<

p> I compile it with: g ++ main.cpp -Wall -DBOOST_ALL_DYN_LINK -lboost_system -lboost_log_setup -lboost_log -lboost_filesystem -lboost_date_time -lboost_thread -o main



任何帮助赞赏!

解决方案

进入同样的问题(空白%严重%文件和尝试添加寄存器功能时相同的编译器错误)。
我发现对register_simple_formatter_factory的调用应该是:

  boost :: log :: register_simple_formatter_factory< boost :: log :: trivial :: severity_level,char>(Severity); 


I'm using Boost-Log 2.0, which has some differences from version 1, and I have a hard time outputting the "Severity" attribute.

I'm using the "Boost.Format-style" formatters : "%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"

TimeStamp, LineID, and Message are common_attributes. Uptime is an attribute I added using attrs::timer(). I thought that Severity was automatically added when using severity_logger, but apparently it isn't and that's my problem. I get empty severities, eg:

2013-Apr-06 19:21:52.408974 [00:00:00.001337] (3) <>: A warning severity message

Notice the empty <>. I've tried to add severity using register_simple_formatter_factory but then I get the compiler error :

error: no matching function for call to ‘register_simple_formatter_factory(const char [9])’

and I don't get why.

Here's my code :

#include <iostream>


#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>

#include <boost/log/attributes.hpp>

using namespace std;

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

enum severity_level
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    CRITICAL
};

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt< severity_level> )

// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
    std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
    static const char* const str[] =
    {
        "DEBUG",
        "INFO",
        "WARNING",
        "ERROR",
        "CRITICAL"
    };
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
        strm << str[lvl];
    else
        strm << static_cast< int >(lvl);
    return strm;
}

void init() {
    // logging::register_simple_formatter_factory< severity_level >("Severity");
    logging::add_file_log(
            keywords::file_name = "blop.log",
            keywords::auto_flush = true,
            keywords::open_mode = (std::ios::out | std::ios::app),
            keywords::format = "%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"
            );
    logging::add_common_attributes();
    logging::core::get()->add_global_attribute("Uptime", attrs::timer());
}

int main(int argc, const char *argv[]) {
    init();
    src::severity_logger_mt< severity_level > lg = my_logger::get();
    BOOST_LOG_SEV(lg, DEBUG) << "A debug severity message";
    BOOST_LOG_SEV(lg, INFO) << "An informational severity message";
    BOOST_LOG_SEV(lg, WARNING) << "A warning severity message";
    BOOST_LOG_SEV(lg, ERROR) << "An error severity message";
    BOOST_LOG_SEV(lg, CRITICAL) << "A critical severity message";
    return 0;
}

Notice the commented-out line :

// logging::register_simple_formatter_factory< severity_level >("Severity");

which produces the mentioned error.

I compile it with : g++ main.cpp -Wall -DBOOST_ALL_DYN_LINK -lboost_system -lboost_log_setup -lboost_log -lboost_filesystem -lboost_date_time -lboost_thread -o main

Any help appreciated!

解决方案

Ran into the same problem (blank %Severity% in the log file AND the same compiler error when trying to add the register function). I found that the call to register_simple_formatter_factory should be:

boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");

这篇关于Boost Log 2.0:empty日志中的严重性级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆