Boost.Log unable to set logging filter (undeclared identifier 'severity')
Asked Answered
I

2

7

I'm trying to get Boost.Log going in my project. The problem comes in the following line from the trivial example:

using namespace boost::log;
core::get()->set_filter
(
    trivial::severity >= trivial::info
);

In my code, this translates to the following:

boost::log::core::get()->set_filter(
  boost::log::trivial::severity >= boost::log::trivial::info
);

However, I get the following errors:

error C2039: 'severity' : is not a member of 'boost::log::v2s_mt_nt5::trivial'
error C2065: 'severity' : undeclared identifier

I'm kind of searching around the namespaces trying to find out how I'm supposed to do this, but I'm not really finding anything that works. It seems it's some crazy lambda function required for this. I'm alright with some alternative (defining a function that fills in the filtering level?), but I'm not sure how to accomplish this. Any ideas?

I'm using Boost.Log version 2.0-r862, and Boost 1.53.0.

SOLUTION: Ryan pointed out that I should check my includes, and sure enough I was only including trivial.hpp, but core.hpp and expressions.hpp are also required as includes. Including them solved the problem.

// need at least these 3 to get "trivial" to work
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
Interdiction answered 1/5, 2013 at 19:36 Comment(1)
Hey, aardvark, I'm having trouble filtering the severity. My question is here: https://mcmap.net/q/1628115/-boost-set_filter-is-not-working/1735836Eyespot
R
2

Looking at trivial.hpp, it appears severity is part of the keywords namespace. Did you try boost::log::keywords::severity?

Rafferty answered 1/5, 2013 at 19:48 Comment(5)
Yeah, I've tried that too. Unfortunately I get the following: error C2784: 'std::_Boolarray std::operator >=(const std::valarray<_Ty> &,const std::valarray<_Ty> &)' : could not deduce template argument for 'const std::valarray<_Ty> &' from 'const boost::parameter::keyword<Tag>'Interdiction
It looks like the >= operator isn't defined for the keyword type? I think the "severity level keyword" is different than a "severity level", so it kind of makes sense that this doesn't work...Interdiction
Ugh, this was totally it. For some (stupid?) reason, I assumed I could get away with only including trivial.hpp. Not sure where that logic came from, but the error resolved itself upon including core.hpp and expressions.hpp. Sorry for my dumbness, and thanks so much for your help!Interdiction
Not a problem, we all forget the includes sometimes :). Good to hear it worked out.Rafferty
@Ryan, I'm having trouble filtering the severity. My question is here: https://mcmap.net/q/1628115/-boost-set_filter-is-not-working/1735836Eyespot
H
0

Here is a full working example in C++11, that summarizes @aardvarkk solution:

#include <boost/log/expressions.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>

namespace logging = boost::log;

auto init() -> void
{
    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::warning
    );
}

auto main(int argn, char *args[]) -> int
{
    init();

    BOOST_LOG_TRIVIAL(info) << "Testing the log system";
    BOOST_LOG_TRIVIAL(error) << "Testing the log error";


    return 0;
}
Heterogamy answered 3/3, 2015 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.