From 367b174d78060a04313acbf09780b669c554a82d Mon Sep 17 00:00:00 2001 From: Marian Krivos Date: Sat, 4 Aug 2012 10:40:58 +0000 Subject: [PATCH] Poco::Logger - add formatted version of methods --- Foundation/include/Poco/Logger.h | 346 ++++++++++++++++++++++- Foundation/testsuite/src/LoggerTest.cpp | 35 +++ Foundation/testsuite/src/LoggerTest.h | 1 + Foundation/testsuite/src/TestChannel.cpp | 1 + Foundation/testsuite/src/TestChannel.h | 2 + 5 files changed, 384 insertions(+), 1 deletion(-) diff --git a/Foundation/include/Poco/Logger.h b/Foundation/include/Poco/Logger.h index 8e065647f..72f6d580b 100644 --- a/Foundation/include/Poco/Logger.h +++ b/Foundation/include/Poco/Logger.h @@ -172,6 +172,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void fatal(const std::string& fmt, const Any& value1); + void fatal(const std::string& fmt, const Any& value1, const Any& value2); + void fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void critical(const std::string& msg); /// If the Logger's log level is at least PRIO_CRITICAL, /// creates a Message with priority PRIO_CRITICAL @@ -188,6 +195,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void critical(const std::string& fmt, const Any& value1); + void critical(const std::string& fmt, const Any& value1, const Any& value2); + void critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void error(const std::string& msg); /// If the Logger's log level is at least PRIO_ERROR, /// creates a Message with priority PRIO_ERROR @@ -204,6 +218,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void error(const std::string& fmt, const Any& value1); + void error(const std::string& fmt, const Any& value1, const Any& value2); + void error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void warning(const std::string& msg); /// If the Logger's log level is at least PRIO_WARNING, /// creates a Message with priority PRIO_WARNING @@ -220,6 +241,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void warning(const std::string& fmt, const Any& value1); + void warning(const std::string& fmt, const Any& value1, const Any& value2); + void warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void notice(const std::string& msg); /// If the Logger's log level is at least PRIO_NOTICE, /// creates a Message with priority PRIO_NOTICE @@ -236,6 +264,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void notice(const std::string& fmt, const Any& value1); + void notice(const std::string& fmt, const Any& value1, const Any& value2); + void notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void information(const std::string& msg); /// If the Logger's log level is at least PRIO_INFORMATION, /// creates a Message with priority PRIO_INFORMATION @@ -252,6 +287,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void information(const std::string& fmt, const Any& value1); + void information(const std::string& fmt, const Any& value1, const Any& value2); + void information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void debug(const std::string& msg); /// If the Logger's log level is at least PRIO_DEBUG, /// creates a Message with priority PRIO_DEBUG @@ -268,6 +310,13 @@ public: /// the __FILE__ macro. The string is not copied /// internally for performance reasons. + void debug(const std::string& fmt, const Any& value1); + void debug(const std::string& fmt, const Any& value1, const Any& value2); + void debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void trace(const std::string& msg); /// If the Logger's log level is at least PRIO_TRACE, /// creates a Message with priority PRIO_TRACE @@ -283,7 +332,14 @@ public: /// File must be a static string, such as the value of /// the __FILE__ macro. The string is not copied /// internally for performance reasons. - + + void trace(const std::string& fmt, const Any& value1); + void trace(const std::string& fmt, const Any& value1, const Any& value2); + void trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3); + void trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4); + void trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5); + void trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6); + void dump(const std::string& msg, const void* buffer, std::size_t length, Message::Priority prio = Message::PRIO_DEBUG); /// Logs the given message, followed by the data in buffer. /// @@ -628,6 +684,42 @@ inline void Logger::fatal(const std::string& msg, const char* file, int line) } +inline void Logger::fatal(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_FATAL); +} + + +inline void Logger::fatal(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_FATAL); +} + + +inline void Logger::fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_FATAL); +} + + +inline void Logger::fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_FATAL); +} + + +inline void Logger::fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_FATAL); +} + + +inline void Logger::fatal(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_FATAL); +} + + inline void Logger::critical(const std::string& msg) { log(msg, Message::PRIO_CRITICAL); @@ -640,6 +732,42 @@ inline void Logger::critical(const std::string& msg, const char* file, int line) } +inline void Logger::critical(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_CRITICAL); +} + + +inline void Logger::critical(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_CRITICAL); +} + + +inline void Logger::critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_CRITICAL); +} + + +inline void Logger::critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_CRITICAL); +} + + +inline void Logger::critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_CRITICAL); +} + + +inline void Logger::critical(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_CRITICAL); +} + + inline void Logger::error(const std::string& msg) { log(msg, Message::PRIO_ERROR); @@ -652,6 +780,42 @@ inline void Logger::error(const std::string& msg, const char* file, int line) } +inline void Logger::error(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_ERROR); +} + + +inline void Logger::error(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_ERROR); +} + + +inline void Logger::error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_ERROR); +} + + +inline void Logger::error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_ERROR); +} + + +inline void Logger::error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_ERROR); +} + + +inline void Logger::error(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_ERROR); +} + + inline void Logger::warning(const std::string& msg) { log(msg, Message::PRIO_WARNING); @@ -664,6 +828,42 @@ inline void Logger::warning(const std::string& msg, const char* file, int line) } +inline void Logger::warning(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_WARNING); +} + + +inline void Logger::warning(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_WARNING); +} + + +inline void Logger::warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_WARNING); +} + + +inline void Logger::warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_WARNING); +} + + +inline void Logger::warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_WARNING); +} + + +inline void Logger::warning(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_WARNING); +} + + inline void Logger::notice(const std::string& msg) { log(msg, Message::PRIO_NOTICE); @@ -676,6 +876,42 @@ inline void Logger::notice(const std::string& msg, const char* file, int line) } +inline void Logger::notice(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_NOTICE); +} + + +inline void Logger::notice(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_NOTICE); +} + + +inline void Logger::notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_NOTICE); +} + + +inline void Logger::notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_NOTICE); +} + + +inline void Logger::notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_NOTICE); +} + + +inline void Logger::notice(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_NOTICE); +} + + inline void Logger::information(const std::string& msg) { log(msg, Message::PRIO_INFORMATION); @@ -688,6 +924,42 @@ inline void Logger::information(const std::string& msg, const char* file, int li } +inline void Logger::information(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_INFORMATION); +} + + +inline void Logger::information(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_INFORMATION); +} + + +inline void Logger::information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_INFORMATION); +} + + +inline void Logger::information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_INFORMATION); +} + + +inline void Logger::information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_INFORMATION); +} + + +inline void Logger::information(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_INFORMATION); +} + + inline void Logger::debug(const std::string& msg) { log(msg, Message::PRIO_DEBUG); @@ -700,6 +972,42 @@ inline void Logger::debug(const std::string& msg, const char* file, int line) } +inline void Logger::debug(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_DEBUG); +} + + +inline void Logger::debug(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_DEBUG); +} + + +inline void Logger::debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_DEBUG); +} + + +inline void Logger::debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_DEBUG); +} + + +inline void Logger::debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_DEBUG); +} + + +inline void Logger::debug(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_DEBUG); +} + + inline void Logger::trace(const std::string& msg) { log(msg, Message::PRIO_TRACE); @@ -712,6 +1020,42 @@ inline void Logger::trace(const std::string& msg, const char* file, int line) } +inline void Logger::trace(const std::string& fmt, const Any& value1) +{ + log(Poco::format(fmt, value1), Message::PRIO_TRACE); +} + + +inline void Logger::trace(const std::string& fmt, const Any& value1, const Any& value2) +{ + log(Poco::format(fmt, value1, value2), Message::PRIO_TRACE); +} + + +inline void Logger::trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) +{ + log(Poco::format(fmt, value1, value2, value3), Message::PRIO_TRACE); +} + + +inline void Logger::trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) +{ + log(Poco::format(fmt, value1, value2, value3, value4), Message::PRIO_TRACE); +} + + +inline void Logger::trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5), Message::PRIO_TRACE); +} + + +inline void Logger::trace(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) +{ + log(Poco::format(fmt, value1, value2, value3, value4, value5, value6), Message::PRIO_TRACE); +} + + inline bool Logger::is(int level) const { return _level >= level; diff --git a/Foundation/testsuite/src/LoggerTest.cpp b/Foundation/testsuite/src/LoggerTest.cpp index caa4b0c82..6c1483441 100644 --- a/Foundation/testsuite/src/LoggerTest.cpp +++ b/Foundation/testsuite/src/LoggerTest.cpp @@ -187,6 +187,40 @@ void LoggerTest::testFormat() assert (str == "1234"); } +void LoggerTest::testFormatAny() +{ + AutoPtr pChannel = new TestChannel; + Logger& root = Logger::root(); + root.setChannel(pChannel.get()); + + root.error("%s%s", std::string("foo"), std::string("bar")); + assert (pChannel->getLastMessage().getText() == "foobar"); + + root.error("foo%s", std::string("bar")); + assert (pChannel->getLastMessage().getText() == "foobar"); + + root.error("the amount is %% %d", 100); + assert (pChannel->getLastMessage().getText() == "the amount is % 100"); + + root.error("%d", 1); + assert (pChannel->getLastMessage().getText() == "1"); + + root.error("%d%d", 1, 2); + assert (pChannel->getLastMessage().getText() == "12"); + + root.error("%d%d%d", 1, 2, 3); + assert (pChannel->getLastMessage().getText() == "123"); + + root.error("%d%d%d%d", 1, 2, 3, 4); + assert (pChannel->getLastMessage().getText() == "1234"); + + root.error("%d%d%d%d%d", 1, 2, 3, 4, 5); + assert (pChannel->getLastMessage().getText() == "12345"); + + root.error("%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6); + assert (pChannel->getLastMessage().getText() == "123456"); +} + void LoggerTest::testDump() { @@ -245,6 +279,7 @@ CppUnit::Test* LoggerTest::suite() CppUnit_addTest(pSuite, LoggerTest, testLogger); CppUnit_addTest(pSuite, LoggerTest, testFormat); + CppUnit_addTest(pSuite, LoggerTest, testFormatAny); CppUnit_addTest(pSuite, LoggerTest, testDump); return pSuite; diff --git a/Foundation/testsuite/src/LoggerTest.h b/Foundation/testsuite/src/LoggerTest.h index ea01e50ef..649364918 100644 --- a/Foundation/testsuite/src/LoggerTest.h +++ b/Foundation/testsuite/src/LoggerTest.h @@ -48,6 +48,7 @@ public: void testLogger(); void testFormat(); + void testFormatAny(); void testDump(); void setUp(); diff --git a/Foundation/testsuite/src/TestChannel.cpp b/Foundation/testsuite/src/TestChannel.cpp index cbe5234ff..838924496 100644 --- a/Foundation/testsuite/src/TestChannel.cpp +++ b/Foundation/testsuite/src/TestChannel.cpp @@ -46,6 +46,7 @@ TestChannel::~TestChannel() void TestChannel::log(const Poco::Message& msg) { _msgList.push_back(msg); + _lastMessage = msg; } diff --git a/Foundation/testsuite/src/TestChannel.h b/Foundation/testsuite/src/TestChannel.h index 42d5a0666..fd63a463f 100644 --- a/Foundation/testsuite/src/TestChannel.h +++ b/Foundation/testsuite/src/TestChannel.h @@ -52,9 +52,11 @@ public: void log(const Poco::Message& msg); MsgList& list(); void clear(); + const Poco::Message& getLastMessage() const { return _lastMessage; } private: MsgList _msgList; + Poco::Message _lastMessage; };