1 /*
2 Copyright (c) 2003-2006 by Juliusz Chroboczek
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #define L_ERROR 0x1
24 #define L_WARN 0x2
25 #define L_INFO 0x4
26 #define L_FORBIDDEN 0x8
27 #define L_UNCACHEABLE 0x10
28 #define L_SUPERSEDED 0x20
29 #define L_VARY 0x40
30 #define L_TUNNEL 0x80
31 
32 #define D_SERVER_CONN 0x100
33 #define D_SERVER_REQ 0x200
34 #define D_CLIENT_CONN 0x400
35 #define D_CLIENT_REQ 0x800
36 #define D_ATOM_REFCOUNT 0x1000
37 #define D_REFCOUNT 0x2000
38 #define D_LOCK 0x4000
39 #define D_OBJECT 0x8000
40 #define D_OBJECT_DATA 0x10000
41 #define D_SERVER_OFFSET 0x20000
42 #define D_CLIENT_DATA 0x40000
43 #define D_DNS 0x80000
44 #define D_CHILD 0x100000
45 #define D_IO 0x200000
46 
47 #define LOGGING_DEFAULT (L_ERROR | L_WARN | L_INFO)
48 #define LOGGING_MAX 0xFF
49 
50 extern int scrubLogs;
51 
52 void preinitLog(void);
53 void initLog(void);
54 void reopenLog(void);
55 void flushLog(void);
56 int loggingToStderr(void);
57 
58 void really_do_log(int type, const char *f, ...)
59     ATTRIBUTE ((format (printf, 2, 3)));
60 void really_do_log_v(int type, const char *f, va_list args)
61     ATTRIBUTE ((format (printf, 2, 0)));
62 void really_do_log_n(int type, const char *s, int n);
63 void really_do_log_error(int type, int e, const char *f, ...)
64     ATTRIBUTE ((format (printf, 3, 4)));
65 void really_do_log_error_v(int type, int e, const char *f, va_list args)
66     ATTRIBUTE ((format (printf, 3, 0)));
67 const char *scrub(const char *message);
68 
69 #ifdef __GNUC__
70 #define DO_BACKTRACE()                  \
71   do {                                  \
72     int n;                              \
73     void *buffer[10];                   \
74     n = backtrace(buffer, 5);           \
75     fflush(stderr);                     \
76     backtrace_symbols_fd(buffer, n, 2); \
77  } while(0)
78 #else
79 #define DO_BACKTRACE() /* */
80 #endif
81 
82 /* These are macros because it's important that they should be
83    optimised away. */
84 
85 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
86 
87 #define do_log(_type, ...)                                           \
88     do {                                                             \
89         if((_type) & (LOGGING_MAX)) really_do_log((_type), __VA_ARGS__); \
90     } while(0)
91 #define do_log_error(_type, _e, ...)                                 \
92     do {                                                             \
93         if((_type) & (LOGGING_MAX))                                  \
94             really_do_log_error((_type), (_e), __VA_ARGS__);         \
95     } while(0)
96 
97 #elif defined(__GNUC__)
98 
99 #define do_log(_type, _args...)                                \
100     do {                                                       \
101         if((_type) & (LOGGING_MAX)) really_do_log((_type), _args); \
102     } while(0)
103 #define do_log_error(_type, _e, _args...)                      \
104     do {                                                       \
105         if((_type) & (LOGGING_MAX))                            \
106             really_do_log_error((_type), (_e), _args);         \
107     } while(0)
108 
109 #else
110 
111 /* No variadic macros -- let's hope inline works. */
112 
113 static inline void
do_log(int type,const char * f,...)114 do_log(int type, const char *f, ...)
115 {
116     va_list args;
117 
118     va_start(args, f);
119     if((type & (LOGGING_MAX)) != 0)
120         really_do_log_v(type, f, args);
121     va_end(args);
122 }
123 
124 static inline void
do_log_error(int type,int e,const char * f,...)125 do_log_error(int type, int e, const char *f, ...)
126 {
127     va_list args;
128 
129     va_start(args, f);
130     if((type & (LOGGING_MAX)) != 0)
131         really_do_log_error_v(type, e, f, args);
132     va_end(args);
133 }
134 
135 #endif
136 
137 #define do_log_n(_type, _s, _n) \
138     do { \
139         if((_type) & (LOGGING_MAX)) really_do_log_n((_type), (_s), (_n)); \
140     } while(0)
141