如何使用boost.log记录器?

时间:2019-05-14 13:02:49

标签: c++ boost-log

尝试基本的boost.log示例,但失败

我正在将Boost.log v2集成到我的项目中。 该平台是带有vs2017的Windows 10。 该程序已编译为目标x64。

#include <boost/thread/mutex.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/exceptions.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks.hpp>
void test(){
   boost::log::sources::severity_logger<severity_level> slg1;
   slg1.open_record();               // this is ok
   slg1.open_record(normal);          // this line failed at next code
}
template< typename BaseT, typename LevelT = int >
class basic_severity_logger : public BaseT
{
    template< typename ArgsT >
    record open_record_unlocked(ArgsT const& args)
    {
       // !!! error at here !!! 
       // because "normal" is an enum variable that doesn't support "operator[]"
        m_SeverityAttr.set_value(args[keywords::severity | m_DefaultSeverity]);   


        return base_type::open_record_unlocked(args);
    }
};

实际上,我不知道basic_severity_logger的称呼吗?

// logger with multithread supported
boost::log::sources::severity_logger_mt<severity_level> slg;
slg.open_record();                // failed with "no member named 'open_record' in severity_logger_mt<...>"
slg.open_record(DEBUG);           // failed with "no member named 'open_record' in severity_logger_mt<...>"

当前,代码在编译步骤失败,尚未到达链接阶段。

我尝试了boost-1.64,boost-1.68和boost-1.70,问题仍然相同

有人可以帮助我吗?

代码第一部分的错误消息

D:\libraries\boost\boost_1_70_0\boost/log/sources/severity_feature.hpp(252): error C2676: binary “[”:“const ArgsT”does not define this operator or a conversion to a type acceptable to the predefined operator
        with
        [
            ArgsT=severity_level
        ]

1 个答案:

答案 0 :(得分:0)

谢谢大家。 我现在知道如何解决它,但我不知道为什么。

修改代码如下:

boost::log::sources::severity_logger<boost::log::trivial::severity_level> slg1;
slg1.open_record();
BOOST_LOG_SEV(slg1, boost::log::trivial::trace) << "A regular message";
slg1.open_record(boost::log::keywords::severity=boost::log::trivial::trace);

boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> slg;
slg.open_record();
slg.open_record(boost::log::keywords::severity=boost::log::trivial::trace);

密钥是所使用的参数boost::log::keywords::severity

slg1.open_record(boost::log::keywords::severity=boost::log::trivial::trace);

有人告诉我它是如何工作的吗? boost::log::keywords::severity是功能对象还是其他对象?