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 #ifdef WITH_LOG4C
63 #define CSYNC_LOG_PRIORITY_FATAL LOG4C_PRIORITY_FATAL
64 #define CSYNC_LOG_PRIORITY_ALERT LOG4C_PRIORITY_ALERT
65 #define CSYNC_LOG_PRIORITY_CRIT LOG4C_PRIORITY_CRIT
66 #define CSYNC_LOG_PRIORITY_ERROR LOG4C_PRIORITY_ERROR
67 #define CSYNC_LOG_PRIORITY_WARN LOG4C_PRIORITY_WARN
68 #define CSYNC_LOG_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE
69 #define CSYNC_LOG_PRIORITY_INFO LOG4C_PRIORITY_INFO
70 #define CSYNC_LOG_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG
71 #define CSYNC_LOG_PRIORITY_TRACE LOG4C_PRIORITY_TRACE
72 #define CSYNC_LOG_PRIORITY_NOTSET LOG4C_PRIORITY_NOTSET
73 #define CSYNC_LOG_PRIORITY_UNKNOWN LOG4C_PRIORITY_UNKNOWN
74 #else
75 #define LOG4C_INLINE inline
76 #define CSYNC_LOG_PRIORITY_FATAL 000
77 #define CSYNC_LOG_PRIORITY_ALERT 100
78 #define CSYNC_LOG_PRIORITY_CRIT 200
79 #define CSYNC_LOG_PRIORITY_ERROR 300
80 #define CSYNC_LOG_PRIORITY_WARN 500
81 #define CSYNC_LOG_PRIORITY_NOTICE 500
82 #define CSYNC_LOG_PRIORITY_INFO 600
83 #define CSYNC_LOG_PRIORITY_DEBUG 700
84 #define CSYNC_LOG_PRIORITY_TRACE 800
85 #define CSYNC_LOG_PRIORITY_NOTSET 900
86 #define CSYNC_LOG_PRIORITY_UNKNOWN 1000
87 #endif
88 
89 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
90  const char* a_format,...) PRINTF_ATTRIBUTE(3, 4);
91 /**
92  * @brief The constructor of the logging mechanism
93  *
94  * @return 0 on success, less than 0 if an error occured.
95  */
97 #ifdef WITH_LOG4C
98  return log4c_init();
99 #else
100  return 0;
101 #endif
102 }
103 
104 /**
105  * @brief Load resource configuration file
106  *
107  * @param Path to the file to load
108  *
109  * @return 0 on success, less than 0 if an error occured.
110  **/
111 static LOG4C_INLINE int csync_log_load(const char *path){
112 #ifdef WITH_LOG4C
113  return log4c_load(path);
114 #else
115  if (path == NULL) {
116  return 0;
117  }
118  return 0;
119 #endif
120 }
121 
122 /**
123  * @brief The destructor of the logging mechanism
124  *
125  * @return 0 on success, less than 0 if an error occured.
126  */
128 #ifdef WITH_LOG4C
129  return log4c_fini();
130 #else
131  return 0;
132 #endif
133 }
134 
135 static LOG4C_INLINE int csync_log_setappender(char *catName, char *appName) {
136 #ifdef WITH_LOG4C
137  log4c_category_set_appender(log4c_category_get(catName),
138  log4c_appender_get(appName));
139  return 0;
140 #else
141  if (catName == NULL || appName == NULL) {
142  return 0;
143  }
144  return 0;
145 #endif
146 }
147 
148 static LOG4C_INLINE void csync_log(char *catName, int a_priority,
149  const char* a_format,...) {
150 #ifdef WITH_LOG4C
151  const log4c_category_t* a_category = log4c_category_get(catName);
152  if (log4c_category_is_priority_enabled(a_category, a_priority)) {
153  va_list va;
154  va_start(va, a_format);
155  log4c_category_vlog(a_category, a_priority, a_format, va);
156  va_end(va);
157  }
158 #else
159  va_list va;
160  va_start(va, a_format);
161  if (a_priority > 0) {
162  printf("%s - ", catName);
163  }
164  vprintf(a_format, va);
165  va_end(va);
166  printf("\n");
167 #endif
168 }
169 
170 /**
171  * }@
172  */
173 #endif /* _CSYNC_LOG_H */
174 
175 /* vim: set ft=c.doxygen ts=8 sw=2 et cindent: */