H:/gfcppsancout/product/include/gfcpp/Log.hpp

Go to the documentation of this file.
00001 #ifndef _GEMFIRE_LOG_HPP_
00002 #define _GEMFIRE_LOG_HPP_
00003 
00004 
00005 /*=========================================================================
00006  * (c) Copyright 2004-2007, GemStone Systems, Inc. All Rights Reserved.
00007  * 1260 NW Waterhouse Ave., Suite 200,  Beaverton, OR 97006
00008  *=========================================================================
00009  */
00010 
00017 #include "gfcpp_globals.hpp"
00018 #include <stdio.h>
00019 #include <stdarg.h>
00020 
00021 /******************************************************************************/
00022 
00023 
00024 #ifndef GEMFIRE_HIGHEST_LOG_LEVEL
00025   #define GEMFIRE_HIGHEST_LOG_LEVEL             All
00026 #endif
00027 
00028 
00029 #ifndef GEMFIRE_MAX_LOG_FILE_LIMIT
00030   #define GEMFIRE_MAX_LOG_FILE_LIMIT            (1024 * 1024 * 1024)
00031 #endif
00032 
00033 #ifndef GEMFIRE_MAX_LOG_DISK_LIMIT
00034 #define GEMFIRE_MAX_LOG_DISK_LIMIT              (1024ll * 1024ll * 1024ll * 1024ll)
00035 #endif
00036 
00037 #define _GF_MSG_LIMIT                           8192
00038 
00039 /******************************************************************************/
00040 
00044 namespace gemfire {
00045 
00046 class Exception;
00047 
00048 /******************************************************************************/
00049 /******************************************************************************/
00050 
00051 /* Logs the message if the given level is less than or equal to the current logging level. */
00052 #define GF_LOG(level,expr) if ( level > gemfire::Log::logLevel() ) { } else gemfire::Log::log(level, expr)
00053 
00054 
00055 
00133 class CPPCACHE_EXPORT Log {
00134 
00135 public:
00136 
00137 
00138   /******/
00139 
00140 
00141   enum LogLevel {
00142 
00143     // NOTE: if you change this enum declaration at all, be sure to
00144     // change levelToChars and charsToLevel functions!
00145 
00146     None,
00147 
00148     Error,
00149     Warning,
00150     Info,
00151 
00152     Default,
00153 
00154     Config,
00155 
00156     Fine,
00157     Finer,
00158     Finest,
00159 
00160     Debug,
00161 
00162     All
00163 
00164   };
00165 
00166 
00167   /******/
00168 
00169 
00173   static LogLevel logLevel()
00174     { return s_logLevel; }
00175 
00179   static void setLogLevel( LogLevel level )
00180     { s_logLevel = level; }
00181 
00187   static const char* logFileName();
00188 
00189 
00196   static void init
00197     // 0 => use maximum value (currently 1G)
00198     (LogLevel level, const char* logFileName, int32 logFileLimit = 0, int64 logDiskSpaceLimit = 0);
00199 
00200 
00204   static void close();
00205 
00206 
00213   static const char* levelToChars(Log::LogLevel level);
00214 
00215 
00221   static LogLevel charsToLevel(const char* chars);
00222 
00223 
00238   static char* formatLogLine(char* buf, LogLevel level);
00239 
00240   /******/
00241 
00242 
00246   static bool enabled(LogLevel level)
00247   {
00248     return
00249       (((s_doingDebug && level == Debug) ||
00250         GEMFIRE_HIGHEST_LOG_LEVEL >= level) && s_logLevel >= level);
00251   }
00252 
00253 
00257   static void log(LogLevel level, const char* msg)
00258   {
00259     if (enabled(level))
00260       put(level, msg);
00261   }
00262 
00263 
00267   static void logThrow(LogLevel level, const char* msg, const Exception& ex)
00268   {
00269     if (enabled(level))
00270       putThrow(level, msg, ex);
00271   }
00272 
00273 
00277   static void logCatch(LogLevel level, const char* msg, const Exception& ex)
00278   {
00279     if (enabled(level))
00280       putCatch(level, msg, ex);
00281   }
00282 
00283 
00284   /******/
00285 
00286 
00290   static bool errorEnabled()
00291   {
00292     return GEMFIRE_HIGHEST_LOG_LEVEL >= Error && s_logLevel >= Error;
00293   }
00294 
00295 
00300   static void error(const char* msg)
00301   {
00302     if (errorEnabled())
00303       put(Error, msg);
00304   }
00305 
00306 
00311   static void errorThrow(const char* msg, const Exception& ex)
00312   {
00313     if (errorEnabled())
00314       putThrow(Error, msg, ex);
00315   }
00316 
00321   static void errorCatch(const char* msg, const Exception& ex)
00322   {
00323     if (errorEnabled())
00324       putCatch(Error, msg, ex);
00325   }
00326 
00327 
00328   /******/
00329 
00330 
00334   static bool warningEnabled()
00335   {
00336     return GEMFIRE_HIGHEST_LOG_LEVEL >= Warning && s_logLevel >= Warning;
00337   }
00338 
00339 
00344   static void warning(const char* msg)
00345   {
00346     if (warningEnabled())
00347       put(Warning, msg);
00348   }
00349 
00350 
00355   static void warningThrow(const char* msg, const Exception& ex)
00356   {
00357     if (warningEnabled())
00358       putThrow(Warning, msg, ex);
00359   }
00360 
00361 
00366   static void warningCatch(const char* msg, const Exception& ex)
00367   {
00368     if (warningEnabled())
00369       putCatch(Warning, msg, ex);
00370   }
00371 
00372 
00373   /******/
00374 
00375 
00379   static bool infoEnabled()
00380   {
00381     return GEMFIRE_HIGHEST_LOG_LEVEL >= Info && s_logLevel >= Info;
00382   }
00383 
00384 
00389   static void info(const char* msg)
00390   {
00391     if (infoEnabled())
00392       put(Info, msg);
00393   }
00394 
00395 
00400   static void infoThrow(const char* msg, const Exception& ex)
00401   {
00402     if (infoEnabled())
00403       putThrow(Info, msg, ex);
00404   }
00405 
00406 
00411   static void infoCatch(const char* msg, const Exception& ex)
00412   {
00413     if (infoEnabled())
00414       putCatch(Info, msg, ex);
00415   }
00416 
00417 
00418   /******/
00419 
00420 
00424   static bool configEnabled()
00425   {
00426     return GEMFIRE_HIGHEST_LOG_LEVEL >= Config && s_logLevel >= Config;
00427   }
00428 
00429 
00434   static void config(const char* msg)
00435   {
00436     if (configEnabled())
00437       put(Config, msg);
00438   }
00439 
00440 
00445   static void configThrow(const char* msg, const Exception& ex)
00446   {
00447     if (configEnabled())
00448       putThrow(Config, msg, ex);
00449   }
00450 
00451 
00456   static void configCatch(const char* msg, const Exception& ex)
00457   {
00458     if (configEnabled())
00459       putCatch(Config, msg, ex);
00460   }
00461 
00462 
00463   /******/
00464 
00465 
00469   static bool fineEnabled()
00470   {
00471     return GEMFIRE_HIGHEST_LOG_LEVEL >= Fine && s_logLevel >= Fine;
00472   }
00473 
00474 
00479   static void fine(const char* msg)
00480   {
00481     if (fineEnabled())
00482       put(Fine, msg);
00483   }
00484 
00485 
00490   static void fineThrow(const char* msg, const Exception& ex)
00491   {
00492     if (fineEnabled())
00493       putThrow(Fine, msg, ex);
00494   }
00495 
00496 
00501   static void fineCatch(const char* msg, const Exception& ex)
00502   {
00503     if (fineEnabled())
00504       putCatch(Fine, msg, ex);
00505   }
00506 
00507 
00508   /******/
00509 
00510 
00514   static bool finerEnabled()
00515   {
00516     return GEMFIRE_HIGHEST_LOG_LEVEL >= Finer && s_logLevel >= Finer;
00517   }
00518 
00519 
00524   static void finer(const char* msg)
00525   {
00526     if (finerEnabled())
00527       put(Finer, msg);
00528   }
00529 
00530 
00535   static void finerThrow(const char* msg, const Exception& ex)
00536   {
00537     if (finerEnabled())
00538       putThrow(Finer, msg, ex);
00539   }
00540 
00541 
00546   static void finerCatch(const char* msg, const Exception& ex)
00547   {
00548     if (finerEnabled())
00549       putCatch(Finer, msg, ex);
00550   }
00551 
00552 
00553   /******/
00554 
00555 
00559   static bool finestEnabled()
00560   {
00561     return GEMFIRE_HIGHEST_LOG_LEVEL >= Finest && s_logLevel >= Finest;
00562   }
00563 
00564 
00569   static void finest(const char* msg)
00570   {
00571     if (finestEnabled())
00572       put(Finest, msg);
00573   }
00574 
00575 
00580   static void finestThrow(const char* msg, const Exception& ex)
00581   {
00582     if (finestEnabled())
00583       putThrow(Finest, msg, ex);
00584   }
00585 
00586 
00591   static void finestCatch(const char* msg, const Exception& ex)
00592   {
00593     if (finestEnabled())
00594       putCatch(Finest, msg, ex);
00595   }
00596 
00597 
00598   /******/
00599 
00600 
00604   static bool debugEnabled()
00605   {
00606     return (s_doingDebug || GEMFIRE_HIGHEST_LOG_LEVEL >= Debug) &&
00607       s_logLevel >= Debug;
00608   }
00609 
00610 
00615   static void debug(const char* msg)
00616   {
00617     if (debugEnabled())
00618       put(Debug, msg);
00619   }
00620 
00621 
00626   static void debugThrow(const char* msg, const Exception& ex)
00627   {
00628     if (debugEnabled())
00629       putThrow(Debug, msg, ex);
00630   }
00631 
00632 
00637   static void debugCatch(const char* msg, const Exception& ex)
00638   {
00639     if (debugEnabled())
00640       putCatch(Debug, msg, ex);
00641   }
00642 
00643 
00644   /******/
00645 
00646 
00647   static void enterFn(LogLevel level, const char* functionName);
00648 
00649 
00650   static void exitFn(LogLevel level, const char* functionName);
00651 
00652 
00653 
00654   /******/
00655 
00656 private:
00657 
00658 
00659   static LogLevel s_logLevel;
00660 
00661 
00662   /******/
00663 
00664 #ifdef DEBUG
00665   enum { s_doingDebug = 1 };
00666 #else
00667   enum { s_doingDebug = 0 };
00668 #endif
00669 
00670 
00671   /******/
00672 
00673 
00674   static void writeBanner();
00675 
00676 
00677   /******/
00678 public:
00679 
00680   static void put(LogLevel level, const char* msg);
00681 
00682   static void putThrow
00683     (LogLevel level, const char* msg, const Exception& ex);
00684 
00685   static void putCatch
00686     (LogLevel level, const char* msg, const Exception& ex);
00687 
00688 };
00689 
00690 
00691 /******************************************************************************/
00692 /******************************************************************************/
00693 
00694 
00695 class LogFn {
00696 
00697   const char* m_functionName;
00698   Log::LogLevel m_level;
00699 
00700 public:
00701 
00702   LogFn(const char* functionName, Log::LogLevel level = Log::Finest)
00703     : m_functionName(functionName), m_level(level)
00704   {
00705     if (Log::enabled(m_level))
00706       Log::enterFn(m_level, m_functionName);
00707   }
00708 
00709   ~LogFn()
00710   {
00711     if (Log::enabled(m_level))
00712       Log::exitFn(m_level, m_functionName);
00713   }
00714 
00715 private:
00716   LogFn(const LogFn& rhs); // never defined
00717   void operator = (const LogFn& rhs); // never defined
00718 };
00719 
00720 /******************************************************************************/
00721 /******************************************************************************/
00722 
00727 class CPPCACHE_EXPORT LogVarargs
00728 {
00729   public:
00730 
00731   static void debug( const char* fmt, ...);
00732   static void error( const char* fmt, ...);
00733   static void warn( const char* fmt, ...);
00734   static void info( const char* fmt, ...);
00735   static void config( const char* fmt, ...);
00736   static void fine( const char* fmt, ...);
00737   static void finer( const char* fmt, ...);
00738   static void finest( const char* fmt, ...);
00739 
00740 };
00741 
00742 }
00743 
00744 /************************ LOGDEBUG ***********************************/
00745 
00746 #define LOGDEBUG if ( gemfire::Log::Debug <= gemfire::Log::logLevel() ) gemfire::LogVarargs::debug
00747 
00748 /************************ LOGERROR ***********************************/
00749 
00750 #define LOGERROR if ( gemfire::Log::Error <= gemfire::Log::logLevel() ) gemfire::LogVarargs::error
00751 
00752 /************************ LOGWARN ***********************************/
00753 
00754 #define LOGWARN if ( gemfire::Log::Warning <= gemfire::Log::logLevel() ) gemfire::LogVarargs::warn
00755 
00756 /************************ LOGINFO ***********************************/
00757 
00758 #define LOGINFO if ( gemfire::Log::Info <= gemfire::Log::logLevel() ) gemfire::LogVarargs::info
00759 
00760 /************************ LOGCONFIG ***********************************/
00761 
00762 #define LOGCONFIG if ( gemfire::Log::Config <= gemfire::Log::logLevel() ) gemfire::LogVarargs::config
00763 
00764 /************************ LOGFINE ***********************************/
00765 
00766 #define LOGFINE if ( gemfire::Log::Fine <= gemfire::Log::logLevel() ) gemfire::LogVarargs::fine
00767 
00768 /************************ LOGFINER ***********************************/
00769 
00770 #define LOGFINER if ( gemfire::Log::Finer <= gemfire::Log::logLevel() ) gemfire::LogVarargs::finer
00771 
00772 /************************ LOGFINEST ***********************************/
00773 
00774 #define LOGFINEST if ( gemfire::Log::Finest <= gemfire::Log::logLevel() ) gemfire::LogVarargs::finest
00775 
00776 /******************************************************************************/
00777 
00778 
00779 /******************************************************************************/
00780 
00781 
00782 #endif
00783 
00784 
00785 /******************************************************************************/

GemFire C++ Cache API Documentation