To make setting the log level at runtime easy, I created the following routine:
-----Utiliites.hpp
namespace Utilities
{
void setLogLevel(const std::string &level);
};
------Utilities.cpp -----
#include "Utilities.hpp"
#include
#include
namespace logging = boost::log;
#define SET_IF_LOG_LEVEL(lvlVar, levelVal) \
if (lvlVar.compare( #levelVal ) == 0) \
{ \
logging::core::get()->set_filter \
( \
logging::trivial::severity >= logging::trivial:: ## levelVal \
); \
}
void Utilities::setLogLevel(const std::string &level)
{
// default is debug
std::string lvl = (level.length() > 0)?level:"debug";
Utilities::lowerCase(lvl); // just to make sure we're consistent
if (lvl.compare("disabled") == 0)
boost::log::core::get()->set_logging_enabled(false);
SET_IF_LOG_LEVEL(lvl,trace);
SET_IF_LOG_LEVEL(lvl,debug);
SET_IF_LOG_LEVEL(lvl,info);
SET_IF_LOG_LEVEL(lvl,warning);
SET_IF_LOG_LEVEL(lvl,error);
SET_IF_LOG_LEVEL(lvl,fatal);
}
-------------------main.cpp ------------------------------
#include
#include
namespace logging = boost::log;
int main(int argc, char** argv)
{
if (argc > 1)
{
Utilities::setLogLevel(argv[1]);
}
//logging::core::get()->set_filter
// (
// logging::trivial::severity >= logging::trivial::debug
// );
BOOST_LOG_TRIVIAL(trace) << "Utilities unit test main starting " << std::endl;
}
------------------------
Utilities.cpp is compiled as part of a static library that is linked to my main routine. If I call this function from my main routine, it appears to have no effect. I still get every logging message no matter what level I attempt to set. However, if I call logging::core::get()->set_filter() from my main routine (passing an appropriate level) it works as expected.
I'm not certain what's happening here. Is it possible that logging::core::get() isn't actually dealing with a singleton? That the pointer returned by get() when called from my library is not the same as the pointer returned by the same call in my main routine?
And, regardless of what is happening, how do I fix this?
I'm using Visual Studio 2012 building Debug Win32 console application. Running on Windows 7 64bit.
Steve Hickman
System Architect, Flight Deck of the Future
480-236-8367