Set up test environment properly
[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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
32 #endif
33
34 #include <stdarg.h>
35 #include <glib/gtypes.h>
36 #include <glib/gmacros.h>
37
38 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
39  */
40 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
41 #pragma GCC system_header
42 #endif
43
44 G_BEGIN_DECLS
45
46 /* calculate a string size, guaranteed to fit format + args.
47  */
48 GLIB_AVAILABLE_IN_ALL
49 gsize   g_printf_string_upper_bound (const gchar* format,
50                                      va_list      args) G_GNUC_PRINTF(1, 0);
51
52 /* Log level shift offset for user defined
53  * log levels (0-7 are used by GLib).
54  */
55 #define G_LOG_LEVEL_USER_SHIFT  (8)
56
57 /* Glib log levels and flags.
58  */
59 typedef enum
60 {
61   /* log flags */
62   G_LOG_FLAG_RECURSION          = 1 << 0,
63   G_LOG_FLAG_FATAL              = 1 << 1,
64
65   /* GLib log levels */
66   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
67   G_LOG_LEVEL_CRITICAL          = 1 << 3,
68   G_LOG_LEVEL_WARNING           = 1 << 4,
69   G_LOG_LEVEL_MESSAGE           = 1 << 5,
70   G_LOG_LEVEL_INFO              = 1 << 6,
71   G_LOG_LEVEL_DEBUG             = 1 << 7,
72
73   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
74 } GLogLevelFlags;
75
76 /* GLib log levels that are considered fatal by default */
77 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
78
79 typedef void            (*GLogFunc)             (const gchar   *log_domain,
80                                                  GLogLevelFlags log_level,
81                                                  const gchar   *message,
82                                                  gpointer       user_data);
83
84 /* Logging mechanism
85  */
86 GLIB_AVAILABLE_IN_ALL
87 guint           g_log_set_handler       (const gchar    *log_domain,
88                                          GLogLevelFlags  log_levels,
89                                          GLogFunc        log_func,
90                                          gpointer        user_data);
91 GLIB_AVAILABLE_IN_ALL
92 void            g_log_remove_handler    (const gchar    *log_domain,
93                                          guint           handler_id);
94 GLIB_AVAILABLE_IN_ALL
95 void            g_log_default_handler   (const gchar    *log_domain,
96                                          GLogLevelFlags  log_level,
97                                          const gchar    *message,
98                                          gpointer        unused_data);
99 GLIB_AVAILABLE_IN_ALL
100 GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
101                                            gpointer      user_data);
102 GLIB_AVAILABLE_IN_ALL
103 void            g_log                   (const gchar    *log_domain,
104                                          GLogLevelFlags  log_level,
105                                          const gchar    *format,
106                                          ...) G_GNUC_PRINTF (3, 4);
107 GLIB_AVAILABLE_IN_ALL
108 void            g_logv                  (const gchar    *log_domain,
109                                          GLogLevelFlags  log_level,
110                                          const gchar    *format,
111                                          va_list         args) G_GNUC_PRINTF(3, 0);
112 GLIB_AVAILABLE_IN_ALL
113 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
114                                          GLogLevelFlags  fatal_mask);
115 GLIB_AVAILABLE_IN_ALL
116 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
117
118 /* internal */
119 void    _g_log_fallback_handler (const gchar   *log_domain,
120                                                  GLogLevelFlags log_level,
121                                                  const gchar   *message,
122                                                  gpointer       unused_data);
123
124 /* Internal functions, used to implement the following macros */
125 GLIB_AVAILABLE_IN_ALL
126 void g_return_if_fail_warning (const char *log_domain,
127                                const char *pretty_function,
128                                const char *expression) G_ANALYZER_NORETURN;
129 GLIB_AVAILABLE_IN_ALL
130 void g_warn_message           (const char     *domain,
131                                const char     *file,
132                                int             line,
133                                const char     *func,
134                                const char     *warnexpr) G_ANALYZER_NORETURN;
135 GLIB_DEPRECATED
136 void g_assert_warning         (const char *log_domain,
137                                const char *file,
138                                const int   line,
139                                const char *pretty_function,
140                                const char *expression) G_GNUC_NORETURN;
141
142
143 #ifndef G_LOG_DOMAIN
144 #define G_LOG_DOMAIN    ((gchar*) 0)
145 #endif  /* G_LOG_DOMAIN */
146
147 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
148 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
149  * Put space before ending semicolon to avoid C++ build warnings.
150  */
151 #define g_error(...)  G_STMT_START {                 \
152                         g_log (G_LOG_DOMAIN,         \
153                                G_LOG_LEVEL_ERROR,    \
154                                __VA_ARGS__);         \
155                         for (;;) ;                   \
156                       } G_STMT_END
157                         
158 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
159                                G_LOG_LEVEL_MESSAGE,  \
160                                __VA_ARGS__)
161 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
162                                G_LOG_LEVEL_CRITICAL, \
163                                __VA_ARGS__)
164 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
165                                G_LOG_LEVEL_WARNING,  \
166                                __VA_ARGS__)
167 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
168                                G_LOG_LEVEL_DEBUG,    \
169                                __VA_ARGS__)
170 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
171 #define g_error(format...)    G_STMT_START {                 \
172                                 g_log (G_LOG_DOMAIN,         \
173                                        G_LOG_LEVEL_ERROR,    \
174                                        format);              \
175                                 for (;;) ;                   \
176                               } G_STMT_END
177                               
178 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
179                                        G_LOG_LEVEL_MESSAGE,  \
180                                        format)
181 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
182                                        G_LOG_LEVEL_CRITICAL, \
183                                        format)
184 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
185                                        G_LOG_LEVEL_WARNING,  \
186                                        format)
187 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
188                                        G_LOG_LEVEL_DEBUG,    \
189                                        format)
190 #else   /* no varargs macros */
191 static void
192 g_error (const gchar *format,
193          ...) G_ANALYZER_NORETURN
194 {
195   va_list args;
196   va_start (args, format);
197   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
198   va_end (args);
199
200   for(;;) ;
201 }
202 static void
203 g_message (const gchar *format,
204            ...)
205 {
206   va_list args;
207   va_start (args, format);
208   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
209   va_end (args);
210 }
211 static void
212 g_critical (const gchar *format,
213             ...) G_ANALYZER_NORETURN
214 {
215   va_list args;
216   va_start (args, format);
217   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
218   va_end (args);
219 }
220 static void
221 g_warning (const gchar *format,
222            ...)
223 {
224   va_list args;
225   va_start (args, format);
226   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
227   va_end (args);
228 }
229 static void
230 g_debug (const gchar *format,
231          ...)
232 {
233   va_list args;
234   va_start (args, format);
235   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
236   va_end (args);
237 }
238 #endif  /* !__GNUC__ */
239
240 /**
241  * GPrintFunc:
242  * @string: the message to output
243  *
244  * Specifies the type of the print handler functions.
245  * These are called with the complete formatted string to output.
246  */
247 typedef void    (*GPrintFunc)           (const gchar    *string);
248 GLIB_AVAILABLE_IN_ALL
249 void            g_print                 (const gchar    *format,
250                                          ...) G_GNUC_PRINTF (1, 2);
251 GLIB_AVAILABLE_IN_ALL
252 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
253 GLIB_AVAILABLE_IN_ALL
254 void            g_printerr              (const gchar    *format,
255                                          ...) G_GNUC_PRINTF (1, 2);
256 GLIB_AVAILABLE_IN_ALL
257 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
258
259 /**
260  * g_warn_if_reached:
261  *
262  * Logs a critical warning.
263  *
264  * Since: 2.16
265  */
266 #define g_warn_if_reached() \
267   do { \
268     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
269   } while (0)
270
271 /**
272  * g_warn_if_fail:
273  * @expr: the expression to check
274  *
275  * Logs a warning if the expression is not true.
276  *
277  * Since: 2.16
278  */
279 #define g_warn_if_fail(expr) \
280   do { \
281     if G_LIKELY (expr) ; \
282     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
283   } while (0)
284
285 #ifdef G_DISABLE_CHECKS
286
287 /**
288  * g_return_if_fail:
289  * @expr: the expression to check
290  *
291  * Verifies that the expression evaluates to %TRUE.  If the expression
292  * evaluates to %FALSE, a critical message is logged and the current
293  * function returns.  This can only be used in functions which do not
294  * return a value.
295  *
296  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
297  * should therefore not depend on any side effects of @expr.
298  */
299 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
300
301 /**
302  * g_return_val_if_fail:
303  * @expr: the expression to check
304  * @val: the value to return from the current function
305  *       if the expression is not true
306  *
307  * Verifies that the expression evaluates to %TRUE.  If the expression
308  * evaluates to %FALSE, a critical message is logged and @val is
309  * returned from the current function.
310  *
311  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
312  * should therefore not depend on any side effects of @expr.
313  */
314 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
315
316 /**
317  * g_return_if_reached:
318  *
319  * Logs a critical message and returns from the current function.
320  * This can only be used in functions which do not return a value.
321  */
322 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
323
324 /**
325  * g_return_val_if_reached:
326  * @val: the value to return from the current function
327  *
328  * Logs a critical message and returns @val.
329  */
330 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
331
332 #else /* !G_DISABLE_CHECKS */
333
334 #ifdef __GNUC__
335
336 #define g_return_if_fail(expr)          G_STMT_START{                   \
337      if G_LIKELY(expr) { } else                                         \
338        {                                                                \
339          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
340                                    __PRETTY_FUNCTION__,                 \
341                                    #expr);                              \
342          return;                                                        \
343        };                               }G_STMT_END
344
345 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
346      if G_LIKELY(expr) { } else                                         \
347        {                                                                \
348          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
349                                    __PRETTY_FUNCTION__,                 \
350                                    #expr);                              \
351          return (val);                                                  \
352        };                               }G_STMT_END
353
354 #define g_return_if_reached()           G_STMT_START{                   \
355      g_log (G_LOG_DOMAIN,                                               \
356             G_LOG_LEVEL_CRITICAL,                                       \
357             "file %s: line %d (%s): should not be reached",             \
358             __FILE__,                                                   \
359             __LINE__,                                                   \
360             __PRETTY_FUNCTION__);                                       \
361      return;                            }G_STMT_END
362
363 #define g_return_val_if_reached(val)    G_STMT_START{                   \
364      g_log (G_LOG_DOMAIN,                                               \
365             G_LOG_LEVEL_CRITICAL,                                       \
366             "file %s: line %d (%s): should not be reached",             \
367             __FILE__,                                                   \
368             __LINE__,                                                   \
369             __PRETTY_FUNCTION__);                                       \
370      return (val);                      }G_STMT_END
371
372 #else /* !__GNUC__ */
373
374 #define g_return_if_fail(expr)          G_STMT_START{           \
375      if (expr) { } else                                         \
376        {                                                        \
377          g_log (G_LOG_DOMAIN,                                   \
378                 G_LOG_LEVEL_CRITICAL,                           \
379                 "file %s: line %d: assertion '%s' failed",      \
380                 __FILE__,                                       \
381                 __LINE__,                                       \
382                 #expr);                                         \
383          return;                                                \
384        };                               }G_STMT_END
385
386 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
387      if (expr) { } else                                         \
388        {                                                        \
389          g_log (G_LOG_DOMAIN,                                   \
390                 G_LOG_LEVEL_CRITICAL,                           \
391                 "file %s: line %d: assertion '%s' failed",      \
392                 __FILE__,                                       \
393                 __LINE__,                                       \
394                 #expr);                                         \
395          return (val);                                          \
396        };                               }G_STMT_END
397
398 #define g_return_if_reached()           G_STMT_START{           \
399      g_log (G_LOG_DOMAIN,                                       \
400             G_LOG_LEVEL_CRITICAL,                               \
401             "file %s: line %d: should not be reached",          \
402             __FILE__,                                           \
403             __LINE__);                                          \
404      return;                            }G_STMT_END
405
406 #define g_return_val_if_reached(val)    G_STMT_START{           \
407      g_log (G_LOG_DOMAIN,                                       \
408             G_LOG_LEVEL_CRITICAL,                               \
409             "file %s: line %d: should not be reached",          \
410             __FILE__,                                           \
411             __LINE__);                                          \
412      return (val);                      }G_STMT_END
413
414 #endif /* !__GNUC__ */
415
416 #endif /* !G_DISABLE_CHECKS */
417
418 G_END_DECLS
419
420 #endif /* __G_MESSAGES_H__ */