Logger::setLevel(string) - optionally parse log level as number in range 1 to 8

This commit is contained in:
Marian Krivos 2014-12-07 20:39:00 +01:00
parent 8296d47cea
commit 0139648c46
2 changed files with 35 additions and 1 deletions

View File

@ -19,6 +19,7 @@
#include "Poco/LoggingRegistry.h"
#include "Poco/Exception.h"
#include "Poco/NumberFormatter.h"
#include "Poco/NumberParser.h"
#include "Poco/String.h"
@ -432,7 +433,18 @@ int Logger::parseLevel(const std::string& level)
else if (icompare(level, "trace") == 0)
return Message::PRIO_TRACE;
else
throw InvalidArgumentException("Not a valid log level", level);
{
int numLevel;
if (Poco::NumberParser::tryParse(level, numLevel))
{
if (numLevel > 0 && numLevel < 9)
return numLevel;
else
throw InvalidArgumentException("Log level out of range ", level);
}
else
throw InvalidArgumentException("Not a valid log level", level);
}
}

View File

@ -142,6 +142,28 @@ void LoggerTest::testLogger()
pChannel->list().clear();
root.fatal("fatal");
assert (pChannel->list().begin()->getPriority() == Message::PRIO_FATAL);
root.setLevel("1");
assert (root.getLevel() == Message::PRIO_FATAL);
root.setLevel("8");
assert (root.getLevel() == Message::PRIO_TRACE);
try
{
root.setLevel("0");
assert(0);
}
catch(Poco::InvalidArgumentException&)
{
}
try
{
root.setLevel("9");
assert(0);
}
catch(Poco::InvalidArgumentException&)
{
}
}