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.