boost.log 使用 sink.set_filter 时发生错误

boost.log error occured while using sink.set_filter

我的项目使用 boost.log。 但是当我尝试为接收器设置过滤器时,出现了编译错误。

代码很简单:

BOOST_LOG_ATTRIBUTE_KEYWORD(MyTag, "My_Tag", std::string);
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(
        my_logger
        , boost::log::sources::channel_logger_mt< >
        , (boost::log::keywords::channel = "default"))

typedef boost::log::sinks::synchronous_sink<
            boost::log::sinks::text_ostream_backend > TextSink;
boost::shared_ptr< TextSink > logSink = boost::make_shared< TextSink >();
auto backSink = boost::make_shared<std::ofstream>("default.log");

!!! compilation error, no add_attribute in channel_logger !!!
my_logger::get().add_attribute("My_Tag"
                , boost::log::attributes::constant<String>("My_Tag"));

logSink->set_filter(
                    boost::log::expressions::has_attr(MyTag)
!!! compilation error, invalid operand expression between attribute_actor and std::string !!!
                    && boost::log::expressions::attr<std::string>("My_Tag")==std::string("My_Tag")
!!! compilation error, invalid operand expression between attribute_keyword<tag::MyTag> and char[7] !!!
                    && MyTag=="My_Tag"

我也尝试定义过滤器函数,但出现以下错误:

bool my_filter(
            boost::log::value_ref< std::string
!!! compilation error, no tag_attr in boost::log::expressions::tag !!!
            , boost::log::expressions::tag::tag_attr > const& tag)
{
    return level >= warning || tag == "IMPORTANT_MESSAGE";
}

顺便说一句,我也有以下错误,但如果所有其他编译错误都解决了,这个错误就会消失。

!!! no open_record in severity_logger_mt !!!
BOOST_LOG_SEV(slg, normal) << "A regular message";

谁能帮帮我?


完整代码:

#include <boost/phoenix/operator.hpp>
#include <boost/log/sources/channel_logger.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/attributes/attribute.hpp>
#include <boost/log/attributes/attribute_cast.hpp>
#include <boost/log/keywords/filter.hpp>
#include <boost/log/expressions/predicates.hpp>
#include <boost/log/expressions/attr_fwd.hpp>
#include <boost/log/expressions/attr.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>

BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(
        my_channel_logger
        , boost::log::sources::channel_logger_mt< >
        , (boost::log::keywords::channel = "channel"))

BOOST_LOG_ATTRIBUTE_KEYWORD(MyTag, "My_Tag", std::string);

typedef boost::log::sinks::synchronous_sink<
            boost::log::sinks::text_ostream_backend > TextSink;
static boost::shared_ptr< TextSink > logSink = boost::make_shared< TextSink >();
static auto backSink = boost::make_shared<std::ofstream>("output.log");
void func() {

    !!! compilation error, no 'open_record' in channel_logger_mt !!!
    BOOST_LOG_CHANNEL(my_channel_logger::get(), "channel")<<"channel log";

    !!! compilation error, no add_attribute in channel_logger !!!
    my_channel_logger::get().add_attribute("My_Tag"
       , boost::log::attributes::constant<String>("My_Tag"));
}

no open_record in channel_logger_mt 的编译错误:

***.cpp:87:5: error: no member named 'open_record' in 'boost::log::v2_mt_nt6::sources::channel_logger_mt<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
channel_feature.hpp:236:41: note: expanded from macro 'BOOST_LOG_CHANNEL'
channel_feature.hpp:231:5: note: expanded from macro 'BOOST_LOG_STREAM_CHANNEL'
record_ostream.hpp:566:5: note: expanded from macro 'BOOST_LOG_STREAM_WITH_PARAMS'
record_ostream.hpp:555:50: note: expanded from macro 'BOOST_LOG_STREAM_WITH_PARAMS_INTERNAL'

no add_attribute in channel_logger_mt 的编译错误:

***.cpp:76:31: error: no member named 'add_attribute' in 'boost::log::v2_mt_nt6::sources::channel_logger_mt<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >'

您的代码中很可能缺少 #include

!!! compilation error, no add_attribute in channel_logger !!!
my_logger::get().add_attribute("My_Tag"
                , boost::log::attributes::constant<String>("My_Tag"));

add_attributebasic_composite_logger, from which channel_logger_mt derives 定义。您需要包括 boost/log/sources/channel_logger.hpp.

!!! compilation error, invalid operand expression between attribute_actor and std::string !!!
                    && boost::log::expressions::attr<std::string>("My_Tag")==std::string("My_Tag")
!!! compilation error, invalid operand expression between attribute_keyword<tag::MyTag> and char[7] !!!
                    && MyTag=="My_Tag"

您很可能遗漏了 boost/phoenix/operator.hpp 的包含。或者您可以包含 boost/log/expressions.hpp,它会自动为您包含它。

!!! no open_record in severity_logger_mt !!!
BOOST_LOG_SEV(slg, normal) << "A regular message";

channel_logger_mt 相同。您需要包括 boost/log/sources/severity_logger.hpp.

请注意,对于文档中的每个组件,在该部分的开头都有一个关联的 Boost.Log header 列表。 Here 是严重性记录器的示例。


2019-07-03更新:

如果添加以下内容,您的完整代码将编译:

#include <string>
#include <fstream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/record_ostream.hpp>

我还必须删除“!!!”行并在 constant 属性中使用 std::string 而不是 String

一般来说,您必须为代码中使用的每个组件包含一个 header。