0

I used to format output of boost::log in a DLL of mine, named libfoo and used by a Win32 console application exefoo. Code is

// #include various boost log headers

void __declspec(dllexport) add_boost_log_console_sink()
{
    boost::log::add_common_attributes();

    boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();

    sink->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter()));

    sink->set_formatter(xspectra::get_our_formatter(false));

    sink->set_filter(boost::log::trivial::severity >= boost::log::trivial::info
        &&
        boost::log::expressions::has_attr(tag_attr) == false);

    boost::log::core::get()->add_sink(sink);
}

boost::log::formatter __declspec(dllexport) get_our_formatter(bool bSubSeconds)
{
    std::string sTimeStamp("%H:%M:%S");
    if (bSubSeconds)
        sTimeStamp += ".%f";

    return boost::log::expressions::stream
        //<< std::hex  //To print the LineID in Hexadecimal format
        << std::setw(6) << std::setfill('0')
        << line_id
        << "\t"
        << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", sTimeStamp)
        << " <" << boost::log::trivial::severity
        << ">  \t"
        /*
                    << boost::log::expressions::if_(boost::log::expressions::has_attr(tag_attr))
                    [
                        boost::log::expressions::stream << "[" << tag_attr << "]\t"         TAGS SUSPENDED
                    ]
        */
        << boost::log::expressions::smessage;
}

In the main, exposed object of libfoo, I was calling -once- add_boost_log_console_sink(); same call inside the main() of exefoo.

Every subsequent BOOST_LOG_TRIVIAL(severity) call inside libfoo was producing formatted output, while every similar call inside exefoo was not.

For code modularity purposes, I moved these functions into a new DLL, liblog, used by libfoo and exefoo. Now every log is not formatted.

It seems that having this code loaded from a DLL somehow doesn't produce any effect; I wish it would do.

Patrizio Bertoni
  • 2,582
  • 31
  • 43
  • Do you link with Boost.Log dynamically (i.e. dlls)? Do you define `BOOST_LOG_DYN_LINK` or `BOOST_ALL_DYN_LINK`? – Andrey Semashev Nov 30 '16 at 06:28
  • I don't, I'm linking statically against `libboost_log-vc140-mt-1_60.lib`. But *liblog* and *libfoo* are dlls. Maybe I should change at least *liblog* to static lib. – Patrizio Bertoni Nov 30 '16 at 08:47
  • That's the source of the problem. Boost.Log requires to be built as a shared library if used from different modules. http://www.boost.org/doc/libs/1_62_0/libs/log/doc/html/log/installation/config.html – Andrey Semashev Nov 30 '16 at 10:34

1 Answers1

0

As an insight provided by Andrey Semashev, i switched liblog Configuration Type from dynamic to static library. Now everything works fine.

Community
  • 1
  • 1
Patrizio Bertoni
  • 2,582
  • 31
  • 43