mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 08:22:37 +01:00
Logger::setLevel(string) - optionally parse log level as number in range 1 to 8
This commit is contained in:
parent
8296d47cea
commit
0139648c46
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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&)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user