Kill a bunch of unused cruft.
[adu.git] / adu.h
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file para.h global paraslash definitions */
8
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/wait.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <ctype.h>
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 */
24 #include <string.h>
25 #include <assert.h>
26 #include <osl.h>
27 #include "gcc-compat.h"
28
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))
35
36 /** debug loglevel, gets really noisy */
37 #define DEBUG 1
38 /** still noisy, but won't fill your disk */
39 #define INFO 2
40 /** normal, but significant event */
41 #define NOTICE 3
42 /** unexpected event that can be handled */
43 #define WARNING 4
44 /** unhandled error condition */
45 #define ERROR 5
46 /** system might be unreliable */
47 #define CRIT 6
48 /** last message before exit */
49 #define EMERG 7
50
51 /** Log messages with lower priority than that will not be compiled in. */
52 #define COMPILE_TIME_LOGLEVEL 0
53
54 /** \cond */
55 #if DEBUG > COMPILE_TIME_LOGLEVEL
56 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
57 #else
58 #define DEBUG_LOG(...) do {;} while (0)
59 #endif
60
61 #if INFO > COMPILE_TIME_LOGLEVEL
62 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
63 #else
64 #define INFO_LOG(...) do {;} while (0)
65 #endif
66
67 #if NOTICE > COMPILE_TIME_LOGLEVEL
68 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
69 #else
70 #define NOTICE_LOG(...) do {;} while (0)
71 #endif
72
73 #if WARNING > COMPILE_TIME_LOGLEVEL
74 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
75 #else
76 #define WARNING_LOG(...) do {;} while (0)
77 #endif
78
79 #if ERROR > COMPILE_TIME_LOGLEVEL
80 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
81 #else
82 #define ERROR_LOG(...) do {;} while (0)
83 #endif
84
85 #if CRIT > COMPILE_TIME_LOGLEVEL
86 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
87 #else
88 #define CRIT_LOG(...) do {;} while (0)
89 #endif
90
91 #if EMERG > COMPILE_TIME_LOGLEVEL
92 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
93 #else
94 #define EMERG_LOG(...)
95 #endif
96 /** \endcond */
97 __printf_2_3 void __log(int, const char*, ...);
98
99 /**
100 * Write a log message to a dynamically allocated string.
101 *
102 * \param fmt Usual format string.
103 * \param p Result pointer.
104 *
105 * \sa printf(3). */
106 #define VSPRINTF(fmt, p) \
107 { \
108 int n; \
109 size_t size = 100; \
110 p = para_malloc(size); \
111 while (1) { \
112 va_list ap; \
113 /* Try to print in the allocated space. */ \
114 va_start(ap, fmt); \
115 n = vsnprintf(p, size, fmt, ap); \
116 va_end(ap); \
117 /* If that worked, return the string. */ \
118 if (n > -1 && n < size) \
119 break; \
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 = para_realloc(p, size); \
126 } \
127 }