Boost log severity_logger init_from_stream
Asked Answered
T

1

11

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;
}
Tetrastich answered 2/8, 2013 at 10:9 Comment(2)
It would appeat that you need to instruct Boost Log to actually use Sinks.MySink. Sadly, I have no experience with Boost Log configurationNarrow
That is not the problem. It is using MySink. The Format I provide is used as wanted. Except: %Severity% is replaced by an empty string. It should be the number of the enum value. It works out fine with the outcommented way of sink configuration.Tetrastich
U
11

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"); 
Uneducated answered 2/8, 2013 at 11:1 Comment(2)
You're the man. Took me 2 days until I stumbled upon your post because I believed I had made a config-mistake...Ichthyology
Few years later... Boost is version 1.62 and I was hitting the same issue in the last 2 days. Thanks to this answer I was able to make the "Severity" working.Boise

© 2022 - 2024 — McMap. All rights reserved.