~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/include/impl/messages.h

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 
2
 * 
 
3
 * basic message reporting.
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
 
23
#       error "Only <framework.h> can be included directly."
 
24
#endif
 
25
 
 
26
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
27
#       pragma once
 
28
#endif
 
29
#ifndef __PGM_IMPL_MESSAGES_H__
 
30
#define __PGM_IMPL_MESSAGES_H__
 
31
 
 
32
#ifdef _MSC_VER
 
33
#       include <pgm/wininttypes.h>
 
34
#else
 
35
#       include <inttypes.h>
 
36
#endif
 
37
#include <pgm/zinttypes.h>
 
38
#include <stdarg.h>
 
39
#include <stdlib.h>
 
40
#include <pgm/types.h>
 
41
#include <pgm/messages.h>
 
42
 
 
43
PGM_BEGIN_DECLS
 
44
 
 
45
PGM_GNUC_INTERNAL void pgm__log  (const int, const char*, ...) PGM_GNUC_PRINTF (2, 3);
 
46
PGM_GNUC_INTERNAL void pgm__logv (const int, const char*, va_list) PGM_GNUC_PRINTF (2, 0);
 
47
 
 
48
#ifdef CONFIG_HAVE_ISO_VARARGS
 
49
 
 
50
/* debug trace level only valid in debug mode */
 
51
#       ifdef PGM_DEBUG
 
52
#               define pgm_debug(...) \
 
53
                        do { \
 
54
                                if (pgm_min_log_level == PGM_LOG_LEVEL_DEBUG) \
 
55
                                        pgm__log (PGM_LOG_LEVEL_DEBUG, __VA_ARGS__); \
 
56
                        } while (0)
 
57
#       else
 
58
#               define pgm_debug(...)   while (0)
 
59
#       endif /* !PGM_DEBUG */
 
60
 
 
61
#       define pgm_trace(r,...) \
 
62
                        do { \
 
63
                                if (pgm_min_log_level <= PGM_LOG_LEVEL_TRACE && pgm_log_mask & (r)) \
 
64
                                        pgm__log (PGM_LOG_LEVEL_TRACE, __VA_ARGS__); \
 
65
                        } while (0)
 
66
#       define pgm_minor(...) \
 
67
                        do { \
 
68
                                if (pgm_min_log_level <= PGM_LOG_LEVEL_MINOR) \
 
69
                                        pgm__log (PGM_LOG_LEVEL_MINOR, __VA_ARGS__); \
 
70
                        } while (0)
 
71
#       define pgm_info(...) \
 
72
                        do { \
 
73
                                if (pgm_min_log_level <= PGM_LOG_LEVEL_NORMAL) \
 
74
                                        pgm__log (PGM_LOG_LEVEL_NORMAL, __VA_ARGS__); \
 
75
                        } while (0)
 
76
#       define pgm_warn(...) \
 
77
                        do { \
 
78
                                if (pgm_min_log_level <= PGM_LOG_LEVEL_WARNING) \
 
79
                                        pgm__log (PGM_LOG_LEVEL_WARNING, __VA_ARGS__); \
 
80
                        } while (0)
 
81
#       define pgm_error(...) \
 
82
                        do { \
 
83
                                if (pgm_min_log_level <= PGM_LOG_LEVEL_ERROR) \
 
84
                                        pgm__log (PGM_LOG_LEVEL_ERROR, __VA_ARGS__); \
 
85
                        } while (0)
 
86
#       define pgm_fatal(...) \
 
87
                        do { \
 
88
                                pgm__log (PGM_LOG_LEVEL_FATAL, __VA_ARGS__); \
 
89
                        } while (0)
 
90
 
 
91
#elif defined(CONFIG_HAVE_GNUC_VARARGS)
 
92
 
 
93
#       ifdef PGM_DEBUG
 
94
#               define pgm_debug(f...) \
 
95
                        do { \
 
96
                                if (pgm_min_log_level == PGM_LOG_LEVEL_DEBUG) \
 
97
                                        pgm__log (PGM_LOG_LEVEL_DEBUG, f); \
 
98
                        } while (0)
 
99
#       else
 
100
#               define pgm_debug(f...)  while (0)
 
