master
Denes Matetelki 13 years ago
parent a847388ee1
commit 65c49e071c

@ -6,6 +6,7 @@
#include <string> #include <string>
#include <set> #include <set>
#include <ostream> #include <ostream>
#include "Common.hpp"
class Logger : public Singleton<Logger> class Logger : public Singleton<Logger>
@ -31,7 +32,7 @@ public:
static void init(std::ostream& log_stream ); static void init(std::ostream& log_stream );
static void setLogLevel ( const LogLevel loglevel ); static void setLogLevel ( const LogLevel loglevel );
static void setNoPrefix (); static void usePrefix ( const bool use = true );
inline static LogLevel getLoglevel() { return m_logLevel; } inline static LogLevel getLoglevel() { return m_logLevel; }
@ -63,42 +64,73 @@ private:
#ifdef NO_TRACE #ifdef NO_TRACE
#define TRACE (void)0 #define TRACE (void)0;
#define TRACE_STATIC (void)0 #define TRACE_STATIC (void)0;
#define LOG(level, msg) (void)0
#define LOG_STATIC(level, msg) (void)0
#else #else
#define TRACE \ #define TRACE \
if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \ if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \
Logger::getInstance()->log_pointer( \ Logger::getInstance()->log_pointer( \
this, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ this, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#define TRACE_STATIC \ #define TRACE_STATIC \
if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \ if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \
Logger::getInstance()->log_pointer( \ Logger::getInstance()->log_pointer( \
0, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ 0, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#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) \ #define LOG(level, msg) \
if ( Logger::getInstance()->getLoglevel() >= level ) \ if ( Logger::getInstance()->getLoglevel() >= level ) \
Logger::getInstance()->log_string( \ Logger::getInstance()->log_string( \
level, this, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ level, this, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#define LOG_STATIC(level, msg) \ #define LOG_STATIC(level, msg) \
if ( Logger::getInstance()->getLoglevel() >= level ) \ if ( Logger::getInstance()->getLoglevel() >= level ) \
Logger::getInstance()->log_string( \ Logger::getInstance()->log_string( \
level, 0, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ level, 0, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
else (void)0
#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 #endif
/// @todo remove this #define MSG(text) Logger::getInstance()->msg(text);
#define MSG(text) Logger::getInstance()->msg(text)
#endif // LOGGER_HPP #endif // LOGGER_HPP

@ -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;
} }

@ -0,0 +1,91 @@
#include <cxxtest/TestSuite.h>
#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");
}
};
Loading…
Cancel
Save