1 /*
2  * This file is part of the zlog Library.
3  *
4  * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
5  *
6  * Licensed under the LGPL v2.1, see the file COPYING in base directory.
7  */
8 
9 #ifndef __zlog_h
10 #define __zlog_h
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include <stdarg.h> /* for va_list */
17 #include <stdio.h> /* for size_t */
18 
19 # if defined __GNUC__
20 #   define ZLOG_CHECK_PRINTF(m,n) __attribute__((format(printf,m,n)))
21 # else
22 #   define ZLOG_CHECK_PRINTF(m,n)
23 # endif
24 
25 typedef struct zlog_category_s zlog_category_t;
26 
27 int zlog_init(const char *confpath);
28 int zlog_reload(const char *confpath);
29 void zlog_fini(void);
30 
31 void zlog_profile(void);
32 
33 zlog_category_t *zlog_get_category(const char *cname);
34 
35 int zlog_put_mdc(const char *key, const char *value);
36 char *zlog_get_mdc(const char *key);
37 void zlog_remove_mdc(const char *key);
38 void zlog_clean_mdc(void);
39 
40 void zlog(zlog_category_t * category,
41 	const char *file, size_t filelen,
42 	const char *func, size_t funclen,
43 	long line, int level,
44 	const char *format, ...) ZLOG_CHECK_PRINTF(8,9);
45 void vzlog(zlog_category_t * category,
46 	const char *file, size_t filelen,
47 	const char *func, size_t funclen,
48 	long line, int level,
49 	const char *format, va_list args);
50 void hzlog(zlog_category_t * category,
51 	const char *file, size_t filelen,
52 	const char *func, size_t funclen,
53 	long line, int level,
54 	const void *buf, size_t buflen);
55 
56 int dzlog_init(const char *confpath, const char *cname);
57 int dzlog_set_category(const char *cname);
58 
59 void dzlog(const char *file, size_t filelen,
60 	const char *func, size_t funclen,
61 	long line, int level,
62 	const char *format, ...) ZLOG_CHECK_PRINTF(7,8);
63 void vdzlog(const char *file, size_t filelen,
64 	const char *func, size_t funclen,
65 	long line, int level,
66 	const char *format, va_list args);
67 void hdzlog(const char *file, size_t filelen,
68 	const char *func, size_t funclen,
69 	long line, int level,
70 	const void *buf, size_t buflen);
71 
72 typedef struct zlog_msg_s {
73 	char *buf;
74 	size_t len;
75 	char *path;
76 } zlog_msg_t;
77 
78 typedef int (*zlog_record_fn)(zlog_msg_t *msg);
79 int zlog_set_record(const char *rname, zlog_record_fn record);
80 
81 /******* useful macros, can be redefined at user's h file **********/
82 
83 typedef enum {
84 	ZLOG_LEVEL_DEBUG = 20,
85 	ZLOG_LEVEL_INFO = 40,
86 	ZLOG_LEVEL_NOTICE = 60,
87 	ZLOG_LEVEL_WARN = 80,
88 	ZLOG_LEVEL_ERROR = 100,
89 	ZLOG_LEVEL_FATAL = 120
90 } zlog_level;
91 
92 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
93 # if defined __GNUC__ && __GNUC__ >= 2
94 #  define __func__ __FUNCTION__
95 # else
96 #  define __func__ "<unknown>"
97 # endif
98 #endif
99 
100 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
101 /* zlog macros */
102 #define zlog_fatal(cat, ...) \
103 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
104 	ZLOG_LEVEL_FATAL, __VA_ARGS__)
105 #define zlog_error(cat, ...) \
106 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
107 	ZLOG_LEVEL_ERROR, __VA_ARGS__)
108 #define zlog_warn(cat, ...) \
109 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
110 	ZLOG_LEVEL_WARN, __VA_ARGS__)
111 #define zlog_notice(cat, ...) \
112 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
113 	ZLOG_LEVEL_NOTICE, __VA_ARGS__)
114 #define zlog_info(cat, ...) \
115 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
116 	ZLOG_LEVEL_INFO, __VA_ARGS__)
117 #define zlog_debug(cat, ...) \
118 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
119 	ZLOG_LEVEL_DEBUG, __VA_ARGS__)
120 /* dzlog macros */
121 #define dzlog_fatal(...) \
122 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
123 	ZLOG_LEVEL_FATAL, __VA_ARGS__)
124 #define dzlog_error(...) \
125 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
126 	ZLOG_LEVEL_ERROR, __VA_ARGS__)
127 #define dzlog_warn(...) \
128 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
129 	ZLOG_LEVEL_WARN, __VA_ARGS__)
130 #define dzlog_notice(...) \
131 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
132 	ZLOG_LEVEL_NOTICE, __VA_ARGS__)
133 #define dzlog_info(...) \
134 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
135 	ZLOG_LEVEL_INFO, __VA_ARGS__)
136 #define dzlog_debug(...) \
137 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
138 	ZLOG_LEVEL_DEBUG, __VA_ARGS__)
139 #elif defined __GNUC__
140 /* zlog macros */
141 #define zlog_fatal(cat, format, args...) \
142 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
143 	ZLOG_LEVEL_FATAL, format, ##args)
144 #define zlog_error(cat, format, args...) \
145 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
146 	ZLOG_LEVEL_ERROR, format, ##args)
147 #define zlog_warn(cat, format, args...) \
148 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
149 	ZLOG_LEVEL_WARN, format, ##args)
150 #define zlog_notice(cat, format, args...) \
151 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
152 	ZLOG_LEVEL_NOTICE, format, ##args)
153 #define zlog_info(cat, format, args...) \
154 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
155 	ZLOG_LEVEL_INFO, format, ##args)
156 #define zlog_debug(cat, format, args...) \
157 	zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
158 	ZLOG_LEVEL_DEBUG, format, ##args)
159 /* dzlog macros */
160 #define dzlog_fatal(format, args...) \
161 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
162 	ZLOG_LEVEL_FATAL, format, ##args)
163 #define dzlog_error(format, args...) \
164 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
165 	ZLOG_LEVEL_ERROR, format, ##args)
166 #define dzlog_warn(format, args...) \
167 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
168 	ZLOG_LEVEL_WARN, format, ##args)
169 #define dzlog_notice(format, args...) \
170 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
171 	ZLOG_LEVEL_NOTICE, format, ##args)
172 #define dzlog_info(format, args...) \
173 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
174 	ZLOG_LEVEL_INFO, format, ##args)
175 #define dzlog_debug(format, args...) \
176 	dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
177 	ZLOG_LEVEL_DEBUG, format, ##args)
178 #endif
179 
180 /* vzlog macros */
181 #define vzlog_fatal(cat, format, args) \
182 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
183 	ZLOG_LEVEL_FATAL, format, args)
184 #define vzlog_error(cat, format, args) \
185 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
186 	ZLOG_LEVEL_ERROR, format, args)
187 #define vzlog_warn(cat, format, args) \
188 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
189 	ZLOG_LEVEL_WARN, format, args)
190 #define vzlog_notice(cat, format, args) \
191 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
192 	ZLOG_LEVEL_NOTICE, format, args)
193 #define vzlog_info(cat, format, args) \
194 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
195 	ZLOG_LEVEL_INFO, format, args)
196 #define vzlog_debug(cat, format, args) \
197 	vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
198 	ZLOG_LEVEL_DEBUG, format, args)
199 
200 /* hzlog macros */
201 #define hzlog_fatal(cat, buf, buf_len) \
202 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
203 	ZLOG_LEVEL_FATAL, buf, buf_len)
204 #define hzlog_error(cat, buf, buf_len) \
205 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
206 	ZLOG_LEVEL_ERROR, buf, buf_len)
207 #define hzlog_warn(cat, buf, buf_len) \
208 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
209 	ZLOG_LEVEL_WARN, buf, buf_len)
210 #define hzlog_notice(cat, buf, buf_len) \
211 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
212 	ZLOG_LEVEL_NOTICE, buf, buf_len)
213 #define hzlog_info(cat, buf, buf_len) \
214 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
215 	ZLOG_LEVEL_INFO, buf, buf_len)
216 #define hzlog_debug(cat, buf, buf_len) \
217 	hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
218 	ZLOG_LEVEL_DEBUG, buf, buf_len)
219 
220 
221 /* vdzlog macros */
222 #define vdzlog_fatal(format, args) \
223 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
224 	ZLOG_LEVEL_FATAL, format, args)
225 #define vdzlog_error(format, args) \
226 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
227 	ZLOG_LEVEL_ERROR, format, args)
228 #define vdzlog_warn(format, args) \
229 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
230 	ZLOG_LEVEL_WARN, format, args)
231 #define vdzlog_notice(format, args) \
232 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
233 	ZLOG_LEVEL_NOTICE, format, args)
234 #define vdzlog_info(format, args) \
235 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
236 	ZLOG_LEVEL_INFO, format, args)
237 #define vdzlog_debug(format, args) \
238 	vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
239 	ZLOG_LEVEL_DEBUG, format, args)
240 
241 /* hdzlog macros */
242 #define hdzlog_fatal(buf, buf_len) \
243 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
244 	ZLOG_LEVEL_FATAL, buf, buf_len)
245 #define hdzlog_error(buf, buf_len) \
246 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
247 	ZLOG_LEVEL_ERROR, buf, buf_len)
248 #define hdzlog_warn(buf, buf_len) \
249 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
250 	ZLOG_LEVEL_WARN, buf, buf_len)
251 #define hdzlog_notice(buf, buf_len) \
252 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
253 	ZLOG_LEVEL_NOTICE, buf, buf_len)
254 #define hdzlog_info(buf, buf_len) \
255 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
256 	ZLOG_LEVEL_INFO, buf, buf_len)
257 #define hdzlog_debug(buf, buf_len) \
258 	hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
259 	ZLOG_LEVEL_DEBUG, buf, buf_len)
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif
266