2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
12 #include <time.h> /* time(), localtime() */
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/socket.h>
21 #include <sys/un.h> /* needed by create_pf_socket */
24 #include "gcc-compat.h"
26 /** compute the maximum of \a a and \a b */
27 #define MAX(a,b) ((a) > (b) ? (a) : (b))
29 /** debug loglevel, gets really noisy */
31 /** still noisy, but won't fill your disk */
33 /** normal, but significant event */
35 /** unexpected event that can be handled */
37 /** unhandled error condition */
39 /** system might be unreliable */
41 /** last message before exit */
44 /** Log messages with lower priority than that will not be compiled in. */
45 #define COMPILE_TIME_LOGLEVEL 0
48 #if DEBUG > COMPILE_TIME_LOGLEVEL
49 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
51 #define DEBUG_LOG(...) do {;} while (0)
54 #if INFO > COMPILE_TIME_LOGLEVEL
55 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
57 #define INFO_LOG(...) do {;} while (0)
60 #if NOTICE > COMPILE_TIME_LOGLEVEL
61 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
63 #define NOTICE_LOG(...) do {;} while (0)
66 #if WARNING > COMPILE_TIME_LOGLEVEL
67 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
69 #define WARNING_LOG(...) do {;} while (0)
72 #if ERROR > COMPILE_TIME_LOGLEVEL
73 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
75 #define ERROR_LOG(...) do {;} while (0)
78 #if CRIT > COMPILE_TIME_LOGLEVEL
79 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
81 #define CRIT_LOG(...) do {;} while (0)
84 #if EMERG > COMPILE_TIME_LOGLEVEL
85 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
87 #define EMERG_LOG(...)
91 __printf_2_3 void __log(int, const char*, ...);
94 * Write a log message to a dynamically allocated string.
96 * \param fmt Usual format string.
97 * \param p Result pointer.
100 #define VSPRINTF(fmt, p) \
109 /* Try to print in the allocated space. */ \
111 n = vsnprintf(p, size, fmt, ap); \
113 /* If that worked, return the string. */ \
114 if (n > -1 && n < size) \
116 /* Else try again with more space. */ \
117 if (n > -1) /* glibc 2.1 */ \
118 size = n + 1; /* precisely what is needed */ \
119 else /* glibc 2.0 */ \
120 size *= 2; /* twice the old size */ \
121 q = realloc(p, size); \