]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.h
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / log.h
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Cedric Le Goater <legoater@free.fr>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 #ifndef _log_h
25 #define _log_h
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <sys/time.h>
30 #include <string.h>
31 #include <stdbool.h>
32
33 #ifndef O_CLOEXEC
34 #define O_CLOEXEC 02000000
35 #endif
36
37 #ifndef F_DUPFD_CLOEXEC
38 #define F_DUPFD_CLOEXEC 1030
39 #endif
40
41 #define LXC_LOG_PREFIX_SIZE 32
42 #define LXC_LOG_BUFFER_SIZE 512
43
44 /* predefined priorities. */
45 enum lxc_loglevel {
46 LXC_LOG_PRIORITY_TRACE,
47 LXC_LOG_PRIORITY_DEBUG,
48 LXC_LOG_PRIORITY_INFO,
49 LXC_LOG_PRIORITY_NOTICE,
50 LXC_LOG_PRIORITY_WARN,
51 LXC_LOG_PRIORITY_ERROR,
52 LXC_LOG_PRIORITY_CRIT,
53 LXC_LOG_PRIORITY_ALERT,
54 LXC_LOG_PRIORITY_FATAL,
55 LXC_LOG_PRIORITY_NOTSET,
56 };
57
58 /* location information of the logging event */
59 struct lxc_log_locinfo {
60 const char *file;
61 const char *func;
62 int line;
63 };
64
65 #define LXC_LOG_LOCINFO_INIT \
66 { .file = __FILE__, .func = __func__, .line = __LINE__ }
67
68 /* brief logging event object */
69 struct lxc_log_event {
70 const char* category;
71 int priority;
72 struct timeval timestamp;
73 struct lxc_log_locinfo *locinfo;
74 const char *fmt;
75 va_list *vap;
76 };
77
78 /* log appender object */
79 struct lxc_log_appender {
80 const char* name;
81 int (*append)(const struct lxc_log_appender *, struct lxc_log_event *);
82
83 /*
84 * appenders can be stacked
85 */
86 struct lxc_log_appender *next;
87 };
88
89 /* log category object */
90 struct lxc_log_category {
91 const char *name;
92 int priority;
93 struct lxc_log_appender *appender;
94 const struct lxc_log_category *parent;
95 };
96
97 /*
98 * Returns true if the chained priority is equal to or higher than
99 * given priority.
100 */
101 static inline int
102 lxc_log_priority_is_enabled(const struct lxc_log_category* category,
103 int priority)
104 {
105 while (category->priority == LXC_LOG_PRIORITY_NOTSET &&
106 category->parent)
107 category = category->parent;
108
109 return priority >= category->priority;
110 }
111
112 /*
113 * converts a priority to a literal string
114 */
115 static inline const char* lxc_log_priority_to_string(int priority)
116 {
117 switch (priority) {
118 case LXC_LOG_PRIORITY_TRACE: return "TRACE";
119 case LXC_LOG_PRIORITY_DEBUG: return "DEBUG";
120 case LXC_LOG_PRIORITY_INFO: return "INFO";
121 case LXC_LOG_PRIORITY_NOTICE: return "NOTICE";
122 case LXC_LOG_PRIORITY_WARN: return "WARN";
123 case LXC_LOG_PRIORITY_ERROR: return "ERROR";
124 case LXC_LOG_PRIORITY_CRIT: return "CRIT";
125 case LXC_LOG_PRIORITY_ALERT: return "ALERT";
126 case LXC_LOG_PRIORITY_FATAL: return "FATAL";
127 default:
128 return "NOTSET";
129 }
130 }
131 /*
132 * converts a literal priority to an int
133 */
134 static inline int lxc_log_priority_to_int(const char* name)
135 {
136 if (!strcasecmp("TRACE", name)) return LXC_LOG_PRIORITY_TRACE;
137 if (!strcasecmp("DEBUG", name)) return LXC_LOG_PRIORITY_DEBUG;
138 if (!strcasecmp("INFO", name)) return LXC_LOG_PRIORITY_INFO;
139 if (!strcasecmp("NOTICE", name)) return LXC_LOG_PRIORITY_NOTICE;
140 if (!strcasecmp("WARN", name)) return LXC_LOG_PRIORITY_WARN;
141 if (!strcasecmp("ERROR", name)) return LXC_LOG_PRIORITY_ERROR;
142 if (!strcasecmp("CRIT", name)) return LXC_LOG_PRIORITY_CRIT;
143 if (!strcasecmp("ALERT", name)) return LXC_LOG_PRIORITY_ALERT;
144 if (!strcasecmp("FATAL", name)) return LXC_LOG_PRIORITY_FATAL;
145
146 return LXC_LOG_PRIORITY_NOTSET;
147 }
148
149 static inline void
150 __lxc_log_append(const struct lxc_log_appender *appender,
151 struct lxc_log_event* event)
152 {
153 va_list va, *va_keep;
154 va_keep = event->vap;
155
156 while (appender) {
157 va_copy(va, *va_keep);
158 event->vap = &va;
159 appender->append(appender, event);
160 appender = appender->next;
161 va_end(va);
162 }
163 }
164
165 static inline void
166 __lxc_log(const struct lxc_log_category* category,
167 struct lxc_log_event* event)
168 {
169 while (category) {
170 __lxc_log_append(category->appender, event);
171 category = category->parent;
172 }
173 }
174
175 /*
176 * Helper macro to define log functions.
177 */
178 #define lxc_log_priority_define(acategory, PRIORITY) \
179 \
180 static inline void LXC_##PRIORITY(struct lxc_log_locinfo *, \
181 const char *, ...) __attribute__ ((format (printf, 2, 3))); \
182 \
183 static inline void LXC_##PRIORITY(struct lxc_log_locinfo* locinfo, \
184 const char* format, ...) \
185 { \
186 if (lxc_log_priority_is_enabled(acategory, \
187 LXC_LOG_PRIORITY_##PRIORITY)) { \
188 struct lxc_log_event evt = { \
189 .category = (acategory)->name, \
190 .priority = LXC_LOG_PRIORITY_##PRIORITY, \
191 .fmt = format, \
192 .locinfo = locinfo \
193 }; \
194 va_list va_ref; \
195 \
196 gettimeofday(&evt.timestamp, NULL); \
197 \
198 va_start(va_ref, format); \
199 evt.vap = &va_ref; \
200 __lxc_log(acategory, &evt); \
201 va_end(va_ref); \
202 } \
203 }
204
205 /*
206 * Helper macro to define and use static categories.
207 */
208 #define lxc_log_category_define(name, parent) \
209 extern struct lxc_log_category lxc_log_category_##parent; \
210 struct lxc_log_category lxc_log_category_##name = { \
211 #name, \
212 LXC_LOG_PRIORITY_NOTSET, \
213 NULL, \
214 &lxc_log_category_##parent \
215 };
216
217 #define lxc_log_define(name, parent) \
218 lxc_log_category_define(name, parent) \
219 \
220 lxc_log_priority_define(&lxc_log_category_##name, TRACE) \
221 lxc_log_priority_define(&lxc_log_category_##name, DEBUG) \
222 lxc_log_priority_define(&lxc_log_category_##name, INFO) \
223 lxc_log_priority_define(&lxc_log_category_##name, NOTICE) \
224 lxc_log_priority_define(&lxc_log_category_##name, WARN) \
225 lxc_log_priority_define(&lxc_log_category_##name, ERROR) \
226 lxc_log_priority_define(&lxc_log_category_##name, CRIT) \
227 lxc_log_priority_define(&lxc_log_category_##name, ALERT) \
228 lxc_log_priority_define(&lxc_log_category_##name, FATAL)
229
230 #define lxc_log_category_priority(name) \
231 (lxc_log_priority_to_string(lxc_log_category_##name.priority))
232
233 /*
234 * top categories
235 */
236 extern struct lxc_log_category lxc_log_category_lxc;
237
238 #define TRACE(format, ...) do { \
239 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
240 LXC_TRACE(&locinfo, format, ##__VA_ARGS__); \
241 } while (0)
242
243 #define DEBUG(format, ...) do { \
244 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
245 LXC_DEBUG(&locinfo, format, ##__VA_ARGS__); \
246 } while (0)
247
248 #define INFO(format, ...) do { \
249 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
250 LXC_INFO(&locinfo, format, ##__VA_ARGS__); \
251 } while (0)
252
253 #define NOTICE(format, ...) do { \
254 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
255 LXC_NOTICE(&locinfo, format, ##__VA_ARGS__); \
256 } while (0)
257
258 #define WARN(format, ...) do { \
259 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
260 LXC_WARN(&locinfo, format, ##__VA_ARGS__); \
261 } while (0)
262
263 #define ERROR(format, ...) do { \
264 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
265 LXC_ERROR(&locinfo, format, ##__VA_ARGS__); \
266 } while (0)
267
268 #define CRIT(format, ...) do { \
269 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
270 LXC_CRIT(&locinfo, format, ##__VA_ARGS__); \
271 } while (0)
272
273 #define ALERT(format, ...) do { \
274 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
275 LXC_ALERT(&locinfo, format, ##__VA_ARGS__); \
276 } while (0)
277
278 #define FATAL(format, ...) do { \
279 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
280 LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
281 } while (0)
282
283
284
285 #define SYSERROR(format, ...) do { \
286 ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
287 } while (0)
288
289 extern int lxc_log_fd;
290
291 extern int lxc_log_init(const char *name, const char *file,
292 const char *priority, const char *prefix, int quiet,
293 const char *lxcpath);
294
295 extern int lxc_log_set_file(const char *fname);
296 extern int lxc_log_set_level(int level);
297 extern void lxc_log_set_prefix(const char *prefix);
298 extern const char *lxc_log_get_file(void);
299 extern int lxc_log_get_level(void);
300 extern bool lxc_log_has_valid_level(void);
301 extern const char *lxc_log_get_prefix(void);
302 #endif