00001 // Module: Log4CPLUS 00002 // File: logger.h 00003 // Created: 6/2001 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2001-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00024 #ifndef _LOG4CPLUS_LOGGERHEADER_ 00025 #define _LOG4CPLUS_LOGGERHEADER_ 00026 00027 #include "dcmtk/oflog/config.h" 00028 #include "dcmtk/oflog/loglevel.h" 00029 #include "dcmtk/oflog/tstring.h" 00030 #include "dcmtk/oflog/streams.h" 00031 #include "dcmtk/oflog/spi/apndatch.h" 00032 #include "dcmtk/oflog/spi/logfact.h" 00033 00034 //#include <vector> 00035 00036 00037 namespace log4cplus 00038 { 00039 // Forward declarations 00040 00041 class Appender; 00042 class Hierarchy; 00043 class HierarchyLocker; 00044 class DefaultLoggerFactory; 00045 00046 namespace spi 00047 { 00048 00049 class LoggerImpl; 00050 00051 } 00052 00053 00056 typedef OFList<Logger> LoggerList; 00057 typedef OFListIterator(Logger) LoggerListIterator; 00058 00059 00068 class LOG4CPLUS_EXPORT Logger 00069 : public log4cplus::spi::AppenderAttachable 00070 { 00071 public: 00072 // Static Methods 00079 static bool exists(const log4cplus::tstring& name); 00080 00081 /* 00082 * Returns all the currently defined loggers in the default 00083 * hierarchy. 00084 * 00085 * The root logger is <em>not</em> included in the returned 00086 * list. 00087 */ 00088 static LoggerList getCurrentLoggers(); 00089 00093 static Hierarchy& getDefaultHierarchy(); 00094 00106 static Logger getInstance(const log4cplus::tstring& name); 00107 00120 static Logger getInstance(const log4cplus::tstring& name, spi::LoggerFactory& factory); 00121 00132 static Logger getRoot(); 00133 00148 static void shutdown(); 00149 00150 // Non-Static Methods 00159 void assertion(bool assertionVal, const log4cplus::tstring& msg) const; 00160 00165 void closeNestedAppenders() const; 00166 00173 bool isEnabledFor(LogLevel ll) const; 00174 00178 void log(LogLevel ll, const log4cplus::tstring& message, 00179 const char* file=NULL, int line=-1, 00180 const char* function=NULL) const; 00181 00186 void forcedLog(LogLevel ll, const log4cplus::tstring& message, 00187 const char* file=NULL, int line=-1, 00188 const char* function=NULL) const; 00189 00201 void callAppenders(const spi::InternalLoggingEvent& event) const; 00202 00211 LogLevel getChainedLogLevel() const; 00212 00218 LogLevel getLogLevel() const; 00219 00223 void setLogLevel(LogLevel ll); 00224 00229 Hierarchy& getHierarchy() const; 00230 00234 log4cplus::tstring getName() const; 00235 00239 bool getAdditivity() const; 00240 00244 void setAdditivity(bool additive); 00245 00246 // AppenderAttachable Methods 00247 virtual void addAppender(SharedAppenderPtr newAppender); 00248 00249 virtual SharedAppenderPtrList getAllAppenders(); 00250 00251 virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name); 00252 00253 virtual void removeAllAppenders(); 00254 00255 virtual void removeAppender(SharedAppenderPtr appender); 00256 00257 virtual void removeAppender(const log4cplus::tstring& name); 00258 00259 Logger (); 00260 Logger(const Logger& rhs); 00261 Logger& operator=(const Logger& rhs); 00262 virtual ~Logger(); 00263 00264 void swap (Logger &); 00265 00270 Logger getParent() const; 00271 00272 protected: 00273 // Data 00275 spi::LoggerImpl * value; 00276 00277 private: 00278 // Ctors 00288 Logger(spi::LoggerImpl * ptr); 00289 00290 // Friends 00291 friend class log4cplus::spi::LoggerImpl; 00292 friend class log4cplus::Hierarchy; 00293 friend class log4cplus::HierarchyLocker; 00294 friend class log4cplus::DefaultLoggerFactory; 00295 }; 00296 00297 00302 class LOG4CPLUS_EXPORT DefaultLoggerFactory : public spi::LoggerFactory { 00303 public: 00304 Logger makeNewLoggerInstance(const log4cplus::tstring& name, Hierarchy& h); 00305 }; 00306 00307 00308 00319 class TraceLogger 00320 { 00321 public: 00322 TraceLogger(const Logger& l, const log4cplus::tstring& _msg, 00323 const char* _file=NULL, int _line=-1) 00324 : logger(l), msg(_msg), file(_file), line(_line) 00325 { if(logger.isEnabledFor(TRACE_LOG_LEVEL)) 00326 logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("ENTER: ") + msg, file, line); 00327 } 00328 00329 ~TraceLogger() 00330 { if(logger.isEnabledFor(TRACE_LOG_LEVEL)) 00331 logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("EXIT: ") + msg, file, line); 00332 } 00333 00334 private: 00335 TraceLogger(const TraceLogger&); 00336 TraceLogger& operator=(const TraceLogger&); 00337 Logger logger; 00338 log4cplus::tstring msg; 00339 const char* file; 00340 int line; 00341 }; 00342 00343 } // end namespace log4cplus 00344 00345 00346 #include "dcmtk/oflog/logmacro.h" 00347 00348 00349 #endif // _LOG4CPLUS_LOGGERHEADER_ 00350