src/log4qt/logger.h File Reference

#include <QtCore/QObject>
#include <QtCore/QAtomicPointer>
#include <QtCore/QList>
#include <QtCore/QReadWriteLock>
#include "log4qt/helpers/logerror.h"
#include "log4qt/helpers/classlogger.h"
#include "log4qt/helpers/logobjectptr.h"
#include "log4qt/level.h"

Go to the source code of this file.

Namespaces

namespace  Log4Qt

Classes

class  Log4Qt::Logger
 The class Logger provides logging services. More...

Defines

#define LOG4QT_DECLARE_STATIC_LOGGER(FUNCTION, CLASS)
#define LOG4QT_DECLARE_QCLASS_LOGGER


Define Documentation

#define LOG4QT_DECLARE_QCLASS_LOGGER

Value:

private:                                                              \
            mutable Log4Qt::ClassLogger mLog4QtClassLogger;                   \
        public:                                                               \
            inline Log4Qt::Logger *logger() const                             \
            {   return mLog4QtClassLogger.logger(this);    }                  \
        private:
LOG4QT_DECLARE_QCLASS_LOGGER declares member functions to retrieve Logger for the class it is used in.

On the first invocation the Logger is requested by a call to Logger::logger(const char *pName). The pointer is stored to be returned on subsequent invocations.

The following example shows how to use the macro to define a logger to be used within a class derived from QObject.

 #file: counter.h

 #include qobject.h
 #include logger.h

 class Counter : public QObject
 {
     Q_OBJECT
     LOG4QT_DECLARE_QCLASS_LOGGER
 public:
     Counter();
     Counter(int preset);
 private:
     int mCount;
 }
 #file: counter.cpp

 #include counter.h

 Counter::Counter() :
     mCount(0)
 {}

 void Counter::Counter(int preset)
     mCount(preset)
 {
     if (preset < 0)
     {
         logger()->warn("Invalid negative counter preset %1. Using 0 instead.", preset);
         mCount = 0;
     }
 }

Note:
The function created by the macro is thread-safe.
See also:
Logger::logger(const char *pName), ClassLogger

#define LOG4QT_DECLARE_STATIC_LOGGER ( FUNCTION,
CLASS   ) 

Value:

static Log4Qt::Logger *FUNCTION()                                     \
        {                                                                     \
            static QBasicAtomicPointer<Log4Qt::Logger > p_logger =            \
                Q_BASIC_ATOMIC_INITIALIZER(0);                                \
            if (!p_logger)                                                    \
            {                                                                 \
                p_logger.testAndSetOrdered(0,                                 \
                    Log4Qt::Logger::logger( #CLASS ));                        \
            }                                                                 \
            return p_logger;                                                  \
        }
LOG4QT_DECLARE_STATIC_LOGGER declares a static function FUNCTION that returns a pointer to a Logger named after CLASS.

On the first invocation the Logger is requested by calling Logger::logger( #CLASS ). The pointer is stored to be returned on subsequent invocations.

The following example shows how to use the macro to define a logger to be used within a class not derived from QObject.

 #file: counter.h

 #include logger.h

 class Counter
 {
 public:
     Counter();
     Counter(int preset);
 private:
     int mCount;
 }
 #file: counter.cpp

 #include counter.h

 LOG4QT_DECLARE_STATIC_LOGGER(logger, Counter)

 Counter::Counter() :
     mCount(0)
 {}

 void Counter::Counter(int preset) :
     mCount(preset)
 {
     if (preset < 0)
     {
         logger()->warn("Invalid negative counter preset %1. Using 0 instead.", preset);
         mCount = 0;
     }
 }

Note:
The function created by the macro is thread-safe.
See also:
Logger::logger(const char *pName)


Generated on Sun Mar 1 16:40:45 2009 for Log4Qt by  doxygen 1.5.6