2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file para.h global paraslash definitions */
14 #include <time.h> /* time(), localtime() */
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <sys/un.h> /* needed by create_pf_socket */
27 #include "gcc-compat.h"
29 /** compute the minimum of \a a and \a b */
30 #define MIN(a,b) ((a) < (b) ? (a) : (b))
31 /** compute the maximum of \a a and \a b */
32 #define MAX(a,b) ((a) > (b) ? (a) : (b))
33 /** compute the absolute value of \a a */
34 #define ABS(a) ((a) > 0 ? (a) : -(a))
36 /** debug loglevel, gets really noisy */
38 /** still noisy, but won't fill your disk */
40 /** normal, but significant event */
42 /** unexpected event that can be handled */
44 /** unhandled error condition */
46 /** system might be unreliable */
48 /** last message before exit */
51 /** Log messages with lower priority than that will not be compiled in. */
52 #define COMPILE_TIME_LOGLEVEL 0
55 #if DEBUG > COMPILE_TIME_LOGLEVEL
56 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
58 #define DEBUG_LOG(...) do {;} while (0)
61 #if INFO > COMPILE_TIME_LOGLEVEL
62 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
64 #define INFO_LOG(...) do {;} while (0)
67 #if NOTICE > COMPILE_TIME_LOGLEVEL
68 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
70 #define NOTICE_LOG(...) do {;} while (0)
73 #if WARNING > COMPILE_TIME_LOGLEVEL
74 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
76 #define WARNING_LOG(...) do {;} while (0)
79 #if ERROR > COMPILE_TIME_LOGLEVEL
80 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
82 #define ERROR_LOG(...) do {;} while (0)
85 #if CRIT > COMPILE_TIME_LOGLEVEL
86 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
88 #define CRIT_LOG(...) do {;} while (0)
91 #if EMERG > COMPILE_TIME_LOGLEVEL
92 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
94 #define EMERG_LOG(...)
97 __printf_2_3 void __log(int, const char*, ...);
100 * Write a log message to a dynamically allocated string.
102 * \param fmt Usual format string.
103 * \param p Result pointer.
106 #define VSPRINTF(fmt, p) \
110 p = adu_malloc(size); \
113 /* Try to print in the allocated space. */ \
115 n = vsnprintf(p, size, fmt, ap); \
117 /* If that worked, return the string. */ \
118 if (n > -1 && n < size) \
120 /* Else try again with more space. */ \
121 if (n > -1) /* glibc 2.1 */ \
122 size = n + 1; /* precisely what is needed */ \
123 else /* glibc 2.0 */ \
124 size *= 2; /* twice the old size */ \
125 p = adu_realloc(p, size); \