101
#       endif /* !PGM_DEBUG */
 
102
 
 
103
#       define pgm_trace(r,f...)        if (pgm_min_log_level <= PGM_LOG_LEVEL_TRACE && pgm_log_mask & (r)) \
 
104
                                        pgm__log (PGM_LOG_LEVEL_TRACE, f)
 
105
#       define pgm_minor(f...)          if (pgm_min_log_level <= PGM_LOG_LEVEL_MINOR) pgm__log (PGM_LOG_LEVEL_MINOR, f)
 
106
#       define pgm_info(f...)           if (pgm_min_log_level <= PGM_LOG_LEVEL_NORMAL) pgm__log (PGM_LOG_LEVEL_NORMAL, f)
 
107
#       define pgm_warn(f...)           if (pgm_min_log_level <= PGM_LOG_LEVEL_WARNING) pgm__log (PGM_LOG_LEVEL_WARNING, f)
 
108
#       define pgm_error(f...)          if (pgm_min_log_level <= PGM_LOG_LEVEL_ERROR) pgm__log (PGM_LOG_LEVEL_ERROR, f)
 
109
#       define pgm_fatal(f...)          pgm__log (PGM_LOG_LEVEL_FATAL, f)
 
110
 
 
111
#else   /* no varargs macros */
 
112
 
 
113
/* declare for GCC attributes */
 
