1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef SU_DEBUG_H
26 /** Defined when <sofia-sip/su_debug.h> has been included */
27 #define SU_DEBUG_H
28 
29 /**@ingroup su_log
30  * @file sofia-sip/su_debug.h
31  * @brief SU debugging macros
32  *
33  * The logging levels and macros to use are defined as follows:
34  *  - SU_DEBUG_0()  fatal errors, panic
35  *  - SU_DEBUG_1()  critical errors, minimal progress at subsystem level
36  *  - SU_DEBUG_2()  non-critical errors
37  *  - SU_DEBUG_3()  warnings, progress messages
38  *  - SU_DEBUG_5()  signaling protocol actions (incoming packets, etc.)
39  *  - SU_DEBUG_7()  media protocol actions (incoming packets, etc.)
40  *  - SU_DEBUG_9()  entering/exiting functions, very verbatim progress
41  *
42  * These macros are used to log with module-specific levels. The SU_LOG
43  * macro is redefined with a pointer to a module-specific #su_log_t
44  * structure, e.g., "iptsec_debug.h".
45  *
46  * @author Pekka Pessi <Pekka.Pessi@nokia.com>
47  *
48  * @date Created: Tue Feb  8 10:06:33 2000 ppessi
49  *
50  * @sa @ref debug_logs, su_llog(), su_vllog(), #su_log_t,
51  */
52 
53 #ifndef SU_LOG_H
54 #include <sofia-sip/su_log.h>
55 #endif
56 
57 SOFIA_BEGIN_DECLS
58 
59 #ifndef SU_DEBUG_MAX
60 /** The maximum debugging level. */
61 #define SU_DEBUG_MAX 9
62 #endif
63 
64 #define SU_LOG_LEVEL \
65 ((SU_LOG != NULL && SU_LOG->log_init) == 0 ? 9 : \
66 ((SU_LOG != NULL && SU_LOG->log_init > 1) ? \
67   SU_LOG->log_level : su_log_default->log_level))
68 
69 #if SU_DEBUG_MAX >= 0
70 #ifndef SU_LOG
71 #define SU_LOG       (su_log_default)
72 #else
73 SOFIAPUBVAR su_log_t SU_LOG[];
74 #endif
75 
76 #define VA_NONE "%s",""
77 
78 #define SU_DEBUG_DEF(level) \
79   su_inline void su_debug_##level(char const *fmt, ...) \
80     __attribute__ ((__format__ (printf, 1, 2))); \
81   su_inline void su_debug_##level(char const *fmt, ...) \
82   { va_list ap; va_start(ap, fmt); su_vllog(SU_LOG, level, fmt, ap); va_end(ap); }
83 
84 //SU_DEBUG_DEF(0)
85 #define su_debug_0(_f, ...) su_llog(SU_LOG, 0, _f, __VA_ARGS__)
86 
87 /** Log messages at level 0.
88  *
89  * Fatal errors and panic messages should be logged at level 0.
90  *
91  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
92  */
93 #define SU_DEBUG_0(x) (SU_LOG_LEVEL >= 0 ? (su_debug_0 x) : (void)0)
94 
95 /** Log C library errors. */
96 #define SU_LERROR(s) (su_llog(SU_LOG, 1, "%s: %s\n", (s), strerror(errno)))
97 /** Log socket errors. */
98 #define SU_LSERROR(s) \
99   (su_llog(SU_LOG, 1, "%s: %s\n", (s), su_strerror(su_errno())))
100 #else
101 #define SU_DEBUG_0(x) ((void)0)
102 #define SU_LERROR(s)  ((void)0)
103 #define SU_LSERROR(s) ((void)0)
104 #endif
105 
106 #if SU_DEBUG_MAX >= 1
107 //SU_DEBUG_DEF(1)
108 #define su_debug_1(_f, ...) su_llog(SU_LOG, 1, _f, __VA_ARGS__)
109 
110 
111 /**Log messages at level 1.
112  *
113  * Critical errors and minimal progress at subsystem level should be logged
114  * at level 1.
115  *
116  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
117  */
118 #define SU_DEBUG_1(x) (SU_LOG_LEVEL >= 1 ? (su_debug_1 x) : (void)0)
119 #else
120 #define SU_DEBUG_1(x) (void)1
121 #endif
122 
123 #if SU_DEBUG_MAX >= 2
124 //SU_DEBUG_DEF(2)
125 #define su_debug_2(_f, ...) su_llog(SU_LOG, 2, _f, __VA_ARGS__)
126 /**Log messages at level 2.
127  *
128  * Non-critical errors should be logged at level 2.
129  *
130  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
131  */
132 #define SU_DEBUG_2(x) (SU_LOG_LEVEL >= 2 ? (su_debug_2 x) : (void)0)
133 #else
134 #define SU_DEBUG_2(x) (void)2
135 #endif
136 
137 #if SU_DEBUG_MAX >= 3
138 //SU_DEBUG_DEF(3)
139 #define su_debug_3(_f, ...) su_llog(SU_LOG, 3, _f, __VA_ARGS__)
140 /** Log messages at level 3.
141  *
142  * Warnings and progress messages should be logged at level 3.
143  *
144  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
145  */
146 #define SU_DEBUG_3(x) (SU_LOG_LEVEL >= 3 ? (su_debug_3 x) : (void)0)
147 #else
148 #define SU_DEBUG_3(x) (void)3
149 #endif
150 
151 #if SU_DEBUG_MAX >= 4
152 //SU_DEBUG_DEF(4)
153 #define su_debug_4(_f, ...) su_llog(SU_LOG, 4, _f, __VA_ARGS__)
154 /** Log messages at level 4. */
155 #define SU_DEBUG_4(x) (SU_LOG_LEVEL >= 4 ? (su_debug_4 x) : (void)0)
156 #else
157 #define SU_DEBUG_4(x) (void)4
158 #endif
159 
160 #if SU_DEBUG_MAX >= 5
161 //SU_DEBUG_DEF(5)
162 #define su_debug_5(_f, ...) su_llog(SU_LOG, 5, _f, __VA_ARGS__)
163 /** Log messages at level 5.
164  *
165  * Signaling protocol actions (incoming packets, etc.) should be logged
166  * at level 5.
167  *
168  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
169  */
170 #define SU_DEBUG_5(x) (SU_LOG_LEVEL >= 5 ? (su_debug_5 x) : (void)0)
171 #else
172 #define SU_DEBUG_5(x) (void)5
173 #endif
174 
175 #if SU_DEBUG_MAX >= 6
176 //SU_DEBUG_DEF(6)
177 #define su_debug_6(_f, ...) su_llog(SU_LOG, 6, _f, __VA_ARGS__)
178 /** Log messages at level 6. */
179 #define SU_DEBUG_6(x) (SU_LOG_LEVEL >= 6 ? (su_debug_6 x) : (void)0)
180 #else
181 #define SU_DEBUG_6(x) (void)6
182 #endif
183 
184 #if SU_DEBUG_MAX >= 7
185 //SU_DEBUG_DEF(7)
186 #define su_debug_7(_f, ...) su_llog(SU_LOG, 7, _f, __VA_ARGS__)
187 /** Log messages at level 7.
188  *
189  * Media protocol actions (incoming packets, etc) should be logged at level 7.
190  *
191  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
192  */
193 #define SU_DEBUG_7(x) (SU_LOG_LEVEL >= 7 ? (su_debug_7 x) : (void)0)
194 #else
195 #define SU_DEBUG_7(x) (void)7
196 #endif
197 
198 #if SU_DEBUG_MAX >= 8
199 //SU_DEBUG_DEF(8)
200 #define su_debug_8(_f, ...) su_llog(SU_LOG, 8, _f, __VA_ARGS__)
201 /** Log messages at level 8. */
202 #define SU_DEBUG_8(x) (SU_LOG_LEVEL >= 8 ? (su_debug_8 x) : (void)0)
203 #else
204 #define SU_DEBUG_8(x) (void)8
205 #endif
206 
207 #if SU_DEBUG_MAX >= 9
208 //SU_DEBUG_DEF(9)
209 #define su_debug_9(_f, ...) su_llog(SU_LOG, 9, _f, __VA_ARGS__)
210 /** Log messages at level 9.
211  *
212  * Entering/exiting functions, very verbatim progress should be logged at
213  * level 9.
214  *
215  * @sa su_llog(), su_vllog(), #su_log_t, @ref debug_logs
216  */
217 #define SU_DEBUG_9(x) (SU_LOG_LEVEL >= 9 ? (su_debug_9 x) : (void)0)
218 #else
219 #define SU_DEBUG_9(x) (void)9
220 #endif
221 
222 SOFIA_END_DECLS
223 
224 #endif /* SU_DEBUG_H */
225