make mt-sosd use async remove, stat, copy
[archipelago] / xseg / peers / user / common.h
1 /*
2  * vkoukis.h
3  */
4
5 #define _GNU_SOURCE
6
7 #ifndef _COMMON_H
8 #define _COMMON_H
9
10 #ifdef DEBUG
11 #define p_debug(str, ...)       \
12         perr(PD, 0, "%s: " str, __func__ , __VA_ARGS__)
13 #else
14 #define p_debug(str, ...)       do {const char *s = __VA_ARGS__} while (0)
15 #endif
16
17 #define always_assert(condition) \
18         do { \
19                 if (!(condition)) \
20                 perr(PFE, 1, "%s:%d: Assertion failed: " # condition, \
21                                 __FILE__, __LINE__); \
22         } while (0)
23
24 #ifdef DEBUG
25 #define assert(condition) \
26         do { \
27                 if (!(condition)) \
28                 perr(PFE, 0, "%s:%d: Assertion failed: " # condition, \
29                                 __FILE__, __LINE__); \
30         } while (0)
31 #else
32 #define assert(condition)       do { } while (0)
33 #endif
34
35 #define PERR_BUF_SIZE           2048    
36 #define HOSTNAME_BUF_SIZE       100
37
38 /* Compiler-specific stuff */
39 #define VAR_MAY_BE_UNUSED(x)    ((void)(x))
40
41 /*
42  * Function prototypes and extern definitions
43  */
44
45 /* Perr fatal error, error, information, warning, debug */
46 enum perr_type { PFE = 1, PE = 0, PI = -1, PW = -2, PD = -3 };
47
48 void init_perr(char *prog_name);
49 void perr_func(int type, int want_errno, char *fmt, ...)
50         __attribute__ ((format (printf, 3, 4)));
51
52 /* No inline form of perr can be used, since it is variadic (See gcc manual) */
53
54 #define __fmt2(fmt0, arg0, arg1, fmt1, ...) fmt0 fmt1 "%s", arg0, arg1, __VA_ARGS__
55
56 #ifdef DEBUG
57 #define perr(type, want_errno, ...) \
58         perr_func(type, want_errno, \
59                 __fmt2("%s: %d: ", __func__, __LINE__, __VA_ARGS__, ""))
60 #else
61 #define perr(type, want_errno, ...) \
62         do {                        \
63                 if (type > PD)      \
64                 perr_func(type, want_errno, \
65                         __fmt2("%s: %d: ", __func__, __LINE__, __VA_ARGS__, "")); \
66         } while (0)
67 #endif
68
69 /* String manipulation */
70 size_t strlcpy(char *dst, const char *src, size_t siz);
71 size_t strlcat(char *dst, const char *src, size_t siz);
72
73 #endif  /* _COMMON_H */