Move the declaration of g_return_if_fail_warning() out of the ifdefs, so
[platform/upstream/glib.git] / glib / gmessages.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #ifndef __G_MESSAGES_H__
28 #define __G_MESSAGES_H__
29
30 #include <stdarg.h>
31 #include <glib/gtypes.h>
32 #include <glib/gmacros.h>
33
34 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
35  */
36 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
37 #pragma GCC system_header
38 #endif
39
40 G_BEGIN_DECLS
41
42 /* calculate a string size, guaranteed to fit format + args.
43  */
44 gsize   g_printf_string_upper_bound (const gchar* format,
45                                      va_list      args);
46
47 /* Log level shift offset for user defined
48  * log levels (0-7 are used by GLib).
49  */
50 #define G_LOG_LEVEL_USER_SHIFT  (8)
51
52 /* Glib log levels and flags.
53  */
54 typedef enum
55 {
56   /* log flags */
57   G_LOG_FLAG_RECURSION          = 1 << 0,
58   G_LOG_FLAG_FATAL              = 1 << 1,
59
60   /* GLib log levels */
61   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
62   G_LOG_LEVEL_CRITICAL          = 1 << 3,
63   G_LOG_LEVEL_WARNING           = 1 << 4,
64   G_LOG_LEVEL_MESSAGE           = 1 << 5,
65   G_LOG_LEVEL_INFO              = 1 << 6,
66   G_LOG_LEVEL_DEBUG             = 1 << 7,
67
68   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
69 } GLogLevelFlags;
70
71 /* GLib log levels that are considered fatal by default */
72 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
73
74 typedef void            (*GLogFunc)             (const gchar   *log_domain,
75                                                  GLogLevelFlags log_level,
76                                                  const gchar   *message,
77                                                  gpointer       user_data);
78
79 /* Logging mechanism
80  */
81 guint           g_log_set_handler       (const gchar    *log_domain,
82                                          GLogLevelFlags  log_levels,
83                                          GLogFunc        log_func,
84                                          gpointer        user_data);
85 void            g_log_remove_handler    (const gchar    *log_domain,
86                                          guint           handler_id);
87 void            g_log_default_handler   (const gchar    *log_domain,
88                                          GLogLevelFlags  log_level,
89                                          const gchar    *message,
90                                          gpointer        unused_data);
91 GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
92                                            gpointer      user_data);
93 void            g_log                   (const gchar    *log_domain,
94                                          GLogLevelFlags  log_level,
95                                          const gchar    *format,
96                                          ...) G_GNUC_PRINTF (3, 4);
97 void            g_logv                  (const gchar    *log_domain,
98                                          GLogLevelFlags  log_level,
99                                          const gchar    *format,
100                                          va_list         args);
101 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
102                                          GLogLevelFlags  fatal_mask);
103 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
104
105 /* internal */
106 void    _g_log_fallback_handler (const gchar   *log_domain,
107                                  GLogLevelFlags log_level,
108                                  const gchar   *message,
109                                  gpointer       unused_data) G_GNUC_INTERNAL;
110
111 /* Internal function, used to implement the following macros */
112 void g_return_if_fail_warning (const char *log_domain,
113                                const char *pretty_function,
114                                const char *expression);
115
116
117 #ifndef G_LOG_DOMAIN
118 #define G_LOG_DOMAIN    ((gchar*) 0)
119 #endif  /* G_LOG_DOMAIN */
120 #ifdef G_HAVE_ISO_VARARGS
121 #define g_error(...)    g_log (G_LOG_DOMAIN,         \
122                                G_LOG_LEVEL_ERROR,    \
123                                __VA_ARGS__)
124 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
125                                G_LOG_LEVEL_MESSAGE,  \
126                                __VA_ARGS__)
127 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
128                                G_LOG_LEVEL_CRITICAL, \
129                                __VA_ARGS__)
130 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
131                                G_LOG_LEVEL_WARNING,  \
132                                __VA_ARGS__)
133 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
134                                G_LOG_LEVEL_DEBUG,    \
135                                __VA_ARGS__)
136 #elif defined(G_HAVE_GNUC_VARARGS)
137 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
138                                        G_LOG_LEVEL_ERROR,    \
139                                        format)
140 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
141                                        G_LOG_LEVEL_MESSAGE,  \
142                                        format)
143 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
144                                        G_LOG_LEVEL_CRITICAL, \
145                                        format)
146 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
147                                        G_LOG_LEVEL_WARNING,  \
148                                        format)
149 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
150                                        G_LOG_LEVEL_WARNING,  \
151                                        format)
152 #else   /* no varargs macros */
153 static void
154 g_error (const gchar *format,
155          ...)
156 {
157   va_list args;
158   va_start (args, format);
159   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
160   va_end (args);
161 }
162 static void
163 g_message (const gchar *format,
164            ...)
165 {
166   va_list args;
167   va_start (args, format);
168   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
169   va_end (args);
170 }
171 static void
172 g_critical (const gchar *format,
173             ...)
174 {
175   va_list args;
176   va_start (args, format);
177   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
178   va_end (args);
179 }
180 static void
181 g_warning (const gchar *format,
182            ...)
183 {
184   va_list args;
185   va_start (args, format);
186   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
187   va_end (args);
188 }
189 static void
190 g_debug (const gchar *format,
191          ...)
192 {
193   va_list args;
194   va_start (args, format);
195   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
196   va_end (args);
197 }
198 #endif  /* !__GNUC__ */
199
200 typedef void    (*GPrintFunc)           (const gchar    *string);
201 void            g_print                 (const gchar    *format,
202                                          ...) G_GNUC_PRINTF (1, 2);
203 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
204 void            g_printerr              (const gchar    *format,
205                                          ...) G_GNUC_PRINTF (1, 2);
206 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
207
208
209 /* Provide macros for error handling. The "assert" macros will
210  *  exit on failure. The "return" macros will exit the current
211  *  function. Two different definitions are given for the macros
212  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
213  *  __PRETTY_FUNCTION__ capability.
214  */
215
216 #ifdef G_DISABLE_ASSERT
217
218 #define g_assert(expr)          G_STMT_START{ (void)0; }G_STMT_END
219 #define g_assert_not_reached()  G_STMT_START{ (void)0; }G_STMT_END
220
221 #else /* !G_DISABLE_ASSERT */
222
223 #ifdef __GNUC__
224
225 #define g_assert(expr)                  G_STMT_START{           \
226      if G_LIKELY(expr) { } else                                 \
227         g_log (G_LOG_DOMAIN,                                    \
228               G_LOG_LEVEL_ERROR,                                \
229               "file %s: line %d (%s): assertion failed: (%s)",  \
230               __FILE__,                                         \
231               __LINE__,                                         \
232               __PRETTY_FUNCTION__,                              \
233               #expr);                   }G_STMT_END
234
235 #define g_assert_not_reached()          G_STMT_START{           \
236      g_log (G_LOG_DOMAIN,                                       \
237             G_LOG_LEVEL_ERROR,                                  \
238             "file %s: line %d (%s): should not be reached",     \
239             __FILE__,                                           \
240             __LINE__,                                           \
241             __PRETTY_FUNCTION__);       }G_STMT_END
242
243 #else /* !__GNUC__ */
244
245 #define g_assert(expr)                  G_STMT_START{           \
246      if (expr) { } else                                         \
247        g_log (G_LOG_DOMAIN,                                     \
248               G_LOG_LEVEL_ERROR,                                \
249               "file %s: line %d: assertion failed: (%s)",       \
250               __FILE__,                                         \
251               __LINE__,                                         \
252               #expr);                   }G_STMT_END
253
254 #define g_assert_not_reached()          G_STMT_START{   \
255      g_log (G_LOG_DOMAIN,                               \
256             G_LOG_LEVEL_ERROR,                          \
257             "file %s: line %d: should not be reached",  \
258             __FILE__,                                   \
259             __LINE__);          }G_STMT_END
260
261 #endif /* __GNUC__ */
262
263 #endif /* !G_DISABLE_ASSERT */
264
265
266 #ifdef G_DISABLE_CHECKS
267
268 #define g_return_if_fail(expr)                  G_STMT_START{ (void)0; }G_STMT_END
269 #define g_return_val_if_fail(expr,val)          G_STMT_START{ (void)0; }G_STMT_END
270 #define g_return_if_reached()                   G_STMT_START{ return; }G_STMT_END
271 #define g_return_val_if_reached(val)            G_STMT_START{ return (val); }G_STMT_END
272
273 #else /* !G_DISABLE_CHECKS */
274
275 #ifdef __GNUC__
276
277 #define g_return_if_fail(expr)          G_STMT_START{                   \
278      if G_LIKELY(expr) { } else                                         \
279        {                                                                \
280          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
281                                    __PRETTY_FUNCTION__,                 \
282                                    #expr);                              \
283          return;                                                        \
284        };                               }G_STMT_END
285
286 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
287      if G_LIKELY(expr) { } else                                         \
288        {                                                                \
289          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
290                                    __PRETTY_FUNCTION__,                 \
291                                    #expr);                              \
292          return (val);                                                  \
293        };                               }G_STMT_END
294
295 #define g_return_if_reached()           G_STMT_START{                   \
296      g_log (G_LOG_DOMAIN,                                               \
297             G_LOG_LEVEL_CRITICAL,                                       \
298             "file %s: line %d (%s): should not be reached",             \
299             __FILE__,                                                   \
300             __LINE__,                                                   \
301             __PRETTY_FUNCTION__);                                       \
302      return;                            }G_STMT_END
303
304 #define g_return_val_if_reached(val)    G_STMT_START{                   \
305      g_log (G_LOG_DOMAIN,                                               \
306             G_LOG_LEVEL_CRITICAL,                                       \
307             "file %s: line %d (%s): should not be reached",             \
308             __FILE__,                                                   \
309             __LINE__,                                                   \
310             __PRETTY_FUNCTION__);                                       \
311      return (val);                      }G_STMT_END
312
313 #else /* !__GNUC__ */
314
315 #define g_return_if_fail(expr)          G_STMT_START{           \
316      if (expr) { } else                                         \
317        {                                                        \
318          g_log (G_LOG_DOMAIN,                                   \
319                 G_LOG_LEVEL_CRITICAL,                           \
320                 "file %s: line %d: assertion `%s' failed",      \
321                 __FILE__,                                       \
322                 __LINE__,                                       \
323                 #expr);                                         \
324          return;                                                \
325        };                               }G_STMT_END
326
327 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
328      if (expr) { } else                                         \
329        {                                                        \
330          g_log (G_LOG_DOMAIN,                                   \
331                 G_LOG_LEVEL_CRITICAL,                           \
332                 "file %s: line %d: assertion `%s' failed",      \
333                 __FILE__,                                       \
334                 __LINE__,                                       \
335                 #expr);                                         \
336          return (val);                                          \
337        };                               }G_STMT_END
338
339 #define g_return_if_reached()           G_STMT_START{           \
340      g_log (G_LOG_DOMAIN,                                       \
341             G_LOG_LEVEL_CRITICAL,                               \
342             "file %s: line %d: should not be reached",          \
343             __FILE__,                                           \
344             __LINE__);                                          \
345      return;                            }G_STMT_END
346
347 #define g_return_val_if_reached(val)    G_STMT_START{           \
348      g_log (G_LOG_DOMAIN,                                       \
349             G_LOG_LEVEL_CRITICAL,                               \
350             "file %s: line %d: should not be reached",          \
351             __FILE__,                                           \
352             __LINE__);                                          \
353      return (val);                      }G_STMT_END
354
355 #endif /* !__GNUC__ */
356
357 #endif /* !G_DISABLE_CHECKS */
358
359 G_END_DECLS
360
361 #endif /* __G_MESSAGES_H__ */
362