csync_log.h

Go to the documentation of this file.
00001 /*
00002  * libcsync -- a library to sync a directory with another
00003  *
00004  * Copyright (c) 2006 by Andreas Schneider <mail@cynapses.org>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 /**
00022  * @file csync_log.h
00023  *
00024  * @brief Logging interface of csync
00025  *
00026  * @defgroup csyncLogInternals csync logging internals
00027  * @ingroup csyncInternalAPI
00028  *
00029  * @{
00030  */
00031 
00032 #ifndef _CSYNC_LOG_H
00033 #define _CSYNC_LOG_H
00034 
00035 #include "config.h"
00036 
00037 #ifdef CSYNC_TEST
00038 #undef WITH_LOG4C
00039 #endif
00040 
00041 #ifdef WITH_LOG4C
00042 #include "log4c.h"
00043 #else
00044 #include <stdarg.h>
00045 #include <stdio.h>
00046 #endif
00047 
00048 #ifndef CSYNC_LOG_CATEGORY_NAME
00049 #define CSYNC_LOG_CATEGORY_NAME "root"
00050 #endif
00051 
00052 /* GCC have printf type attribute check.  */
00053 #ifdef __GNUC__
00054 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
00055 #else
00056 #define PRINTF_ATTRIBUTE(a,b)
00057 #endif /* __GNUC__ */
00058 
00059 /*#define CSYNC_LOG(priority, fmt, ...) \
00060   csync_log((char *) CSYNC_LOG_CATEGORY_NAME, priority, fmt, ## __VA_ARGS__)*/
00061 /*
00062  * fix hundreds of these warnings:
00063  * csync.c:272:75: warning: ISO C99 requires rest arguments to be used [enabled by default]
00064  * The ## token in combination with __VA_ARGS__ is a gcc extension that's not part of ISO C99
00065  * http://stackoverflow.com/questions/4100746/suppressing-iso-c99-requires-rest-arguments-to-be-used
00066  */
00067 #ifdef LOG_TO_CALLBACK
00068 #define CSYNC_LOG(priority, ...) \
00069   csync_log_cb((char *) CSYNC_LOG_CATEGORY_NAME, priority,  __VA_ARGS__)
00070 #else
00071 #define CSYNC_LOG(priority, ...) \
00072   csync_log((char *) CSYNC_LOG_CATEGORY_NAME, priority,  __VA_ARGS__)
00073 #endif
00074 
00075 #ifdef WITH_LOG4C
00076 #define CSYNC_LOG_PRIORITY_FATAL   LOG4C_PRIORITY_FATAL
00077 #define CSYNC_LOG_PRIORITY_ALERT   LOG4C_PRIORITY_ALERT
00078 #define CSYNC_LOG_PRIORITY_CRIT    LOG4C_PRIORITY_CRIT
00079 #define CSYNC_LOG_PRIORITY_ERROR   LOG4C_PRIORITY_ERROR
00080 #define CSYNC_LOG_PRIORITY_WARN    LOG4C_PRIORITY_WARN
00081 #define CSYNC_LOG_PRIORITY_NOTICE  LOG4C_PRIORITY_NOTICE
00082 #define CSYNC_LOG_PRIORITY_INFO    LOG4C_PRIORITY_INFO
00083 #define CSYNC_LOG_PRIORITY_DEBUG   LOG4C_PRIORITY_DEBUG
00084 #define CSYNC_LOG_PRIORITY_TRACE   LOG4C_PRIORITY_TRACE
00085 #define CSYNC_LOG_PRIORITY_NOTSET  LOG4C_PRIORITY_NOTSET
00086 #define CSYNC_LOG_PRIORITY_UNKNOWN LOG4C_PRIORITY_UNKNOWN
00087 #else
00088 #define LOG4C_INLINE inline
00089 #define CSYNC_LOG_PRIORITY_FATAL   000
00090 #define CSYNC_LOG_PRIORITY_ALERT   100
00091 #define CSYNC_LOG_PRIORITY_CRIT    200
00092 #define CSYNC_LOG_PRIORITY_ERROR   300
00093 #define CSYNC_LOG_PRIORITY_WARN    500
00094 #define CSYNC_LOG_PRIORITY_NOTICE  500
00095 #define CSYNC_LOG_PRIORITY_INFO    600
00096 #define CSYNC_LOG_PRIORITY_DEBUG   700
00097 #define CSYNC_LOG_PRIORITY_TRACE   800
00098 #define CSYNC_LOG_PRIORITY_NOTSET  900
00099 #define CSYNC_LOG_PRIORITY_UNKNOWN 1000
00100 #endif
00101 
00102 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
00103     const char* a_format,...) PRINTF_ATTRIBUTE(3, 4);
00104 /**
00105  * @brief The constructor of the logging mechanism
00106  *
00107  * @return  0 on success, less than 0 if an error occured.
00108  */
00109 static LOG4C_INLINE int csync_log_init() {
00110 #ifdef WITH_LOG4C
00111   return log4c_init();
00112 #else
00113   return 0;
00114 #endif
00115 }
00116 
00117 /**
00118  * @brief Load resource configuration file
00119  *
00120  * @param Path to the file to load
00121  *
00122  * @return  0 on success, less than 0 if an error occured.
00123  **/
00124 static LOG4C_INLINE int csync_log_load(const char *path){
00125 #ifdef WITH_LOG4C
00126   return log4c_load(path);
00127 #else
00128   if (path == NULL) {
00129     return 0;
00130   }
00131   return 0;
00132 #endif
00133 }
00134 
00135 /**
00136  * @brief The destructor of the logging mechanism
00137  *
00138  * @return  0 on success, less than 0 if an error occured.
00139  */
00140 static LOG4C_INLINE int csync_log_fini(){
00141 #ifdef WITH_LOG4C
00142   return log4c_fini();
00143 #else
00144   return 0;
00145 #endif
00146 }
00147 
00148 static LOG4C_INLINE int csync_log_setappender(char *catName, char *appName) {
00149 #ifdef WITH_LOG4C
00150   log4c_category_set_appender(log4c_category_get(catName),
00151       log4c_appender_get(appName));
00152   return 0;
00153 #else
00154   if (catName == NULL || appName == NULL) {
00155     return 0;
00156   }
00157   return 0;
00158 #endif
00159 }
00160 
00161 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
00162     const char* a_format,...) {
00163 #ifdef WITH_LOG4C
00164   const log4c_category_t* a_category = log4c_category_get(catName);
00165   if (log4c_category_is_priority_enabled(a_category, a_priority)) {
00166     va_list va;
00167     va_start(va, a_format);
00168     log4c_category_vlog(a_category, a_priority, a_format, va);
00169     va_end(va);
00170   }
00171 #else
00172   /* On Apple, all stderr & stdout go to Apple System Log (ASL) by default.
00173    * Thats certainly too much at the moment.
00174    */
00175 #ifndef __APPLE__
00176   va_list va;
00177   va_start(va, a_format);
00178   if (a_priority > 0) {
00179     printf("%s - ", catName);
00180   }
00181   vprintf(a_format, va);
00182   va_end(va);
00183   printf("\n");
00184 #endif
00185 #endif
00186 }
00187 
00188 /**
00189  * }@
00190  */
00191 #endif /* _CSYNC_LOG_H */
00192 
00193 /* vim: set ft=c.doxygen ts=8 sw=2 et cindent: */

Generated on Mon Feb 25 14:24:39 2013 for doc by  doxygen 1.5.6