I am using boost 1.54.0. Below you can find a minimum example that illustrates my problem.

I use the severity_logger of boost log. I want to configure my sinks from a stream. (In the following example I use a stringstream. In my real application the stream comes from a file.) I want to use the %Severity% for output or filtering purposes.

My problem is: If I use it as given in the example below, %Severity% is empty.

%LineID% and %Message% are filled as expected. If I set up a sink as given in the outcommented lines, it works as expected.

Any ideas?

#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/expressions.hpp>

enum SeverityLevel { trace, fatal };

int main (int argc, char *argv[])
{
    boost::log::add_common_attributes();
    /*
    struct severity_tag;
    boost::log::add_console_log(std::clog,
        boost::log::keywords::format = (
            boost::log::expressions::stream
                << boost::log::expressions::attr< unsigned int >("LineID")
                << ": <" << boost::log::expressions::attr<SeverityLevel, severity_tag >("Severity")
                << "> " << boost::log::expressions::smessage)
    ); */

    std::stringstream s;
    s << "[Sinks.MySink]" << std::endl;
    s << "Destination=Console" << std::endl;
    s << "Format=\"%LineID%: <%Severity%> - %Message%\"" << std::endl;
    boost::log::init_from_stream(s);

    boost::log::sources::severity_logger<SeverityLevel> lg;
    BOOST_LOG_SEV(lg, trace) << "This is a trace message";
    BOOST_LOG_SEV(lg, fatal) << "This is a fatal message";
    return 0;
}
有帮助吗?

解决方案

You are right. This is a known bug and fixed in the current development version.

Here is the bug report: https://svn.boost.org/trac/boost/ticket/8840

To make this answer less dependent on the link here is the way it was resolved within the report:

You need to register your severity attribute in the library before parsing the settings file. See here. The attribute may have to be registered both for formatter and filter parsers, if you want to filter records based on severity level.

OK, this is working but I had to add a stream input/extraction function and then I had to add the following two lines before loading the settings from the settings file:

logging::register_simple_formatter_factory<ESeverityLevel, char>("Severity");
logging::register_simple_filter_factory<ESeverityLevel, char>("Severity"); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top