YotKay
YotKay

Reputation: 1207

Boost Log init_from_stream with trivial severity_level

I am trying to use Boost Log library with the feature of initializing from stream. I have some problem here. My code is simple, based on Boost Log documentation.

#include <boost/log/core.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/move/utility.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <iostream>
#include <fstream>

namespace logging  = boost::log;
namespace src      = boost::log::sources;
using namespace boost::log::trivial;

int main(int, char*[])
{        
   logging::add_common_attributes();

   logging::register_simple_filter_factory<logging::trivial::severity_level, char>("Severity");
   logging::register_simple_formatter_factory<logging::trivial::severity_level, char>("Severity");

   std::ifstream file("settings.ini");
   logging::init_from_stream(file);

   src::severity_logger<logging::trivial::severity_level> lg;

   BOOST_LOG_SEV(lg, error) << "test" << std::endl;

   return 0;
}

My settings.ini file is also simple, taken from boost documentation:

[Core]
DisableLogging=false
#Filter="%Severity% > 3"

# Sink settings sections
[Sinks.MySink1]

# Sink destination type
Destination=Console

# Sink-specific filter. Optional, by default no filter is applied.
#Filter="%Target% contains \"MySink1\""

# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%> - %Severity% - %Message%"

# The flag shows whether the sink should be asynchronous
Asynchronous=false

# Enables automatic stream flush after each log record.
AutoFlush=true

Note the commented line with "Filter" in Core section. If it is commented then everything works perfectly. However when I uncomment it I get an error saying:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl

' what(): bad lexical cast: source type value could not be interpreted as target Aborted (core dumped)

I read on forums and in the Boost Log docs that I should register filter and formatter factory even for build in type like boost::log::trivial::severity_level, before calling the init_from_stream function. And I do that. That helped for printing Severity (formatting) but didn't help for reading Severity (parsing). Documentation says that also function for reading Severity from istream is needed, but I checked that it is there in trivial.hpp in boost, so I guess I should not define it myself.

What am I missing here?

Upvotes: 0

Views: 751

Answers (1)

Andrey Semashev
Andrey Semashev

Reputation: 10606

The problem is that "3" in the filter string is not a valid value for the logging::trivial::severity_level enum. Boost.Log provides an operator>> for the enum, which expects one of the following strings: "trace", "debug", "info", "warning", "error" or "fatal". Each of the strings translates to the same named enum value.

Upvotes: 1

Related Questions