logger.h

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Thu Sep 13 2007
00011    copyright            : (C) 2007 by Ewald Arnold
00012    email                : log4sendpp at ewald-arnold dot de
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU Lesser General Public License as
00016    published by the Free Software Foundation; either version 2 of the License,
00017    or (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU Lesser General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027 
00028  **/
00029 
00030 #ifndef LOG4SENDPP_LOGGER_H
00031 #define LOG4SENDPP_LOGGER_H
00032 
00033 #include <log4sendpp/log4sendpp.h> // always first
00034 
00035 #include <log4sendpp/locationinfo.h>
00036 #include <log4sendpp/threaddict.h>
00037 #include <log4sendpp/ndc.h>
00038 #include <log4sendpp/mdc.h>
00039 #include <log4sendpp/property.h>
00040 
00041 
00042 LOG4SENDPP_NS_START
00043 
00044 
00045 class Appender;
00046 
00049 class LOG4SENDPP_API_DECL0 Logger
00050 {
00051   public:
00052 
00056     struct DiagnosticInformation
00057     {
00060       DiagnosticInformation(const MDC      *in_mdc,
00061                             const NDC      *in_ndc,
00062                             const Property *in_property)
00063       : mdc(in_mdc)
00064       , ndc(in_ndc)
00065       , property(in_property)
00066       {}
00067 
00068       const MDC      *mdc;         
00069       const NDC      *ndc;         
00070       const Property *property;    
00071     };
00072 
00076    enum Level
00077    {
00078       All       = 00,  
00079       Trace     = 10,  
00080       Debug     = 20,  
00081       Info      = 30,  
00082       Warning   = 40,  
00083       Error     = 50,  
00084       Fatal     = 60,  
00085       OFF       = 90   
00086    };
00087 
00090     Logger();
00091 
00094     virtual ~Logger();
00095 
00099     static Logger & logger();
00100 
00108     static void initLogging(const LOG4SENDPP_STD_NS::string &appname, const LOG4SENDPP_STD_NS::string &hostname);
00109 
00114     static void initLogging(Appender *prim_app, Appender *sec_app);
00115 
00122     void setPrimaryAppender(Appender *app);
00123 
00127     Appender *getPrimaryAppender() const;
00128 
00133     void setSecondaryAppender(Appender *app);
00134 
00138     Appender *getSecondaryAppender() const;
00139 
00145     void setLevel (Level level);
00146 
00150     Level getLevel () const;
00151 
00156     bool isLogged (Level level) const;
00157 
00162     bool isLocationInfoEnabled() const;
00163 
00168     void setLocationInfoEnabled(bool enable);
00169 
00179     void log (Level level,
00180               LOG4SENDPP_STD_NS::string msg,
00181               LOG4SENDPP_STD_NS::string category = "",
00182               const LocationInformation *info = 0);
00183 
00189     void trace (LOG4SENDPP_STD_NS::string msg,
00190                 LOG4SENDPP_STD_NS::string category = "",
00191                 const LocationInformation *info = 0);
00192 
00198     void debug (LOG4SENDPP_STD_NS::string msg,
00199                 LOG4SENDPP_STD_NS::string category = "",
00200                 const LocationInformation *info = 0);
00201 
00207     void info (LOG4SENDPP_STD_NS::string msg,
00208                LOG4SENDPP_STD_NS::string category = "",
00209                const LocationInformation *info = 0);
00210 
00216     void warn (LOG4SENDPP_STD_NS::string msg,
00217                LOG4SENDPP_STD_NS::string category = "",
00218                const LocationInformation *info = 0);
00219 
00225     void error (LOG4SENDPP_STD_NS::string msg,
00226                 LOG4SENDPP_STD_NS::string category = "",
00227                 const LocationInformation *info = 0);
00228 
00234     void fatal (LOG4SENDPP_STD_NS::string msg,
00235                 LOG4SENDPP_STD_NS::string category = "",
00236                 const LocationInformation *info = 0);
00237 
00241     NDC& ndc();
00242 
00246     MDC& mdc();
00247 
00251     Property& property();
00252 
00257     static LOG4SENDPP_INT64 currentTimeMillis();
00258 
00259   private:
00260 
00261     static Logger default_logger;
00262     static bool   init_logger;
00263 
00264     Property     logproperty;
00265     MDC          logmdc;
00266     NDC          logndc;
00267     Level        current_level;
00268     bool         loc_info;
00269 
00270     Appender    *primary_appender;
00271     Appender    *secondary_appender;
00272     DiagnosticInformation  diaginfo;
00273 };
00274 
00275 
00276 LOG4SENDPP_NS_END
00277 
00278 
00279 #define LOG4SENDPP_MACRO_LI(message, category, level) \
00280   do { if (LOG4SENDPP_NS::Logger::logger().isLogged(level)) \
00281     { \
00282        if (LOG4SENDPP_NS::Logger::logger().isLocationInfoEnabled()) { \
00283          const LOG4SENDPP_STD_NS::string filename__ = __FILE__; \
00284          const unsigned line__ = __LINE__; \
00285          const LOG4SENDPP_STD_NS::string methodname__ = LOG4SENDPP_FUNC_MACRO ; \
00286          const LOG4SENDPP_STD_NS::string threadname__ = LOG4SENDPP_NS::ThreadDictionary::getCurrentNickname(); \
00287          const LOG4SENDPP_NS::LocationInformation info__(filename__, line__, methodname__, threadname__); \
00288          LOG4SENDPP_NS::Logger::logger().log(level, message, category, &info__); \
00289        } \
00290        else \
00291          LOG4SENDPP_NS::Logger::logger().log(level, message, category, 0); \
00292     } \
00293   } while (false)
00294 
00295 
00296 #define LOG4SENDPP_MACRO_TI(message, throwable, level) \
00297   do { if (LOG4SENDPP_NS::Logger::logger().isLogged(level)) \
00298     { \
00299        if (LOG4SENDPP_NS::Logger::logger().isLocationInfoEnabled()) { \
00300          const LOG4SENDPP_STD_NS::string filename__ = __FILE__; \
00301          const unsigned line__ = __LINE__; \
00302          const LOG4SENDPP_STD_NS::string methodname__ = LOG4SENDPP_FUNC_MACRO ; \
00303          const LOG4SENDPP_STD_NS::string threadname__ = LOG4SENDPP_NS::ThreadDictionary::getCurrentNickname(); \
00304          const LOG4SENDPP_STD_NS::string throwable__ = throwable; \
00305          const LOG4SENDPP_NS::LocationInformation info__(filename__, line__, methodname__, threadname__, throwable__); \
00306          LOG4SENDPP_NS::Logger::logger().log(level, message, "", &info__); \
00307        } \
00308        else \
00309          LOG4SENDPP_NS::Logger::logger().log(level, message, "", 0); \
00310     } \
00311   } while (false)
00312 
00313 
00314 #define LOG4SENDPP_MACRO_LTI(message, category, throwable, level) \
00315   do { if (LOG4SENDPP_NS::Logger::logger().isLogged(level)) \
00316     { \
00317        if (LOG4SENDPP_NS::Logger::logger().isLocationInfoEnabled()) { \
00318          const LOG4SENDPP_STD_NS::string filename__ = __FILE__; \
00319          const unsigned line__ = __LINE__; \
00320          const LOG4SENDPP_STD_NS::string methodname__ = LOG4SENDPP_FUNC_MACRO ; \
00321          const LOG4SENDPP_STD_NS::string threadname__ = LOG4SENDPP_NS::ThreadDictionary::getCurrentNickname(); \
00322          const LOG4SENDPP_STD_NS::string throwable__ = throwable; \
00323          const LOG4SENDPP_NS::LocationInformation info__(filename__, line__, methodname__, threadname__, throwable__); \
00324          LOG4SENDPP_NS::Logger::logger().log(level, message, category, &info__); \
00325        } \
00326        else \
00327          LOG4SENDPP_NS::Logger::logger().log(level, message, category, 0); \
00328     } \
00329   } while (false)
00330 
00331 
00332 #define LOG4SENDPP_MACRO(message, category, level) \
00333   do { if (LOG4SENDPP_NS::Logger::logger().isLogged(level)) \
00334     { \
00335       LOG4SENDPP_NS::Logger::logger().log(level, message, category); \
00336     } \
00337   }  while (false)
00338 
00339 
00341 
00342 
00343 #define L4SND_INFO(x)       LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Info)
00344 #define L4SND_WARN(x)       LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Warning)
00345 #define L4SND_ERROR(x)      LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Error)
00346 #define L4SND_FATAL(x)      LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Fatal)
00347 
00348 
00349 #ifdef NDEBUG
00350 
00351 #define L4SND_TRACE          (void* (0))
00352 #define L4SND_DEBUG(x)       (void* (0))
00353 
00354 #else
00355 
00356 #define L4SND_TRACE(x)       LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Trace)
00357 #define L4SND_DEBUG(x)       LOG4SENDPP_MACRO_LI(x, "", LOG4SENDPP_NS::Logger::Debug)
00358 
00359 #endif // NDEBUG
00360 
00361 
00362 #define LOG4SENDPP_TRACE(x)   L4SND_TRACE(x)
00363 #define LOG4SENDPP_DEBUG(x)   L4SND_DEBUG(x)
00364 #define LOG4SENDPP_INFO(x)    L4SND_INFO(x)
00365 #define LOG4SENDPP_WARNING(x) L4SND_WARN(x)
00366 #define LOG4SENDPP_ERROR(x)   L4SND_ERROR(x)
00367 #define LOG4SENDPP_FATAL(x)   L4SND_FATAL(x)
00368 
00369 
00471 #endif // LOG4SENDPP_LOGGER_H
00472 

Generated on Sat Nov 24 14:41:22 2007 for log4sendpp by  doxygen 1.5.3