114
static inline void pgm_debug (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
115
static inline void pgm_trace (const int, const char*, ...) PGM_GNUC_PRINTF (2, 3);
 
116
static inline void pgm_minor (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
117
static inline void pgm_info (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
118
static inline void pgm_warn (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
119
static inline void pgm_error (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
120
static inline void pgm_fatal (const char*, ...) PGM_GNUC_PRINTF (1, 2);
 
121
 
 
122
static inline void pgm_debug (const char* format, ...) {
 
123
        if (PGM_LOG_LEVEL_DEBUG == pgm_min_log_level) {
 
124
                va_list args;
 
125
                va_start (args, format);
 
126
                pgm__logv (PGM_LOG_LEVEL_DEBUG, format, args);
 
127
                va_end (args);
 
128
        }
 
129
}
 
130
 
 
131
static inline void pgm_trace (const int role, const char* format, ...) {
 
132
        if (PGM_LOG_LEVEL_TRACE >= pgm_min_log_level && pgm_log_mask & role) {
 
133
                va_list args;
 
134
                va_start (args, format);
 
135
                pgm__logv (PGM_LOG_LEVEL_TRACE, format, args);
 
136
                va_end (args);
 
137
        }
 
138
}
 
139
 
 
140
static inline void pgm_minor (const char* format, ...) {
 
141
        if (PGM_LOG_LEVEL_MINOR >= pgm_min_log_level) {
 
142
                va_list args;
 
143
                va_start (args, format);
 
144
                pgm__logv (PGM_LOG_LEVEL_MINOR, format, args);
 
145
                va_end (args);
 
146
        }
 
147
}
 
148
 
 
149
static inline void pgm_info (const char* format, ...) {
 
150
        if (PGM_LOG_LEVEL_NORMAL >= pgm_min_log_level) {
 
151
                va_list args;
 
152
                va_start (args, format);
 
153
                pgm__logv (PGM_LOG_LEVEL_NORMAL, format, args);
 
154
                va_end (args);
 
155
        }
 
156
}
 
157
 
 
158
static inline void pgm_warn (const char* format, ...) {
 
159
        if (PGM_LOG_LEVEL_WARNING >= pgm_min_log_level) {
 
160
                va_list args;
 
161
                va_start (args, format);
 
162
                pgm__logv (PGM_LOG_LEVEL_WARNING, format, args);
 
163
                va_end (args);
 
164
        }
 
165
}
 
166
 
 
167
static inline void pgm_error (const char* format, ...) {
 
168
        if (PGM_LOG_LEVEL_ERROR >= pgm_min_log_level) {
 
169
                va_list args;
 
170
                va_start (args, format);
 
171
                pgm__logv (PGM_LOG_LEVEL_WARNING, format, args);
 
172
                va_end (args);
 
173
        }
 
174
}
 
175
 
 
176
static inline void pgm_fatal (const char* format, ...) {
 
177
        va_list args;
 
178
        va_start (args, format);
 
179
        pgm__logv (PGM_LOG_LEVEL_FATAL, format, args);
 
180
        va_end (args);
 
181
}
 
182
 
 
183
#endif /* varargs */
 
184
 
 
185
#define pgm_warn_if_reached() \
 
186
        do { \
 
187
                pgm_warn ("file %s: line %d (%s): code should not be reached", \
 
188
                                __FILE__, __LINE__, __PRETTY_FUNCTION__); \
 
189
        } while (0)
 
190
#define pgm_warn_if_fail(expr) \
 
191
        do { \
 
192
                if (PGM_LIKELY (expr)); \
 
193
                else \
 
194
                        pgm_warn ("file %s: line %d (%s): runtime check failed: (%s)", \
 
195
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
 
196
        } while (0)
 
197
 
 
198
 
 
199
#ifdef PGM_DISABLE_ASSERT
 
200
 
 
201
#       define pgm_assert(expr)                 while (0)
 
202
#       define pgm_assert_not_reached()         while (0)
 
203
#       define pgm_assert_cmpint(n1, cmp, n2)   while (0)
 
204
#       define pgm_assert_cmpuint(n1, cmp, n2)  while (0)
 
205
 
 
206
#elif defined(__GNUC__)
 
207
 
 
208
#       define pgm_assert(expr) \
 
209
        do { \
 
210
                if (PGM_LIKELY(expr)); \
 
211
                else { \
 
212
                        pgm_fatal ("file %s: line %d (%s): assertion failed: (%s)", \
 
213
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
 
214
                        abort (); \
 
215
                } \
 
216
        } while (0)
 
217
#       define pgm_assert_not_reached() \
 
218
        do { \
 
219
                pgm_fatal ("file %s: line %d (%s): should not be reached", \
 
220
                        __FILE__, __LINE__, __PRETTY_FUNCTION__); \
 
221
                abort (); \
 
222
        } while (0)
 
223
#       define pgm_assert_cmpint(n1, cmp, n2) \
 
224
        do { \
 
225
                const int64_t _n1 = (n1), _n2 = (n2); \
 
226
                if (PGM_LIKELY(_n1 cmp _n2)); \
 
227
                else { \
 
228
                        pgm_fatal ("file %s: line %d (%s): assertion failed (%s): (%" PRIi64 " %s %" PRIi64 ")", \
 
229
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
 
230
                        abort (); \
 
231
                } \
 
232
        } while (0)
 
233
#       define pgm_assert_cmpuint(n1, cmp, n2) \
 
234
        do { \
 
235
                const uint64_t _n1 = (n1), _n2 = (n2); \
 
236
                if (PGM_LIKELY(_n1 cmp _n2)); \
 
237
                else { \
 
238
                        pgm_fatal ("file %s: line %d (%s): assertion failed (%s): (%" PRIu64 " %s %" PRIu64 ")", \
 
239
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
 
240
                        abort (); \
 
241
                } \
 
242
        } while (0)
 
243
 
 
244
#else
 
245
 
 
246
#       define pgm_assert(expr) \
 
247
        do { \
 
248
                if (PGM_LIKELY(expr)); \
 
249
                else { \
 
250
                        pgm_fatal ("file %s: line %d: assertion failed: (%s)", \
 
251
                                __FILE__, __LINE__, #expr); \
 
252
                        abort (); \
 
253
                } \
 
254
        } while (0)
 
255
#       define pgm_assert_not_reached() \
 
256
        do { \
 
257
                pgm_fatal ("file %s: line %d: assertion failed: (%s)", \
 
258
                        __FILE__, __LINE__); \
 
259
                abort (); \
 
260
        } while (0)
 
261
#       define pgm_assert_cmpint(n1, cmp, n2) \
 
262
        do { \
 
263
                const int64_t _n1 = (n1), _n2 = (n2); \
 
264
                if (PGM_LIKELY(_n1 cmp _n2)); \
 
265
                else { \
 
266
                        pgm_fatal ("file %s: line %d: assertion failed (%s): (%" PRIi64 " %s %" PRIi64 ")", \
 
267
                                __FILE__, __LINE__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
 
268
                        abort (); \
 
269
                } \
 
270
        } while (0)
 
271
#       define pgm_assert_cmpuint(n1, cmp, n2) \
 
272
        do { \
 
273
                const uint64_t _n1 = (n1), _n2 = (n2); \
 
274
                if (PGM_LIKELY(_n1 cmp _n2)); \
 
275
                else { \
 
276
                        pgm_fatal ("file %s: line %d: assertion failed (%s): (%" PRIu64 " %s %" PRIu64 ")", \
 
277
                                __FILE__, __LINE__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
 
278
                        abort (); \
 
279
                } \
 
280
        } while (0)
 
281
 
 
282
#endif /* !PGM_DISABLE_ASSERT */
 
283
 
 
284
#ifdef PGM_DISABLE_CHECKS
 
285
 
 
286
#       define pgm_return_if_fail(expr)                 while (0)
 
287
#       define pgm_return_val_if_fail(expr, val)        while (0)
 
288
#       define pgm_return_if_reached()                  return
 
289
#       define pgm_return_val_if_reached(val)           return (val)
 
290
 
 
291
#elif defined(__GNUC__)
 
292
 
 
293
#       define pgm_return_if_fail(expr) \
 
294
        do { \
 
295
                if (PGM_LIKELY(expr)); \
 
296
                else { \
 
297
                        pgm_warn ("file %s: line %d (%s): assertion `%s' failed", \
 
298
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
 
299
                        return; \
 
300
                } \
 
301
        } while (0)
 
302
#       define pgm_return_val_if_fail(expr, val) \
 
303
        do { \
 
304
                if (PGM_LIKELY(expr)); \
 
305
                else { \
 
306
                        pgm_warn ("file %s: line %d (%s): assertion `%s' failed", \
 
307
                                __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
 
308
                        return (val); \
 
309
                } \
 
310
        } while (0)
 
311
#       define pgm_return_if_reached() \
 
312
        do { \
 
313
                pgm_warn ("file %s: line %d (%s): should not be reached", \
 
314
                        __FILE__, __LINE__, __PRETTY_FUNCTION__); \
 
315
                return; \
 
316
        } while (0)
 
317
#       define pgm_return_val_if_reached(val) \
 
318
        do { \
 
319
                pgm_warn ("file %s: line %d (%s): should not be reached", \
 
320
                        __FILE__, __LINE__, __PRETTY_FUNCTION__); \
 
321
                return (val); \
 
322
        } while (0)
 
323
 
 
324
#else
 
325
 
 
326
#       define pgm_return_if_fail(expr) \
 
327
        do { \
 
328
                if (PGM_LIKELY(expr)); \
 
329
                else { \
 
330
                        pgm_warn ("file %s: line %d: assertion `%s' failed", \
 
331
                                __FILE__, __LINE__, #expr); \
 
332
                        return; \
 
333
                } \
 
334
        } while (0)
 
335
#       define pgm_return_val_if_fail(expr, val) \
 
336
        do { \
 
337
                if (PGM_LIKELY(expr)); \
 
338
                else { \
 
339
                        pgm_warn ("file %s: line %d: assertion `%s' failed", \
 
340
                                __FILE__, __LINE__, #expr); \
 
341
                        return (val); \
 
342
                } \
 
343
        } while (0)
 
344
#       define pgm_return_if_reached() \
 
345
        do { \
 
346
                pgm_warn ("file %s: line %d): should not be reached", \
 
347
                        __FILE__, __LINE__); \
 
348
                return; \
 
349
        } while (0)
 
350
#       define pgm_return_val_if_reached(val) \
 
351
        do { \
 
352
                pgm_warn ("file %s: line %d: should not be reached", \
 
353
                        __FILE__, __LINE__); \
 
354
                return (val); \
 
355
        } while (0)
 
356
 
 
357
#endif /* !PGM_DISABLE_CHECKS */
 
358
 
 
359
PGM_END_DECLS
 
360
 
 
361
#endif /* __PGM_IMPL_MESSAGES_H__ */