liblogger/liblogger.h

Go to the documentation of this file.
00001 /*
00002  * liblogger - copyright 2007, Vineeth Neelakant, nvineeth@gmail.com
00003  * This file is part of liblogger.
00004  *
00005  * liblogger is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * liblogger is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 #ifndef __EXP_LOGGER_H__
00019 #define __EXP_LOGGER_H__
00020 
00021 #ifdef __cplusplus
00022 extern "C"
00023 {
00024 #endif
00025 
00026 #include <liblogger/liblogger_levels.h>
00027 #include <liblogger/liblogger_config.h>
00028 
00029 /** Liblogger major version */
00030 #define LIBLOGGER_MAJOR_VERSION 0
00031 /** Liblogger minor version */
00032 #define LIBLOGGER_MINOR_VERSION 1
00033 
00034 /*If the default log level is not define,
00035  * then the default log level is set to Trace. */
00036 #ifndef LOG_LEVEL
00037 #define LOG_LEVEL LOG_LEVEL_TRACE
00038 #endif
00039 
00040 /* If the module name is not defined, then the default module name. */
00041 #ifndef LOG_MODULE_NAME
00042 #define LOG_MODULE_NAME ""
00043 #endif
00044 
00045 
00046 
00047 /** enum which indicates the log destination, used with \ref InitLogger */
00048 typedef enum LogDest
00049 {
00050         /** Indicates that logging should be done to file. */
00051         LogToFile,
00052         /** Log to Console. (stdout) */
00053         LogToConsole,
00054         /** Indicates that logging should be done to socket. Please note that log server should be 
00055          *  running. */
00056         LogToSocket
00057 }LogDest;
00058 
00059 /* few compilers dont support variadic macros,so initially undef it, 
00060  * and depending on the compiler define it.
00061  */
00062 #undef VARIADIC_MACROS
00063 
00064 #if !defined(DISABLE_VARIADIC_CHECK) && (defined(__GNUC__) || (_MSC_VER >= 1400))
00065         /* 1. GCC supports variadic macros, 
00066          * 2. this feature is available only in VS 2005 or higher versions.
00067          * */
00068         #define VARIADIC_MACROS
00069 #endif
00070 
00071 /* Forcefully enable the variadic macro support, results in compiler errors if not supported. */
00072 #ifdef ASSUME_VARIADIC_SUPPORT
00073         #define VARIADIC_MACROS
00074 #endif  
00075 
00076 
00077 #ifdef DISABLE_ALL_LOGS
00078 #ifdef __GNUC__
00079 // Logs needs to be disabled, warn the user.
00080 #warning Logger disabled.
00081 #endif // __GNUC__
00082 
00083         #define LogTrace                /* NOP */
00084         #define LogDebug                /* NOP */
00085         #define LogInfo                 /* NOP */
00086         #define LogWarn                 /* NOP */
00087         #define LogError                /* NOP */
00088         #define LogFatal                /* NOP */
00089         #define LogFuncEntry()  /* NOP */
00090         #define LogFuncExit()   /* NOP */
00091         #define InitLogger              /* NOP */
00092         #define DeInitLogger()  /* NOP */
00093 #else
00094 
00095         /* WIN32 support. */
00096         #if defined(WIN32) || defined(_WIN32)
00097         #define __func__ __FUNCTION__
00098         #endif
00099 
00100         // The logs are enabled.
00101 
00102         #ifdef VARIADIC_MACROS  
00103         int LogStub_vm(LogLevel logLevel,
00104                 const char* moduleName,const char* file,
00105                 const char* funcName, const int lineNum,
00106                 const char* fmt,...);
00107         #endif
00108         /** 
00109          * Function used to initialize the logger.
00110          * \param [in] ldest The log destination. see \ref LogDest for possible destinations.
00111          * \param [in] loggerInitParams The logger initialization parameters.
00112          * \returns 0 if successful, -1 if there is a failure.
00113          * */
00114         int InitLogger(LogDest ldest,void* loggerInitParams);
00115 
00116         /** Function used to deinitialize the logger. */
00117         void DeInitLogger();
00118 
00119 
00120         /* -- Log Level Trace -- */
00121         #if LOG_LEVEL<= LOG_LEVEL_TRACE
00122                 #ifdef VARIADIC_MACROS
00123                         #if defined(DISABLE_FILENAMES)
00124                                 /* the filename should be disabled. */
00125                                 #define LogTrace(fmt, ...) LogStub_vm(Trace,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00126                         #else 
00127                                 #define LogTrace(fmt, ...) LogStub_vm(Trace,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00128                         #endif // DISABLE_FILENAMES
00129                 #else
00130                         /** Emit a log with Trace level. */
00131                         int LogTrace(const char *fmt, ...);
00132                 #endif // VARIADIC_MACROS
00133         #else
00134                 /* The chosen log level is greater, so this log should be disabled. */
00135                 #define LogTrace        /*NOP*/
00136         #endif
00137 
00138         /* -- Log Level Debug -- */
00139         #if LOG_LEVEL<= LOG_LEVEL_DEBUG
00140                 #ifdef VARIADIC_MACROS
00141                         #if defined(DISABLE_FILENAMES)
00142                                 /* the filename should be disabled. */
00143                                 #define LogDebug(fmt, ...) LogStub_vm(Debug,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00144                         #else 
00145                                 #define LogDebug(fmt, ...) LogStub_vm(Debug,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00146                         #endif // DISABLE_FILENAMES
00147                 #else
00148                         /** Emit a log with Debug level. */
00149                         int LogDebug(const char *fmt, ...);
00150                 #endif // VARIADIC_MACROS
00151         #else
00152                 /* The chosen log level is greater, so this log should be disabled. */
00153                 #define LogDebug        /*NOP*/
00154         #endif
00155 
00156         /* -- Log Level Info -- */
00157         #if LOG_LEVEL<= LOG_LEVEL_INFO
00158                 #ifdef VARIADIC_MACROS
00159                         #if defined(DISABLE_FILENAMES)
00160                                 /* the filename should be disabled. */
00161                                 #define LogInfo(fmt, ...) LogStub_vm(Info,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00162                         #else 
00163                                 #define LogInfo(fmt, ...) LogStub_vm(Info,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00164                         #endif // DISABLE_FILENAMES
00165                 #else
00166                         /** Emit a log with Info level. */
00167                         int LogInfo(const char *fmt, ...);
00168                 #endif // VARIADIC_MACROS
00169         #else
00170                 /* The chosen log level is greater, so this log should be disabled. */
00171                 #define LogInfo /*NOP*/
00172         #endif
00173 
00174         /* -- Log Level Warn -- */
00175         #if LOG_LEVEL<= LOG_LEVEL_WARN
00176                 #ifdef VARIADIC_MACROS
00177                         #if defined(DISABLE_FILENAMES)
00178                                 /* the filename should be disabled. */
00179                                 #define LogWarn(fmt, ...) LogStub_vm(Warn,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00180                         #else 
00181                                 #define LogWarn(fmt, ...) LogStub_vm(Warn,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00182                         #endif // DISABLE_FILENAMES
00183                 #else
00184                         /** Emit a log with Warn level. */
00185                         int LogWarn(const char *fmt, ...);
00186                 #endif // VARIADIC_MACROS
00187         #else
00188                 /* The chosen log level is greater, so this log should be disabled. */
00189                 #define LogWarn /*NOP*/
00190         #endif
00191 
00192         /* -- Log Level Error-- */
00193         #if LOG_LEVEL<= LOG_LEVEL_ERROR
00194                 #ifdef VARIADIC_MACROS
00195                         #if defined(DISABLE_FILENAMES)
00196                                 /* the filename should be disabled. */
00197                                 #define LogError(fmt, ...) LogStub_vm(Error,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00198                         #else 
00199                                 #define LogError(fmt, ...) LogStub_vm(Error,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00200                         #endif // DISABLE_FILENAMES
00201                 #else
00202                         /** Emit a log with Error level. */
00203                         int LogError(const char *fmt, ...);
00204                 #endif // VARIADIC_MACROS
00205         #else
00206                 /* The chosen log level is greater, so this log should be disabled. */
00207                 #define LogError        /*NOP*/
00208         #endif
00209 
00210         /* -- Log Level Fatal -- */
00211         #if LOG_LEVEL<= LOG_LEVEL_FATAL
00212                 #ifdef VARIADIC_MACROS
00213                         #if defined(DISABLE_FILENAMES)
00214                                 /* the filename should be disabled. */
00215                                 #define LogFatal(fmt, ...) LogStub_vm(Fatal,LOG_MODULE_NAME,"",__func__, __LINE__ , fmt , ## __VA_ARGS__)
00216                         #else 
00217                                 #define LogFatal(fmt, ...) LogStub_vm(Fatal,LOG_MODULE_NAME,__FILE__,__func__, __LINE__ , fmt , ## __VA_ARGS__)
00218                         #endif // DISABLE_FILENAMES
00219                 #else
00220                         /** Emit a log with Fatal level. */
00221                         int LogFatal(const char *fmt, ...);
00222                 #endif // VARIADIC_MACROS
00223         #else
00224                 /* The chosen log level is greater, so this log should be disabled. */
00225                 #define LogFatal        /*NOP*/
00226         #endif
00227 
00228         #if LOG_LEVEL<= LOG_LEVEL_TRACE
00229                 /* Log Entry to a function. */
00230                 int FuncLogEntry(const char* funcName);
00231                 /* Log return from a function. */
00232                 int FuncLogExit(const char* funcName,const int lineNumber);
00233                 
00234                 #define LogFuncEntry()  FuncLogEntry(__func__)
00235                 #define LogFuncExit()   FuncLogExit(__func__,__LINE__)
00236                 
00237         #else
00238                 /* The chosen log level is greater, so this log should be disabled. */
00239                 #define LogFuncEntry()  /* NOP */
00240                 #define LogFuncExit()   /* NOP */
00241         #endif
00242 
00243 #endif // DISABLE_ALL_LOGS
00244 
00245 
00246 #ifdef __cplusplus
00247 }
00248 #endif /* __cplusplus */
00249 
00250 #endif /* __EXP_LOGGER_H__ */
00251 

liblogger © 2007 - SourceForge.net Logo