Можно ли использовать std::shared_ptr с boost::log?

Я получил этот пример, который я отредактировал, чтобы соответствовать моим потребностям, но теперь я изменяю только 3 экземпляра make_shared а также shared_ptr в 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 Джесси, с gcc (Debian 4.9.2-10) 4.9.2,

Что я делаю неправильно?

0 ответов

Другие вопросы по тегам