From 65c49e071cd2d7d5368db26ae0133fcbf91cd441 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sun, 4 Dec 2011 21:33:06 +0100 Subject: [PATCH] LOG_PROP --- include/Logger.hpp | 74 +++++++++++++++++++++++++---------- src/Logger.cpp | 4 +- test/test_Logger.hpp | 91 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 test/test_Logger.hpp diff --git a/include/Logger.hpp b/include/Logger.hpp index 31df661..52712b3 100644 --- a/include/Logger.hpp +++ b/include/Logger.hpp @@ -6,6 +6,7 @@ #include #include #include +#include "Common.hpp" class Logger : public Singleton @@ -31,7 +32,7 @@ public: static void init(std::ostream& log_stream ); static void setLogLevel ( const LogLevel loglevel ); - static void setNoPrefix (); + static void usePrefix ( const bool use = true ); inline static LogLevel getLoglevel() { return m_logLevel; } @@ -63,42 +64,73 @@ private: #ifdef NO_TRACE - #define TRACE (void)0 - #define TRACE_STATIC (void)0 - #define LOG(level, msg) (void)0 - #define LOG_STATIC(level, msg) (void)0 + #define TRACE (void)0; + #define TRACE_STATIC (void)0; #else #define TRACE \ if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \ - Logger::getInstance()->log_pointer( \ - this, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - else (void)0 + Logger::getInstance()->log_pointer( \ + this, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ #define TRACE_STATIC \ if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \ - Logger::getInstance()->log_pointer( \ - 0, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - else (void)0 + Logger::getInstance()->log_pointer( \ + 0, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + +#endif + + +#ifdef NO_LOGS + + #define LOG(level, msg) (void)0; + #define LOG_STATIC(level, msg) (void)0; + + #define LOG_BEGIN(level) { + #define LOG_PROP(name, value) (void)0; + #define LOG_END(msg) } + #define LOG_STATIC_END(msg) } + +#else #define LOG(level, msg) \ - if ( Logger::getInstance()->getLoglevel() >= level ) \ - Logger::getInstance()->log_string( \ - level, this, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - else (void)0 + if ( Logger::getInstance()->getLoglevel() >= level ) \ + Logger::getInstance()->log_string( \ + level, this, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ #define LOG_STATIC(level, msg) \ - if ( Logger::getInstance()->getLoglevel() >= level ) \ - Logger::getInstance()->log_string( \ - level, 0, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - else (void)0 + if ( Logger::getInstance()->getLoglevel() >= level ) \ + Logger::getInstance()->log_string( \ + level, 0, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + + #define LOG_BEGIN(level) \ + if (Logger::getInstance()->getLoglevel() >= level) { \ + Logger::LogLevel loglevel(level); \ + std::string prop; \ + + #define LOG_SPROP(variable) \ + prop.append(" ").append(#variable).append(":").append(TToStr(variable)); + + #define LOG_PROP(name, value) \ + prop.append(" ").append(name).append(":").append(TToStr(value)); + + #define LOG_END(msg) \ + std::string logline(msg); \ + logline.append(prop); \ + Logger::getInstance()->log_string(loglevel, this, logline.c_str(), \ + __FILE__, __LINE__, __PRETTY_FUNCTION__ ); } + + #define LOG_END_STATIC(msg) \ + std::string logline(msg); \ + logline.append(prop); \ + Logger::getInstance()->log_string(loglevel, 0, logline.c_str(), \ + __FILE__, __LINE__, __PRETTY_FUNCTION__ ); } #endif -/// @todo remove this -#define MSG(text) Logger::getInstance()->msg(text) +#define MSG(text) Logger::getInstance()->msg(text); #endif // LOGGER_HPP diff --git a/src/Logger.cpp b/src/Logger.cpp index cf970e6..834bb77 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -18,9 +18,9 @@ void Logger::setLogLevel ( const LogLevel loglevel ) } -void Logger::setNoPrefix () +void Logger::usePrefix( const bool use ) { - m_usePrefix = false; + m_usePrefix = use; } diff --git a/test/test_Logger.hpp b/test/test_Logger.hpp new file mode 100644 index 0000000..6290266 --- /dev/null +++ b/test/test_Logger.hpp @@ -0,0 +1,91 @@ +#include + +#include "Common.hpp" +#include "Fixture.hpp" + + +class TestLoggerSuite : public CxxTest::TestSuite +{ + +public: + + void testBasic() + { + TEST_HEADER; + + int ears(2); + + LOG_BEGIN(Logger::DEBUG) + LOG_PROP("noses", 1) + LOG_PROP("ears", ears) + LOG_END("An avarege human"); + } + +private: + + static void f() + { + TRACE_STATIC; + + LOG_STATIC(Logger::INFO, "From static function"); + + int a(4); + std::string b("eer"); + LOG_BEGIN(Logger::DEBUG) + LOG_SPROP(a) + LOG_SPROP(b) + LOG_END_STATIC("Properties from a static function"); + } + +public: + + void testStatic() + { + TEST_HEADER; + + f(); + } + +private: + + class A { + public: + + A(const char *msg) : message(msg) + { + TRACE; + + LOG_BEGIN(Logger::DEBUG) + LOG_PROP("message", message) + LOG_SPROP(message) + LOG_END("my member"); + } + private: + + std::string message; + + }; + +public: + + void testMember() + { + TEST_HEADER; + + A a("hello"); + } + + void testNoPrefix() + { + TEST_HEADER; + + LOG(Logger::DEBUG, "With prefix"); + + Logger::usePrefix(false); + LOG(Logger::DEBUG, "Without prefix"); + + Logger::usePrefix(); + LOG(Logger::DEBUG, "With prefix again"); + } + +};