doc
csync_log.h
Go to the documentation of this file.
1 /*
2  * libcsync -- a library to sync a directory with another
3  *
4  * Copyright (c) 2006 by Andreas Schneider <mail@cynapses.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * @file csync_log.h
23  *
24  * @brief Logging interface of csync
25  *
26  * @defgroup csyncLogInternals csync logging internals
27  * @ingroup csyncInternalAPI
28  *
29  * @{
30  */
31 
32 #ifndef _CSYNC_LOG_H
33 #define _CSYNC_LOG_H
34 
35 #include "config.h"
36 
37 #ifdef CSYNC_TEST
38 #undef WITH_LOG4C
39 #endif
40 
41 #ifdef WITH_LOG4C
42 #include "log4c.h"
43 #else
44 #include <stdarg.h>
45 #include <stdio.h>
46 #endif
47 
48 #ifndef CSYNC_LOG_CATEGORY_NAME
49 #define CSYNC_LOG_CATEGORY_NAME "root"
50 #endif
51 
52 /* GCC have printf type attribute check. */
53 #ifdef __GNUC__
54 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
55 #else
56 #define PRINTF_ATTRIBUTE(a,b)
57 #endif /* __GNUC__ */
58 
59 /*#define CSYNC_LOG(priority, fmt, ...) \
60  csync_log((char *) CSYNC_LOG_CATEGORY_NAME, priority, fmt, ## __VA_ARGS__)*/
61 /*
62  * fix hundreds of these warnings:
63  * csync.c:272:75: warning: ISO C99 requires rest arguments to be used [enabled by default]
64  * The ## token in combination with __VA_ARGS__ is a gcc extension that's not part of ISO C99
65  * http://stackoverflow.com/questions/4100746/suppressing-iso-c99-requires-rest-arguments-to-be-used
66  */
67 #ifdef LOG_TO_CALLBACK
68 #define CSYNC_LOG(priority, ...) \
69  csync_log_cb((char *) CSYNC_LOG_CATEGORY_NAME, priority, __VA_ARGS__)
70 #else
71 #define CSYNC_LOG(priority, ...) \
72  csync_log((char *) CSYNC_LOG_CATEGORY_NAME, priority, __VA_ARGS__)
73 #endif
74 
75 #ifdef WITH_LOG4C
76 #define CSYNC_LOG_PRIORITY_FATAL LOG4C_PRIORITY_FATAL
77 #define CSYNC_LOG_PRIORITY_ALERT LOG4C_PRIORITY_ALERT
78 #define CSYNC_LOG_PRIORITY_CRIT LOG4C_PRIORITY_CRIT
79 #define CSYNC_LOG_PRIORITY_ERROR LOG4C_PRIORITY_ERROR
80 #define CSYNC_LOG_PRIORITY_WARN LOG4C_PRIORITY_WARN
81 #define CSYNC_LOG_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE
82 #define CSYNC_LOG_PRIORITY_INFO LOG4C_PRIORITY_INFO
83 #define CSYNC_LOG_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG
84 #define CSYNC_LOG_PRIORITY_TRACE LOG4C_PRIORITY_TRACE
85 #define CSYNC_LOG_PRIORITY_NOTSET LOG4C_PRIORITY_NOTSET
86 #define CSYNC_LOG_PRIORITY_UNKNOWN LOG4C_PRIORITY_UNKNOWN
87 #else
88 #define LOG4C_INLINE inline
89 #define CSYNC_LOG_PRIORITY_FATAL 000
90 #define CSYNC_LOG_PRIORITY_ALERT 100
91 #define CSYNC_LOG_PRIORITY_CRIT 200
92 #define CSYNC_LOG_PRIORITY_ERROR 300
93 #define CSYNC_LOG_PRIORITY_WARN 500
94 #define CSYNC_LOG_PRIORITY_NOTICE 500
95 #define CSYNC_LOG_PRIORITY_INFO 600
96 #define CSYNC_LOG_PRIORITY_DEBUG 700
97 #define CSYNC_LOG_PRIORITY_TRACE 800
98 #define CSYNC_LOG_PRIORITY_NOTSET 900
99 #define CSYNC_LOG_PRIORITY_UNKNOWN 1000
100 #endif
101 
102 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
103  const char* a_format,...) PRINTF_ATTRIBUTE(3, 4);
104 /**
105  * @brief The constructor of the logging mechanism
106  *
107  * @return 0 on success, less than 0 if an error occured.
108  */
110 #ifdef WITH_LOG4C
111  return log4c_init();
112 #else
113  return 0;
114 #endif
115 }
116 
117 /**
118  * @brief Load resource configuration file
119  *
120  * @param Path to the file to load
121  *
122  * @return 0 on success, less than 0 if an error occured.
123  **/
124 static LOG4C_INLINE int csync_log_load(const char *path){
125 #ifdef WITH_LOG4C
126  return log4c_load(path);
127 #else
128  if (path == NULL) {
129  return 0;
130  }
131  return 0;
132 #endif
133 }
134 
135 /**
136  * @brief The destructor of the logging mechanism
137  *
138  * @return 0 on success, less than 0 if an error occured.
139  */
141 #ifdef WITH_LOG4C
142  return log4c_fini();
143 #else
144  return 0;
145 #endif
146 }
147 
148 static LOG4C_INLINE int csync_log_setappender(char *catName, char *appName) {
149 #ifdef WITH_LOG4C
150  log4c_category_set_appender(log4c_category_get(catName),
151  log4c_appender_get(appName));
152  return 0;
153 #else
154  if (catName == NULL || appName == NULL) {
155  return 0;
156  }
157  return 0;
158 #endif
159 }
160 
161 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
162  const char* a_format,...) {
163 #ifdef WITH_LOG4C
164  const log4c_category_t* a_category = log4c_category_get(catName);
165  if (log4c_category_is_priority_enabled(a_category, a_priority)) {
166  va_list va;
167  va_start(va, a_format);
168  log4c_category_vlog(a_category, a_priority, a_format, va);
169  va_end(va);
170  }
171 #else
172  /* On Apple, all stderr & stdout go to Apple System Log (ASL) by default.
173  * Thats certainly too much at the moment.
174  */
175 #ifndef __APPLE__
176  va_list va;
177  va_start(va, a_format);
178  if (a_priority > 0) {
179  printf("%s - ", catName);
180  }
181  vprintf(a_format, va);
182  va_end(va);
183  printf("\n");
184 #endif
185 #endif
186 }
187 
188 /**
189  * }@
190  */
191 #endif /* _CSYNC_LOG_H */
192 
193 /* vim: set ft=c.doxygen ts=8 sw=2 et cindent: */