]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.h
fd: only add valid fd to mainloop
[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 __LXC_LOG_H
25 #define __LXC_LOG_H
26
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <sys/time.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <stdbool.h>
34 #include <syslog.h>
35 #include <time.h>
36
37 #include "conf.h"
38
39 #ifndef O_CLOEXEC
40 #define O_CLOEXEC 02000000
41 #endif
42
43 #ifndef F_DUPFD_CLOEXEC
44 #define F_DUPFD_CLOEXEC 1030
45 #endif
46
47 #define LXC_LOG_PREFIX_SIZE 32
48 #define LXC_LOG_BUFFER_SIZE 4096
49
50 /* This attribute is required to silence clang warnings */
51 #if defined(__GNUC__)
52 #define ATTR_UNUSED __attribute__ ((unused))
53 #else
54 #define ATTR_UNUSED
55 #endif
56
57 /* predefined lxc log priorities. */
58 enum lxc_loglevel {
59 LXC_LOG_LEVEL_TRACE,
60 LXC_LOG_LEVEL_DEBUG,
61 LXC_LOG_LEVEL_INFO,
62 LXC_LOG_LEVEL_NOTICE,
63 LXC_LOG_LEVEL_WARN,
64 LXC_LOG_LEVEL_ERROR,
65 LXC_LOG_LEVEL_CRIT,
66 LXC_LOG_LEVEL_ALERT,
67 LXC_LOG_LEVEL_FATAL,
68 LXC_LOG_LEVEL_NOTSET,
69 };
70
71 /* location information of the logging event */
72 struct lxc_log_locinfo {
73 const char *file;
74 const char *func;
75 int line;
76 };
77
78 #define LXC_LOG_LOCINFO_INIT \
79 { .file = __FILE__, .func = __func__, .line = __LINE__ }
80
81 /* brief logging event object */
82 struct lxc_log_event {
83 const char *category;
84 int priority;
85 struct timespec timestamp;
86 struct lxc_log_locinfo *locinfo;
87 const char *fmt;
88 va_list *vap;
89 };
90
91 /* log appender object */
92 struct lxc_log_appender {
93 const char *name;
94 int (*append)(const struct lxc_log_appender *, struct lxc_log_event *);
95
96 /*
97 * appenders can be stacked
98 */
99 struct lxc_log_appender *next;
100 };
101
102 /* log category object */
103 struct lxc_log_category {
104 const char *name;
105 int priority;
106 struct lxc_log_appender *appender;
107 const struct lxc_log_category *parent;
108 };
109
110 #ifndef NO_LXC_CONF
111 extern int lxc_log_use_global_fd;
112 #endif
113
114 /*
115 * Returns true if the chained priority is equal to or higher than
116 * given priority.
117 */
118 static inline int lxc_log_priority_is_enabled(const struct lxc_log_category *category,
119 int priority)
120 {
121 while (category->priority == LXC_LOG_LEVEL_NOTSET && category->parent)
122 category = category->parent;
123
124 int cmp_prio = category->priority;
125 #ifndef NO_LXC_CONF
126 if (!lxc_log_use_global_fd && current_config &&
127 current_config->loglevel != LXC_LOG_LEVEL_NOTSET)
128 cmp_prio = current_config->loglevel;
129 #endif
130
131 return priority >= cmp_prio;
132 }
133
134 /*
135 * converts a priority to a literal string
136 */
137 static inline const char *lxc_log_priority_to_string(int priority)
138 {
139 switch (priority) {
140 case LXC_LOG_LEVEL_TRACE:
141 return "TRACE";
142 case LXC_LOG_LEVEL_DEBUG:
143 return "DEBUG";
144 case LXC_LOG_LEVEL_INFO:
145 return "INFO";
146 case LXC_LOG_LEVEL_NOTICE:
147 return "NOTICE";
148 case LXC_LOG_LEVEL_WARN:
149 return "WARN";
150 case LXC_LOG_LEVEL_ERROR:
151 return "ERROR";
152 case LXC_LOG_LEVEL_CRIT:
153 return "CRIT";
154 case LXC_LOG_LEVEL_ALERT:
155 return "ALERT";
156 case LXC_LOG_LEVEL_FATAL:
157 return "FATAL";
158 }
159
160 return "NOTSET";
161 }
162
163 static inline const char *lxc_syslog_priority_to_string(int priority)
164 {
165 switch (priority) {
166 case LOG_DAEMON:
167 return "daemon";
168 case LOG_LOCAL0:
169 return "local0";
170 case LOG_LOCAL1:
171 return "local1";
172 case LOG_LOCAL2:
173 return "local2";
174 case LOG_LOCAL3:
175 return "local3";
176 case LOG_LOCAL4:
177 return "local4";
178 case LOG_LOCAL5:
179 return "local5";
180 case LOG_LOCAL6:
181 return "local6";
182 case LOG_LOCAL7:
183 return "local7";
184 }
185
186 return "NOTSET";
187 }
188
189 /*
190 * converts a literal priority to an int
191 */
192 static inline int lxc_log_priority_to_int(const char *name)
193 {
194 if (strcasecmp("TRACE", name) == 0)
195 return LXC_LOG_LEVEL_TRACE;
196 if (strcasecmp("DEBUG", name) == 0)
197 return LXC_LOG_LEVEL_DEBUG;
198 if (strcasecmp("INFO", name) == 0)
199 return LXC_LOG_LEVEL_INFO;
200 if (strcasecmp("NOTICE", name) == 0)
201 return LXC_LOG_LEVEL_NOTICE;
202 if (strcasecmp("WARN", name) == 0)
203 return LXC_LOG_LEVEL_WARN;
204 if (strcasecmp("ERROR", name) == 0)
205 return LXC_LOG_LEVEL_ERROR;
206 if (strcasecmp("CRIT", name) == 0)
207 return LXC_LOG_LEVEL_CRIT;
208 if (strcasecmp("ALERT", name) == 0)
209 return LXC_LOG_LEVEL_ALERT;
210 if (strcasecmp("FATAL", name) == 0)
211 return LXC_LOG_LEVEL_FATAL;
212
213 return LXC_LOG_LEVEL_NOTSET;
214 }
215
216 static inline int lxc_syslog_priority_to_int(const char *name)
217 {
218 if (strcasecmp("daemon", name) == 0)
219 return LOG_DAEMON;
220 if (strcasecmp("local0", name) == 0)
221 return LOG_LOCAL0;
222 if (strcasecmp("local1", name) == 0)
223 return LOG_LOCAL1;
224 if (strcasecmp("local2", name) == 0)
225 return LOG_LOCAL2;
226 if (strcasecmp("local3", name) == 0)
227 return LOG_LOCAL3;
228 if (strcasecmp("local4", name) == 0)
229 return LOG_LOCAL4;
230 if (strcasecmp("local5", name) == 0)
231 return LOG_LOCAL5;
232 if (strcasecmp("local6", name) == 0)
233 return LOG_LOCAL6;
234 if (strcasecmp("local7", name) == 0)
235 return LOG_LOCAL7;
236
237 return -EINVAL;
238 }
239
240 static inline void __lxc_log_append(const struct lxc_log_appender *appender,
241 struct lxc_log_event *event)
242 {
243 va_list va;
244 va_list *va_keep = event->vap;
245
246 while (appender) {
247 va_copy(va, *va_keep);
248 event->vap = &va;
249 appender->append(appender, event);
250 appender = appender->next;
251 va_end(va);
252 }
253 }
254
255 static inline void __lxc_log(const struct lxc_log_category *category,
256 struct lxc_log_event *event)
257 {
258 while (category) {
259 __lxc_log_append(category->appender, event);
260 category = category->parent;
261 }
262 }
263
264 /*
265 * Helper macro to define log functions.
266 */
267 #define lxc_log_priority_define(acategory, LEVEL) \
268 \
269 ATTR_UNUSED __attribute__ ((format (printf, 2, 3))) \
270 static inline void LXC_##LEVEL(struct lxc_log_locinfo *, const char *, ...); \
271 \
272 ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
273 const char* format, ...) \
274 { \
275 if (lxc_log_priority_is_enabled(acategory, LXC_LOG_LEVEL_##LEVEL)) { \
276 va_list va_ref; \
277 int saved_errno; \
278 struct lxc_log_event evt = { \
279 .category = (acategory)->name, \
280 .priority = LXC_LOG_LEVEL_##LEVEL, \
281 .fmt = format, \
282 .locinfo = locinfo \
283 }; \
284 \
285 /* clock_gettime() is explicitly marked as MT-Safe \
286 * without restrictions. So let's use it for our \
287 * logging stamps. \
288 */ \
289 saved_errno = errno; \
290 (void)clock_gettime(CLOCK_REALTIME, &evt.timestamp); \
291 \
292 va_start(va_ref, format); \
293 evt.vap = &va_ref; \
294 __lxc_log(acategory, &evt); \
295 va_end(va_ref); \
296 errno = saved_errno; \
297 } \
298 }
299
300 /*
301 * Helper macro to define and use static categories.
302 */
303 #define lxc_log_category_define(name, parent) \
304 extern struct lxc_log_category lxc_log_category_##parent; \
305 struct lxc_log_category lxc_log_category_##name = { \
306 #name, \
307 LXC_LOG_LEVEL_NOTSET, \
308 NULL, \
309 &lxc_log_category_##parent \
310 };
311
312 #define lxc_log_define(name, parent) \
313 lxc_log_category_define(name, parent) \
314 \
315 lxc_log_priority_define(&lxc_log_category_##name, TRACE) \
316 lxc_log_priority_define(&lxc_log_category_##name, DEBUG) \
317 lxc_log_priority_define(&lxc_log_category_##name, INFO) \
318 lxc_log_priority_define(&lxc_log_category_##name, NOTICE) \
319 lxc_log_priority_define(&lxc_log_category_##name, WARN) \
320 lxc_log_priority_define(&lxc_log_category_##name, ERROR) \
321 lxc_log_priority_define(&lxc_log_category_##name, CRIT) \
322 lxc_log_priority_define(&lxc_log_category_##name, ALERT) \
323 lxc_log_priority_define(&lxc_log_category_##name, FATAL)
324
325 #define lxc_log_category_priority(name) \
326 (lxc_log_priority_to_string(lxc_log_category_##name.priority))
327
328 /*
329 * Helper macro to define errno string.
330 */
331 #if HAVE_STRERROR_R
332 #ifndef HAVE_DECL_STRERROR_R
333 #ifdef STRERROR_R_CHAR_P
334 char *strerror_r(int errnum, char *buf, size_t buflen);
335 #else
336 int strerror_r(int errnum, char *buf, size_t buflen);
337 #endif
338 #endif
339
340 #ifdef STRERROR_R_CHAR_P
341 #define lxc_log_strerror_r \
342 char errno_buf[PATH_MAX / 2] = {"Failed to get errno string"}; \
343 char *ptr = NULL; \
344 { \
345 int __saved_errno = errno; \
346 ptr = strerror_r(errno, errno_buf, sizeof(errno_buf)); \
347 errno = __saved_errno; \
348 if (!ptr) \
349 ptr = errno_buf; \
350 }
351 #else
352 #define lxc_log_strerror_r \
353 char errno_buf[PATH_MAX / 2] = {"Failed to get errno string"}; \
354 char *ptr = errno_buf; \
355 { \
356 int __saved_errno = errno; \
357 (void)strerror_r(errno, errno_buf, sizeof(errno_buf)); \
358 errno = __saved_errno; \
359 }
360 #endif
361 #elif ENFORCE_THREAD_SAFETY
362 #error ENFORCE_THREAD_SAFETY was set but cannot be guaranteed
363 #else
364 #define lxc_log_strerror_r \
365 char *ptr = NULL; \
366 { \
367 ptr = strerror(errno); \
368 }
369 #endif
370
371 /*
372 * top categories
373 */
374 #define TRACE(format, ...) do { \
375 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
376 LXC_TRACE(&locinfo, format, ##__VA_ARGS__); \
377 } while (0)
378
379 #define DEBUG(format, ...) do { \
380 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
381 LXC_DEBUG(&locinfo, format, ##__VA_ARGS__); \
382 } while (0)
383
384 #define INFO(format, ...) do { \
385 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
386 LXC_INFO(&locinfo, format, ##__VA_ARGS__); \
387 } while (0)
388
389 #define NOTICE(format, ...) do { \
390 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
391 LXC_NOTICE(&locinfo, format, ##__VA_ARGS__); \
392 } while (0)
393
394 #define WARN(format, ...) do { \
395 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
396 LXC_WARN(&locinfo, format, ##__VA_ARGS__); \
397 } while (0)
398
399 #define ERROR(format, ...) do { \
400 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
401 LXC_ERROR(&locinfo, format, ##__VA_ARGS__); \
402 } while (0)
403
404 #define CRIT(format, ...) do { \
405 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
406 LXC_CRIT(&locinfo, format, ##__VA_ARGS__); \
407 } while (0)
408
409 #define ALERT(format, ...) do { \
410 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
411 LXC_ALERT(&locinfo, format, ##__VA_ARGS__); \
412 } while (0)
413
414 #define FATAL(format, ...) do { \
415 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
416 LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
417 } while (0)
418
419 #if HAVE_M_FORMAT
420 #define SYSTRACE(format, ...) \
421 TRACE("%m - " format, ##__VA_ARGS__)
422 #else
423 #define SYSTRACE(format, ...) \
424 do { \
425 lxc_log_strerror_r; \
426 TRACE("%s - " format, ptr, ##__VA_ARGS__); \
427 } while (0)
428 #endif
429
430 #if HAVE_M_FORMAT
431 #define SYSDEBUG(format, ...) \
432 DEBUG("%m - " format, ##__VA_ARGS__)
433 #else
434 #define SYSDEBUG(format, ...) \
435 do { \
436 lxc_log_strerror_r; \
437 DEBUG("%s - " format, ptr, ##__VA_ARGS__); \
438 } while (0)
439 #endif
440
441
442 #if HAVE_M_FORMAT
443 #define SYSINFO(format, ...) \
444 INFO("%m - " format, ##__VA_ARGS__)
445 #else
446 #define SYSINFO(format, ...) \
447 do { \
448 lxc_log_strerror_r; \
449 INFO("%s - " format, ptr, ##__VA_ARGS__); \
450 } while (0)
451 #endif
452
453 #if HAVE_M_FORMAT
454 #define SYSNOTICE(format, ...) \
455 NOTICE("%m - " format, ##__VA_ARGS__)
456 #else
457 #define SYSNOTICE(format, ...) \
458 do { \
459 lxc_log_strerror_r; \
460 NOTICE("%s - " format, ptr, ##__VA_ARGS__); \
461 } while (0)
462 #endif
463
464 #if HAVE_M_FORMAT
465 #define SYSWARN(format, ...) \
466 WARN("%m - " format, ##__VA_ARGS__)
467 #else
468 #define SYSWARN(format, ...) \
469 do { \
470 lxc_log_strerror_r; \
471 WARN("%s - " format, ptr, ##__VA_ARGS__); \
472 } while (0)
473 #endif
474
475 #if HAVE_M_FORMAT
476 #define SYSERROR(format, ...) \
477 ERROR("%m - " format, ##__VA_ARGS__)
478 #else
479 #define SYSERROR(format, ...) \
480 do { \
481 lxc_log_strerror_r; \
482 ERROR("%s - " format, ptr, ##__VA_ARGS__); \
483 } while (0)
484 #endif
485
486 #if HAVE_M_FORMAT
487 #define CMD_SYSERROR(format, ...) \
488 fprintf(stderr, "%m - " format, ##__VA_ARGS__)
489 #else
490 #define CMD_SYSERROR(format, ...) \
491 do { \
492 lxc_log_strerror_r; \
493 fprintf(stderr, "%s - " format, ptr, ##__VA_ARGS__); \
494 } while (0)
495 #endif
496
497 #if HAVE_M_FORMAT
498 #define CMD_SYSINFO(format, ...) \
499 printf("%m - " format, ##__VA_ARGS__)
500 #else
501 #define CMD_SYSINFO(format, ...) \
502 do { \
503 lxc_log_strerror_r; \
504 printf("%s - " format, ptr, ##__VA_ARGS__); \
505 } while (0)
506 #endif
507
508 #define error_log_errno(__errno__, format, ...) \
509 ({ \
510 errno = __errno__; \
511 SYSERROR(format, ##__VA_ARGS__); \
512 -1; \
513 })
514
515 extern int lxc_log_fd;
516
517 extern int lxc_log_syslog(int facility);
518 extern void lxc_log_enable_syslog(void);
519 extern int lxc_log_set_level(int *dest, int level);
520 extern int lxc_log_get_level(void);
521 extern bool lxc_log_has_valid_level(void);
522 extern int lxc_log_set_file(int *fd, const char *fname);
523 extern const char *lxc_log_get_file(void);
524 extern void lxc_log_set_prefix(const char *prefix);
525 extern const char *lxc_log_get_prefix(void);
526 extern void lxc_log_options_no_override(void);
527 #endif