csync_log.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 #ifndef _CSYNC_LOG_H
00035 #define _CSYNC_LOG_H
00036 
00037 #include "config.h"
00038 
00039 #ifdef CSYNC_TEST
00040 #undef WITH_LOG4C
00041 #endif
00042 
00043 #ifdef WITH_LOG4C
00044 #include "log4c.h"
00045 #else
00046 #include <stdarg.h>
00047 #include <stdio.h>
00048 #endif
00049 
00050 #ifndef CSYNC_LOG_CATEGORY_NAME
00051 #define CSYNC_LOG_CATEGORY_NAME "root"
00052 #endif
00053 
00054 
00055 #ifdef __GNUC__
00056 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
00057 #else
00058 #define PRINTF_ATTRIBUTE(a,b)
00059 #endif 
00060 
00061 #define CSYNC_LOG(priority, fmt, rest...) \
00062   csync_log((char *) CSYNC_LOG_CATEGORY_NAME, priority, fmt, ##rest)
00063 
00064 #ifdef WITH_LOG4C
00065 #define CSYNC_LOG_PRIORITY_FATAL   LOG4C_PRIORITY_FATAL
00066 #define CSYNC_LOG_PRIORITY_ALERT   LOG4C_PRIORITY_ALERT
00067 #define CSYNC_LOG_PRIORITY_CRIT    LOG4C_PRIORITY_CRIT
00068 #define CSYNC_LOG_PRIORITY_ERROR   LOG4C_PRIORITY_ERROR
00069 #define CSYNC_LOG_PRIORITY_WARN    LOG4C_PRIORITY_WARN
00070 #define CSYNC_LOG_PRIORITY_NOTICE  LOG4C_PRIORITY_NOTICE
00071 #define CSYNC_LOG_PRIORITY_INFO    LOG4C_PRIORITY_INFO
00072 #define CSYNC_LOG_PRIORITY_DEBUG   LOG4C_PRIORITY_DEBUG
00073 #define CSYNC_LOG_PRIORITY_TRACE   LOG4C_PRIORITY_TRACE
00074 #define CSYNC_LOG_PRIORITY_NOTSET  LOG4C_PRIORITY_NOTSET
00075 #define CSYNC_LOG_PRIORITY_UNKNOWN LOG4C_PRIORITY_UNKNOWN
00076 #else
00077 #define LOG4C_INLINE inline
00078 #define CSYNC_LOG_PRIORITY_FATAL   000
00079 #define CSYNC_LOG_PRIORITY_ALERT   100
00080 #define CSYNC_LOG_PRIORITY_CRIT    200
00081 #define CSYNC_LOG_PRIORITY_ERROR   300
00082 #define CSYNC_LOG_PRIORITY_WARN    500
00083 #define CSYNC_LOG_PRIORITY_NOTICE  500
00084 #define CSYNC_LOG_PRIORITY_INFO    600
00085 #define CSYNC_LOG_PRIORITY_DEBUG   700
00086 #define CSYNC_LOG_PRIORITY_TRACE   800
00087 #define CSYNC_LOG_PRIORITY_NOTSET  900
00088 #define CSYNC_LOG_PRIORITY_UNKNOWN 1000
00089 #endif
00090 
00091 
00092 
00093 
00094 
00095 
00096 static LOG4C_INLINE int csync_log_init() {
00097 #ifdef WITH_LOG4C
00098   return (log4c_init());
00099 #else
00100   return 0;
00101 #endif
00102 }
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 static LOG4C_INLINE int csync_log_load(const char *path){
00112 #ifdef WITH_LOG4C
00113   return (log4c_load(path));
00114 #else
00115   if (path == NULL) {
00116     return 0;
00117   }
00118   return 0;
00119 #endif
00120 }
00121 
00122 
00123 
00124 
00125 
00126 
00127 static LOG4C_INLINE int csync_log_fini(){
00128 #ifdef WITH_LOG4C
00129   return(log4c_fini());
00130 #else
00131   return 0;
00132 #endif
00133 }
00134 
00135 static LOG4C_INLINE int csync_log_setappender(char *catName, char *appName) {
00136 #ifdef WITH_LOG4C
00137   log4c_category_set_appender(log4c_category_get(catName),log4c_appender_get(appName));
00138   return 0;
00139 #else
00140   if (catName == NULL || appName == NULL) {
00141     return 0;
00142   }
00143   return 0;
00144 #endif
00145 }
00146 
00147 static LOG4C_INLINE void csync_log(char *catName, int a_priority, const char* a_format,...) PRINTF_ATTRIBUTE(3, 4);
00148 static LOG4C_INLINE void csync_log(char *catName, int a_priority, const char* a_format,...) {
00149 #ifdef WITH_LOG4C
00150   const log4c_category_t* a_category = log4c_category_get(catName);
00151   if (log4c_category_is_priority_enabled(a_category, a_priority)) {
00152     va_list va;
00153     va_start(va, a_format);
00154     log4c_category_vlog(a_category, a_priority, a_format, va);
00155     va_end(va);
00156   }
00157 #else
00158   va_list va;
00159   va_start(va, a_format);
00160   if (a_priority > 0) {
00161     printf("%s - ", catName);
00162   }
00163   vprintf(a_format, va);
00164   va_end(va);
00165   printf("\n");
00166 #endif
00167 }
00168 
00169 
00170 
00171 
00172 #endif 
00173