1
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
3
* basic message reporting.
5
* Copyright (c) 2010 Miru Limited.
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.
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.
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
22
#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
23
# error "Only <framework.h> can be included directly."
26
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
29
#ifndef __PGM_IMPL_MESSAGES_H__
30
#define __PGM_IMPL_MESSAGES_H__
33
# include <pgm/wininttypes.h>
35
# include <inttypes.h>
37
#include <pgm/zinttypes.h>
40
#include <pgm/types.h>
41
#include <pgm/messages.h>
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);
48
#ifdef CONFIG_HAVE_ISO_VARARGS
50
/* debug trace level only valid in debug mode */
52
# define pgm_debug(...) \
54
if (pgm_min_log_level == PGM_LOG_LEVEL_DEBUG) \
55
pgm__log (PGM_LOG_LEVEL_DEBUG, __VA_ARGS__); \
58
# define pgm_debug(...) while (0)
59
# endif /* !PGM_DEBUG */
61
# define pgm_trace(r,...) \
63
if (pgm_min_log_level <= PGM_LOG_LEVEL_TRACE && pgm_log_mask & (r)) \
64
pgm__log (PGM_LOG_LEVEL_TRACE, __VA_ARGS__); \
66
# define pgm_minor(...) \
68
if (pgm_min_log_level <= PGM_LOG_LEVEL_MINOR) \
69
pgm__log (PGM_LOG_LEVEL_MINOR, __VA_ARGS__); \
71
# define pgm_info(...) \
73
if (pgm_min_log_level <= PGM_LOG_LEVEL_NORMAL) \
74
pgm__log (PGM_LOG_LEVEL_NORMAL, __VA_ARGS__); \
76
# define pgm_warn(...) \
78
if (pgm_min_log_level <= PGM_LOG_LEVEL_WARNING) \
79
pgm__log (PGM_LOG_LEVEL_WARNING, __VA_ARGS__); \
81
# define pgm_error(...) \
83
if (pgm_min_log_level <= PGM_LOG_LEVEL_ERROR) \
84
pgm__log (PGM_LOG_LEVEL_ERROR, __VA_ARGS__); \
86
# define pgm_fatal(...) \
88
pgm__log (PGM_LOG_LEVEL_FATAL, __VA_ARGS__); \
91
#elif defined(CONFIG_HAVE_GNUC_VARARGS)
94
# define pgm_debug(f...) \
96
if (pgm_min_log_level == PGM_LOG_LEVEL_DEBUG) \
97
pgm__log (PGM_LOG_LEVEL_DEBUG, f); \
100
# define pgm_debug(f...) while (0)
101
# endif /* !PGM_DEBUG */
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)
111
#else /* no varargs macros */
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);
122
static inline void pgm_debug (const char* format, ...) {
123
if (PGM_LOG_LEVEL_DEBUG == pgm_min_log_level) {
125
va_start (args, format);
126
pgm__logv (PGM_LOG_LEVEL_DEBUG, format, args);
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) {
134
va_start (args, format);
135
pgm__logv (PGM_LOG_LEVEL_TRACE, format, args);
140
static inline void pgm_minor (const char* format, ...) {
141
if (PGM_LOG_LEVEL_MINOR >= pgm_min_log_level) {
143
va_start (args, format);
144
pgm__logv (PGM_LOG_LEVEL_MINOR, format, args);
149
static inline void pgm_info (const char* format, ...) {
150
if (PGM_LOG_LEVEL_NORMAL >= pgm_min_log_level) {
152
va_start (args, format);
153
pgm__logv (PGM_LOG_LEVEL_NORMAL, format, args);
158
static inline void pgm_warn (const char* format, ...) {
159
if (PGM_LOG_LEVEL_WARNING >= pgm_min_log_level) {
161
va_start (args, format);
162
pgm__logv (PGM_LOG_LEVEL_WARNING, format, args);
167
static inline void pgm_error (const char* format, ...) {
168
if (PGM_LOG_LEVEL_ERROR >= pgm_min_log_level) {
170
va_start (args, format);
171
pgm__logv (PGM_LOG_LEVEL_WARNING, format, args);
176
static inline void pgm_fatal (const char* format, ...) {
178
va_start (args, format);
179
pgm__logv (PGM_LOG_LEVEL_FATAL, format, args);
185
#define pgm_warn_if_reached() \
187
pgm_warn ("file %s: line %d (%s): code should not be reached", \
188
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
190
#define pgm_warn_if_fail(expr) \
192
if (PGM_LIKELY (expr)); \
194
pgm_warn ("file %s: line %d (%s): runtime check failed: (%s)", \
195
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
199
#ifdef PGM_DISABLE_ASSERT
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)
206
#elif defined(__GNUC__)
208
# define pgm_assert(expr) \
210
if (PGM_LIKELY(expr)); \
212
pgm_fatal ("file %s: line %d (%s): assertion failed: (%s)", \
213
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
217
# define pgm_assert_not_reached() \
219
pgm_fatal ("file %s: line %d (%s): should not be reached", \
220
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
223
# define pgm_assert_cmpint(n1, cmp, n2) \
225
const int64_t _n1 = (n1), _n2 = (n2); \
226
if (PGM_LIKELY(_n1 cmp _n2)); \
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); \
233
# define pgm_assert_cmpuint(n1, cmp, n2) \
235
const uint64_t _n1 = (n1), _n2 = (n2); \
236
if (PGM_LIKELY(_n1 cmp _n2)); \
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); \
246
# define pgm_assert(expr) \
248
if (PGM_LIKELY(expr)); \
250
pgm_fatal ("file %s: line %d: assertion failed: (%s)", \
251
__FILE__, __LINE__, #expr); \
255
# define pgm_assert_not_reached() \
257
pgm_fatal ("file %s: line %d: assertion failed: (%s)", \
258
__FILE__, __LINE__); \
261
# define pgm_assert_cmpint(n1, cmp, n2) \
263
const int64_t _n1 = (n1), _n2 = (n2); \
264
if (PGM_LIKELY(_n1 cmp _n2)); \
266
pgm_fatal ("file %s: line %d: assertion failed (%s): (%" PRIi64 " %s %" PRIi64 ")", \
267
__FILE__, __LINE__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
271
# define pgm_assert_cmpuint(n1, cmp, n2) \
273
const uint64_t _n1 = (n1), _n2 = (n2); \
274
if (PGM_LIKELY(_n1 cmp _n2)); \
276
pgm_fatal ("file %s: line %d: assertion failed (%s): (%" PRIu64 " %s %" PRIu64 ")", \
277
__FILE__, __LINE__, #n1 " " #cmp " " #n2, _n1, #cmp, _n2); \
282
#endif /* !PGM_DISABLE_ASSERT */
284
#ifdef PGM_DISABLE_CHECKS
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)
291
#elif defined(__GNUC__)
293
# define pgm_return_if_fail(expr) \
295
if (PGM_LIKELY(expr)); \
297
pgm_warn ("file %s: line %d (%s): assertion `%s' failed", \
298
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
302
# define pgm_return_val_if_fail(expr, val) \
304
if (PGM_LIKELY(expr)); \
306
pgm_warn ("file %s: line %d (%s): assertion `%s' failed", \
307
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
311
# define pgm_return_if_reached() \
313
pgm_warn ("file %s: line %d (%s): should not be reached", \
314
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
317
# define pgm_return_val_if_reached(val) \
319
pgm_warn ("file %s: line %d (%s): should not be reached", \
320
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
326
# define pgm_return_if_fail(expr) \
328
if (PGM_LIKELY(expr)); \
330
pgm_warn ("file %s: line %d: assertion `%s' failed", \
331
__FILE__, __LINE__, #expr); \
335
# define pgm_return_val_if_fail(expr, val) \
337
if (PGM_LIKELY(expr)); \
339
pgm_warn ("file %s: line %d: assertion `%s' failed", \
340
__FILE__, __LINE__, #expr); \
344
# define pgm_return_if_reached() \
346
pgm_warn ("file %s: line %d): should not be reached", \
347
__FILE__, __LINE__); \
350
# define pgm_return_val_if_reached(val) \
352
pgm_warn ("file %s: line %d: should not be reached", \
353
__FILE__, __LINE__); \
357
#endif /* !PGM_DISABLE_CHECKS */
361
#endif /* __PGM_IMPL_MESSAGES_H__ */