是否可以将std :: shared_ptr与boost :: log一起使用?

时间:2017-04-09 09:56:26

标签: c++ c++11 boost shared-ptr boost-log

我根据自己的需要编辑了this example,但现在我只将make_sharedshared_ptr的3个实例更改为std::而不是boost:: },我得到“没有匹配的调用函数”错误。这是最小的例子:

#include <memory>
#include <fstream>
#include <iomanip>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include "boost/log/utility/setup.hpp"
#include "boost/log/support/date_time.hpp"
#include <boost/log/expressions/formatters/xml_decorator.hpp>

namespace b_logging = boost::log;
namespace b_srcs = b_logging::sources;
namespace b_expr = b_logging::expressions;
namespace b_sinks = b_logging::sinks;

b_logging::formatting_ostream& severity_wrap_prefix(b_logging::formatting_ostream& strm, const b_logging::trivial::severity_level& sev)
{
    if     (sev == b_logging::trivial::severity_level::trace)
        strm << "<font color='gray'>";
    else if(sev == b_logging::trivial::severity_level::debug)
        strm << "<font color='#808080'>";
    else if(sev == b_logging::trivial::severity_level::info)
        strm << "<font color='green'>";
    else if(sev == b_logging::trivial::severity_level::warning)
        strm << "<font color='orange'>";
    else if(sev == b_logging::trivial::severity_level::error)
        strm << "<font color='red'>";
    else if(sev == b_logging::trivial::severity_level::fatal)
        strm << "<strong><font color='red'>";
    else
        strm << "<UNKNOWN SEVERITY LEVEL>";
    return strm;
}

b_logging::formatting_ostream& severity_wrap_suffix(b_logging::formatting_ostream& strm, const b_logging::trivial::severity_level& sev)
{
    if     (sev == b_logging::trivial::severity_level::trace)
        strm << "</font><br>";
    else if(sev == b_logging::trivial::severity_level::debug)
        strm << "</font><br>";
    else if(sev == b_logging::trivial::severity_level::info)
        strm << "</font><br>";
    else if(sev == b_logging::trivial::severity_level::warning)
        strm << "</font><br>";
    else if(sev == b_logging::trivial::severity_level::error)
        strm << "</font><br>";
    else if(sev == b_logging::trivial::severity_level::fatal)
        strm << "</strong></font><br>";
    else
        strm << "<UNKNOWN SEVERITY LEVEL>";
    return strm;
}

std::string ptime_to_string(const boost::posix_time::ptime& time)
{
    std::stringstream str;
    boost::posix_time::time_facet *facet = new boost::posix_time::time_facet("%d.%m.%Y-%H:%M:%S-UTC");
    str.imbue(std::locale(str.getloc(), facet));
    str << time;
    return str.str();
}

void init()
{
    b_logging::core::get()->add_global_attribute("TimeStamp", b_logging::attributes::utc_clock());
    typedef b_sinks::synchronous_sink<b_sinks::text_ostream_backend> text_sink;
    std::shared_ptr<text_sink> sink = std::make_shared<text_sink>();

    sink->locked_backend()->add_stream(
        std::make_shared<std::ofstream>("sample.htm"));

    sink->set_filter(b_logging::trivial::severity >= b_logging::trivial::severity_level::info);

    sink->set_formatter([&](b_logging::record_view const& rec, b_logging::formatting_ostream& strm)
    {
        const b_logging::trivial::severity_level& sev = *b_logging::extract<b_logging::trivial::severity_level>("Severity", rec);

        const boost::posix_time::ptime &pt = *b_logging::extract<boost::posix_time::ptime>("TimeStamp", rec);

        severity_wrap_prefix(strm,sev);
        strm << ptime_to_string(pt) << ": "
                     << rec[b_expr::smessage];
        strm << b_expr::xml_decor[b_expr::stream << b_expr::smessage];
        severity_wrap_suffix(strm,sev);
    });

    b_logging::core::get()->add_sink(sink);
}


int main(int, char*[])
{
    std::cout<<"Start"<<std::endl;
    init();
    b_logging::add_common_attributes();

    using namespace b_logging::trivial;
    b_srcs::severity_logger< severity_level > lg;

    BOOST_LOG_SEV(lg, trace) << "A trace severity message";
    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, fatal) << "A fatal severity message";

    std::cout<<"End"<<std::endl;
    return 0;
}

我得到的错误是:

 main.cpp:79: error: no matching function for call to ‘boost::log::v2s_mt_posix::sinks::basic_text_ostream_backend<char>::add_stream(std::shared_ptr<std::basic_ofstream<char> >)’
     std::make_shared<std::ofstream>("sample.htm"));
                                                  ^

我使用的makefile就是这个(CMake):

project(LogTest)
cmake_minimum_required(VERSION 2.8)

SET(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
aux_source_directory(. SRC_LIST)
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} libboost_log.a libboost_log_setup.a libboost_iostreams.a libboost_thread.a libboost_system.a libboost_filesystem.a libboost_regex.a -pthread)

我在Debian Jessie身上,gcc (Debian 4.9.2-10) 4.9.2

我做错了什么?

0 个答案:

没有答案