1

I got this example that I edited to fit my needs, but now I change the only 3 instances of make_shared and shared_ptr to std:: instead of boost::, and I get the "no matching function for call" error. Here's the minimal example:

#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;
}

The error I'm getting is:

 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"));
                                                  ^

And the makefile I use is this (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)

I'm on Debian Jessie, with gcc (Debian 4.9.2-10) 4.9.2.

What am I doing wrong?

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Unfortunately it appears that the parameter passed to [`basic_text_ostream_backend::add_stream`](http://www.boost.org/doc/libs/1_63_0/libs/log/doc/html/boost/log/sinks/basic_text_ostream_backend.html#idp46921712-bb) *must* be a `boost::shared_ptr` -- there's no overload for `std::shared_ptr` as far as I can see. – G.M. Apr 09 '17 at 10:25
  • 1
    @G.M. This sounds like a bug in boost::log... why isn't this being fixed... I wonder! – The Quantum Physicist Apr 09 '17 at 10:26

0 Answers0