# logger.h

Go to the documentation of this file.

    00001 // Module:  Log4CPLUS
    00002 // File:    logger.h
    00003 // Created: 6/2001
    00004 // Author:  Tad E. Smith
    00005 //
    00006 //
    00007 // Copyright (C) Tad E. Smith  All rights reserved.
    00008 //
    00009 // This software is published under the terms of the Apache Software
    00010 // License version 1.1, a copy of which has been included with this
    00011 // distribution in the LICENSE.APL file.
    00012 //
    00013
    00017 #ifndef _LOG4CPLUS_LOGGERHEADER_
    00018 #define _LOG4CPLUS_LOGGERHEADER_
    00019
    00020 #include <log4cplus/config.h>
    00021 #include <log4cplus/loglevel.h>
    00022 #include <log4cplus/loggingmacros.h>
    00023 #include <log4cplus/tstring.h>
    00024 #include <log4cplus/streams.h>
    00025 #include <log4cplus/helpers/pointer.h>
    00026 #include <log4cplus/spi/appenderattachable.h>
    00027 #include <log4cplus/spi/loggerfactory.h>
    00028
    00029 #include <memory>
    00030 #include <vector>
    00031
    00032 namespace log4cplus {
    00033     // Forward declarations
    00034     namespace spi {
    00035         class LoggerImpl;
    00036         typedef helpers::SharedObjectPtr<LoggerImpl> SharedLoggerImplPtr;
    00037     }
    00038     class Appender;
    00039     class Hierarchy;
    00040     class HierarchyLocker;
    00041     class DefaultLoggerFactory;
    00042
    00043
    00046     typedef std::vector<Logger> LoggerList;
    00047
    00048
    00057     class LOG4CPLUS_EXPORT Logger : public log4cplus::spi::AppenderAttachable {
    00058     public:
    00059       // Static Methods
    00066         static bool exists(const log4cplus::tstring& name);
    00067
    00068         /*
    00069          * Returns all the currently defined loggers in the default
    00070          * hierarchy.
    00071          * <p>
    00072          * The root logger is <em>not</em> included in the returned
    00073          * list.
    00074          */
    00075         static LoggerList getCurrentLoggers();
    00076
    00080         static Hierarchy& getDefaultHierarchy();
    00081
    00093         static Logger getInstance(const log4cplus::tstring& name);
    00094
    00107         static Logger getInstance(const log4cplus::tstring& name, spi::LoggerFactory& factory);
    00108
    00119         static Logger getRoot();
    00120
    00135         static void shutdown();
    00136
    00137       // Non-Static Methods
    00147         void assertion(bool assertionVal, const log4cplus::tstring& msg) {
    00148             if(!assertionVal) {
    00149                 log(FATAL_LOG_LEVEL, msg);
    00150             }
    00151         }
    00152
    00157         void closeNestedAppenders();
    00158
    00165         bool isEnabledFor(LogLevel ll) const;
    00166
    00170         void log(LogLevel ll, const log4cplus::tstring& message,
    00171                  const char* file=NULL, int line=-1);
    00172
    00177         void forcedLog(LogLevel ll, const log4cplus::tstring& message,
    00178                        const char* file=NULL, int line=-1);
    00179
    00191         void callAppenders(const spi::InternalLoggingEvent& event);
    00192
    00201         LogLevel getChainedLogLevel() const;
    00202
    00208         LogLevel getLogLevel() const;
    00209
    00213         void setLogLevel(LogLevel);
    00214
    00219         Hierarchy& getHierarchy() const;
    00220
    00224         log4cplus::tstring getName() const;
    00225
    00229         bool getAdditivity() const;
    00230
    00234         void setAdditivity(bool additive);
    00235
    00236
    00237       // AppenderAttachable Methods
    00238         virtual void addAppender(SharedAppenderPtr newAppender);
    00239
    00240         virtual SharedAppenderPtrList getAllAppenders();
    00241
    00242         virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name);
    00243
    00244         virtual void removeAllAppenders();
    00245
    00246         virtual void removeAppender(SharedAppenderPtr appender);
    00247
    00248         virtual void removeAppender(const log4cplus::tstring& name);
    00249
    00250       // Copy Ctor
    00251         Logger(const Logger& rhs);
    00252         Logger& operator=(const Logger& rhs);
    00253
    00254       // Dtor
    00255         ~Logger();
    00256
    00261         Logger getParent();
    00262
    00263     protected:
    00264       // Data
    00266         spi::LoggerImpl *value;
    00267
    00268     private:
    00269       // Ctors
    00279         Logger(spi::LoggerImpl *ptr);
    00280         Logger(const spi::SharedLoggerImplPtr& val);
    00281
    00282       // Methods
    00283         void init();
    00284         void validate(const char *file, int line) const;
    00285
    00286       // Friends
    00287         friend class log4cplus::spi::LoggerImpl;
    00288         friend class log4cplus::Hierarchy;
    00289         friend class log4cplus::HierarchyLocker;
    00290         friend class log4cplus::DefaultLoggerFactory;
    00291     };
    00292
    00293
    00298     class LOG4CPLUS_EXPORT DefaultLoggerFactory : public spi::LoggerFactory {
    00299     public:
    00300         Logger makeNewLoggerInstance(const log4cplus::tstring& name, Hierarchy& h);
    00301     };
    00302
    00303
    00304
    00315     class LOG4CPLUS_EXPORT TraceLogger {
    00316     public:
    00317         TraceLogger(const Logger& l, const log4cplus::tstring& _msg,
    00318                     const char* _file=NULL, int _line=-1)
    00319           : logger(l), msg(_msg), file(_file), line(_line)
    00320         { if(logger.isEnabledFor(TRACE_LOG_LEVEL))
    00321               logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("ENTER: ") + msg, file, line);
    00322         }
    00323
    00324         ~TraceLogger()
    00325         { if(logger.isEnabledFor(TRACE_LOG_LEVEL))
    00326               logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("EXIT:  ") + msg, file, line);
    00327         }
    00328
    00329     private:
    00330         Logger logger;
    00331         log4cplus::tstring msg;
    00332         const char* file;
    00333         int line;
    00334     };
    00335
    00336 } // end namespace log4cplus
    00337
    00338
    00339 #endif // _LOG4CPLUS_LOGGERHEADER_
    00340