Imported Upstream version 2.72.3
[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.1 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #ifndef __G_MESSAGES_H__
26 #define __G_MESSAGES_H__
27
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
31
32 #include <stdarg.h>
33 #include <glib/gatomic.h>
34 #include <glib/gtypes.h>
35 #include <glib/gmacros.h>
36 #include <glib/gvariant.h>
37
38 G_BEGIN_DECLS
39
40 /* calculate a string size, guaranteed to fit format + args.
41  */
42 GLIB_AVAILABLE_IN_ALL
43 gsize   g_printf_string_upper_bound (const gchar* format,
44                                      va_list      args) G_GNUC_PRINTF(1, 0);
45
46 /* Log level shift offset for user defined
47  * log levels (0-7 are used by GLib).
48  */
49 #define G_LOG_LEVEL_USER_SHIFT  (8)
50
51 /* Glib log levels and flags.
52  */
53 typedef enum
54 {
55   /* log flags */
56   G_LOG_FLAG_RECURSION          = 1 << 0,
57   G_LOG_FLAG_FATAL              = 1 << 1,
58
59   /* GLib log levels */
60   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
61   G_LOG_LEVEL_CRITICAL          = 1 << 3,
62   G_LOG_LEVEL_WARNING           = 1 << 4,
63   G_LOG_LEVEL_MESSAGE           = 1 << 5,
64   G_LOG_LEVEL_INFO              = 1 << 6,
65   G_LOG_LEVEL_DEBUG             = 1 << 7,
66
67   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
68 } GLogLevelFlags;
69
70 /* GLib log levels that are considered fatal by default */
71 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
72
73 typedef void            (*GLogFunc)             (const gchar   *log_domain,
74                                                  GLogLevelFlags log_level,
75                                                  const gchar   *message,
76                                                  gpointer       user_data);
77
78 /* Logging mechanism
79  */
80 GLIB_AVAILABLE_IN_ALL
81 guint           g_log_set_handler       (const gchar    *log_domain,
82                                          GLogLevelFlags  log_levels,
83                                          GLogFunc        log_func,
84                                          gpointer        user_data);
85 GLIB_AVAILABLE_IN_2_46
86 guint           g_log_set_handler_full  (const gchar    *log_domain,
87                                          GLogLevelFlags  log_levels,
88                                          GLogFunc        log_func,
89                                          gpointer        user_data,
90                                          GDestroyNotify  destroy);
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 /* Structured logging mechanism. */
119
120 /**
121  * GLogWriterOutput:
122  * @G_LOG_WRITER_HANDLED: Log writer has handled the log entry.
123  * @G_LOG_WRITER_UNHANDLED: Log writer could not handle the log entry.
124  *
125  * Return values from #GLogWriterFuncs to indicate whether the given log entry
126  * was successfully handled by the writer, or whether there was an error in
127  * handling it (and hence a fallback writer should be used).
128  *
129  * If a #GLogWriterFunc ignores a log entry, it should return
130  * %G_LOG_WRITER_HANDLED.
131  *
132  * Since: 2.50
133  */
134 typedef enum
135 {
136   G_LOG_WRITER_HANDLED = 1,
137   G_LOG_WRITER_UNHANDLED = 0,
138 } GLogWriterOutput;
139
140 /**
141  * GLogField:
142  * @key: field name (UTF-8 string)
143  * @value: field value (arbitrary bytes)
144  * @length: length of @value, in bytes, or -1 if it is nul-terminated
145  *
146  * Structure representing a single field in a structured log entry. See
147  * g_log_structured() for details.
148  *
149  * Log fields may contain arbitrary values, including binary with embedded nul
150  * bytes. If the field contains a string, the string must be UTF-8 encoded and
151  * have a trailing nul byte. Otherwise, @length must be set to a non-negative
152  * value.
153  *
154  * Since: 2.50
155  */
156 typedef struct _GLogField GLogField;
157 struct _GLogField
158 {
159   const gchar *key;
160   gconstpointer value;
161   gssize length;
162 };
163
164 /**
165  * GLogWriterFunc:
166  * @log_level: log level of the message
167  * @fields: (array length=n_fields): fields forming the message
168  * @n_fields: number of @fields
169  * @user_data: user data passed to g_log_set_writer_func()
170  *
171  * Writer function for log entries. A log entry is a collection of one or more
172  * #GLogFields, using the standard [field names from journal
173  * specification](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html).
174  * See g_log_structured() for more information.
175  *
176  * Writer functions must ignore fields which they do not recognise, unless they
177  * can write arbitrary binary output, as field values may be arbitrary binary.
178  *
179  * @log_level is guaranteed to be included in @fields as the `PRIORITY` field,
180  * but is provided separately for convenience of deciding whether or where to
181  * output the log entry.
182  *
183  * Writer functions should return %G_LOG_WRITER_HANDLED if they handled the log
184  * message successfully or if they deliberately ignored it. If there was an
185  * error handling the message (for example, if the writer function is meant to
186  * send messages to a remote logging server and there is a network error), it
187  * should return %G_LOG_WRITER_UNHANDLED. This allows writer functions to be
188  * chained and fall back to simpler handlers in case of failure.
189  *
190  * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
191  *   %G_LOG_WRITER_UNHANDLED otherwise
192  *
193  * Since: 2.50
194  */
195 typedef GLogWriterOutput (*GLogWriterFunc)     (GLogLevelFlags   log_level,
196                                                 const GLogField *fields,
197                                                 gsize            n_fields,
198                                                 gpointer         user_data);
199
200 GLIB_AVAILABLE_IN_2_50
201 void             g_log_structured              (const gchar     *log_domain,
202                                                 GLogLevelFlags   log_level,
203                                                 ...);
204 GLIB_AVAILABLE_IN_2_50
205 void             g_log_structured_array        (GLogLevelFlags   log_level,
206                                                 const GLogField *fields,
207                                                 gsize            n_fields);
208
209 GLIB_AVAILABLE_IN_2_50
210 void             g_log_variant                 (const gchar     *log_domain,
211                                                 GLogLevelFlags   log_level,
212                                                 GVariant        *fields);
213
214 GLIB_AVAILABLE_IN_2_50
215 void             g_log_set_writer_func         (GLogWriterFunc   func,
216                                                 gpointer         user_data,
217                                                 GDestroyNotify   user_data_free);
218
219 GLIB_AVAILABLE_IN_2_50
220 gboolean         g_log_writer_supports_color   (gint             output_fd);
221 GLIB_AVAILABLE_IN_2_50
222 gboolean         g_log_writer_is_journald      (gint             output_fd);
223
224 GLIB_AVAILABLE_IN_2_50
225 gchar           *g_log_writer_format_fields    (GLogLevelFlags   log_level,
226                                                 const GLogField *fields,
227                                                 gsize            n_fields,
228                                                 gboolean         use_color);
229
230 GLIB_AVAILABLE_IN_2_50
231 GLogWriterOutput g_log_writer_journald         (GLogLevelFlags   log_level,
232                                                 const GLogField *fields,
233                                                 gsize            n_fields,
234                                                 gpointer         user_data);
235 GLIB_AVAILABLE_IN_2_50
236 GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags   log_level,
237                                                 const GLogField *fields,
238                                                 gsize            n_fields,
239                                                 gpointer         user_data);
240 GLIB_AVAILABLE_IN_2_50
241 GLogWriterOutput g_log_writer_default          (GLogLevelFlags   log_level,
242                                                 const GLogField *fields,
243                                                 gsize            n_fields,
244                                                 gpointer         user_data);
245
246 GLIB_AVAILABLE_IN_2_68
247 void            g_log_writer_default_set_use_stderr (gboolean use_stderr);
248 GLIB_AVAILABLE_IN_2_68
249 gboolean        g_log_writer_default_would_drop (GLogLevelFlags  log_level,
250                                                  const char     *log_domain);
251
252 /* G_MESSAGES_DEBUG enablement */
253 GLIB_AVAILABLE_IN_2_72
254 gboolean         g_log_get_debug_enabled       (void);
255 GLIB_AVAILABLE_IN_2_72
256 void             g_log_set_debug_enabled       (gboolean         enabled);
257
258 /**
259  * G_DEBUG_HERE:
260  *
261  * A convenience form of g_log_structured(), recommended to be added to
262  * functions when debugging. It prints the current monotonic time and the code
263  * location using %G_STRLOC.
264  *
265  * Since: 2.50
266  */
267 #define G_DEBUG_HERE()                                          \
268   g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,            \
269                     "CODE_FILE", __FILE__,                      \
270                     "CODE_LINE", G_STRINGIFY (__LINE__),        \
271                     "CODE_FUNC", G_STRFUNC,                      \
272                     "MESSAGE", "%" G_GINT64_FORMAT ": %s",      \
273                     g_get_monotonic_time (), G_STRLOC)
274
275 /* internal */
276 void    _g_log_fallback_handler (const gchar   *log_domain,
277                                                  GLogLevelFlags log_level,
278                                                  const gchar   *message,
279                                                  gpointer       unused_data);
280
281 /* Internal functions, used to implement the following macros */
282 GLIB_AVAILABLE_IN_ALL
283 void g_return_if_fail_warning (const char *log_domain,
284                                const char *pretty_function,
285                                const char *expression) G_ANALYZER_NORETURN;
286 GLIB_AVAILABLE_IN_ALL
287 void g_warn_message           (const char     *domain,
288                                const char     *file,
289                                int             line,
290                                const char     *func,
291                                const char     *warnexpr) G_ANALYZER_NORETURN;
292 GLIB_DEPRECATED
293 G_NORETURN
294 void g_assert_warning         (const char *log_domain,
295                                const char *file,
296                                const int   line,
297                                const char *pretty_function,
298                                const char *expression);
299
300 GLIB_AVAILABLE_IN_2_56
301 void g_log_structured_standard (const gchar    *log_domain,
302                                 GLogLevelFlags  log_level,
303                                 const gchar    *file,
304                                 const gchar    *line,
305                                 const gchar    *func,
306                                 const gchar    *message_format,
307                                 ...) G_GNUC_PRINTF (6, 7);
308
309 #ifndef G_LOG_DOMAIN
310 #define G_LOG_DOMAIN    ((gchar*) 0)
311 #endif  /* G_LOG_DOMAIN */
312
313 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
314 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
315 #define g_error(...)  G_STMT_START {                                            \
316                         g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
317                                                    __FILE__, G_STRINGIFY (__LINE__), \
318                                                    G_STRFUNC, __VA_ARGS__); \
319                         for (;;) ;                                              \
320                       } G_STMT_END
321 #define g_message(...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
322                                                    __FILE__, G_STRINGIFY (__LINE__), \
323                                                    G_STRFUNC, __VA_ARGS__)
324 #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
325                                                    __FILE__, G_STRINGIFY (__LINE__), \
326                                                    G_STRFUNC, __VA_ARGS__)
327 #define g_warning(...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
328                                                    __FILE__, G_STRINGIFY (__LINE__), \
329                                                    G_STRFUNC, __VA_ARGS__)
330 #define g_info(...)     g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
331                                                    __FILE__, G_STRINGIFY (__LINE__), \
332                                                    G_STRFUNC, __VA_ARGS__)
333 #define g_debug(...)    g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
334                                                    __FILE__, G_STRINGIFY (__LINE__), \
335                                                    G_STRFUNC, __VA_ARGS__)
336 #else
337 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
338  * Put space before ending semicolon to avoid C++ build warnings.
339  */
340 #define g_error(...)  G_STMT_START {                 \
341                         g_log (G_LOG_DOMAIN,         \
342                                G_LOG_LEVEL_ERROR,    \
343                                __VA_ARGS__);         \
344                         for (;;) ;                   \
345                       } G_STMT_END
346 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
347                                G_LOG_LEVEL_MESSAGE,  \
348                                __VA_ARGS__)
349 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
350                                G_LOG_LEVEL_CRITICAL, \
351                                __VA_ARGS__)
352 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
353                                G_LOG_LEVEL_WARNING,  \
354                                __VA_ARGS__)
355 #define g_info(...)     g_log (G_LOG_DOMAIN,         \
356                                G_LOG_LEVEL_INFO,     \
357                                __VA_ARGS__)
358 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
359                                G_LOG_LEVEL_DEBUG,    \
360                                __VA_ARGS__)
361 #endif
362 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
363 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
364 #define g_error(format...)   G_STMT_START {                                          \
365                                g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
366                                                           __FILE__, G_STRINGIFY (__LINE__), \
367                                                           G_STRFUNC, format); \
368                                for (;;) ;                                            \
369                              } G_STMT_END
370 #define g_message(format...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
371                                                          __FILE__, G_STRINGIFY (__LINE__), \
372                                                          G_STRFUNC, format)
373 #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
374                                                          __FILE__, G_STRINGIFY (__LINE__), \
375                                                          G_STRFUNC, format)
376 #define g_warning(format...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
377                                                          __FILE__, G_STRINGIFY (__LINE__), \
378                                                          G_STRFUNC, format)
379 #define g_info(format...)     g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
380                                                          __FILE__, G_STRINGIFY (__LINE__), \
381                                                          G_STRFUNC, format)
382 #define g_debug(format...)    g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
383                                                          __FILE__, G_STRINGIFY (__LINE__), \
384                                                          G_STRFUNC, format)
385 #else
386 #define g_error(format...)    G_STMT_START {                 \
387                                 g_log (G_LOG_DOMAIN,         \
388                                        G_LOG_LEVEL_ERROR,    \
389                                        format);              \
390                                 for (;;) ;                   \
391                               } G_STMT_END
392
393 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
394                                        G_LOG_LEVEL_MESSAGE,  \
395                                        format)
396 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
397                                        G_LOG_LEVEL_CRITICAL, \
398                                        format)
399 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
400                                        G_LOG_LEVEL_WARNING,  \
401                                        format)
402 #define g_info(format...)       g_log (G_LOG_DOMAIN,         \
403                                        G_LOG_LEVEL_INFO,     \
404                                        format)
405 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
406                                        G_LOG_LEVEL_DEBUG,    \
407                                        format)
408 #endif
409 #else   /* no varargs macros */
410 static G_NORETURN void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
411 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
412
413 static inline void
414 g_error (const gchar *format,
415          ...)
416 {
417   va_list args;
418   va_start (args, format);
419   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
420   va_end (args);
421
422   for(;;) ;
423 }
424 static inline void
425 g_message (const gchar *format,
426            ...)
427 {
428   va_list args;
429   va_start (args, format);
430   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
431   va_end (args);
432 }
433 static inline void
434 g_critical (const gchar *format,
435             ...)
436 {
437   va_list args;
438   va_start (args, format);
439   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
440   va_end (args);
441 }
442 static inline void
443 g_warning (const gchar *format,
444            ...)
445 {
446   va_list args;
447   va_start (args, format);
448   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
449   va_end (args);
450 }
451 static inline void
452 g_info (const gchar *format,
453         ...)
454 {
455   va_list args;
456   va_start (args, format);
457   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
458   va_end (args);
459 }
460 static inline void
461 g_debug (const gchar *format,
462          ...)
463 {
464   va_list args;
465   va_start (args, format);
466   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
467   va_end (args);
468 }
469 #endif  /* !__GNUC__ */
470
471 /**
472  * g_warning_once:
473  * @...: format string, followed by parameters to insert
474  *     into the format string (as with printf())
475  *
476  * Logs a warning only once.
477  *
478  * g_warning_once() calls g_warning() with the passed message the first time
479  * the statement is executed; subsequent times it is a no-op.
480  *
481  * Note! On platforms where the compiler doesn't support variadic macros, the
482  * warning is printed each time instead of only once.
483  *
484  * Since: 2.64
485  */
486 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
487 #define g_warning_once(...) \
488   G_STMT_START { \
489     static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0;  /* (atomic) */ \
490     if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
491                                            0, 1)) \
492       g_warning (__VA_ARGS__); \
493   } G_STMT_END \
494   GLIB_AVAILABLE_MACRO_IN_2_64
495 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
496 #define g_warning_once(format...) \
497   G_STMT_START { \
498     static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0;  /* (atomic) */ \
499     if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
500                                            0, 1)) \
501       g_warning (format); \
502   } G_STMT_END \
503   GLIB_AVAILABLE_MACRO_IN_2_64
504 #else
505 #define g_warning_once g_warning
506 #endif
507
508 /**
509  * GPrintFunc:
510  * @string: the message to output
511  *
512  * Specifies the type of the print handler functions.
513  * These are called with the complete formatted string to output.
514  */
515 typedef void    (*GPrintFunc)           (const gchar    *string);
516 GLIB_AVAILABLE_IN_ALL
517 void            g_print                 (const gchar    *format,
518                                          ...) G_GNUC_PRINTF (1, 2);
519 GLIB_AVAILABLE_IN_ALL
520 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
521 GLIB_AVAILABLE_IN_ALL
522 void            g_printerr              (const gchar    *format,
523                                          ...) G_GNUC_PRINTF (1, 2);
524 GLIB_AVAILABLE_IN_ALL
525 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
526
527 /**
528  * g_warn_if_reached:
529  *
530  * Logs a warning.
531  *
532  * Since: 2.16
533  */
534 #define g_warn_if_reached() \
535   do { \
536     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
537   } while (0)
538
539 /**
540  * g_warn_if_fail:
541  * @expr: the expression to check
542  *
543  * Logs a warning if the expression is not true.
544  *
545  * Since: 2.16
546  */
547 #define g_warn_if_fail(expr) \
548   do { \
549     if G_LIKELY (expr) ; \
550     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
551   } while (0)
552
553 #ifdef G_DISABLE_CHECKS
554
555 /**
556  * g_return_if_fail:
557  * @expr: the expression to check
558  *
559  * Verifies that the expression @expr, usually representing a precondition,
560  * evaluates to %TRUE. If the function returns a value, use
561  * g_return_val_if_fail() instead.
562  *
563  * If @expr evaluates to %FALSE, the current function should be considered to
564  * have undefined behaviour (a programmer error). The only correct solution
565  * to such an error is to change the module that is calling the current
566  * function, so that it avoids this incorrect call.
567  *
568  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
569  * the result is usually that a critical message is logged and the current
570  * function returns.
571  *
572  * If `G_DISABLE_CHECKS` is defined then the check is not performed.  You
573  * should therefore not depend on any side effects of @expr.
574  *
575  * To debug failure of a g_return_if_fail() check, run the code under a debugger
576  * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
577  * environment (see [Running GLib Applications](glib-running.html)):
578  *
579  * |[
580  *   G_DEBUG=fatal-warnings gdb ./my-program
581  * ]|
582  *
583  * Any unrelated failures can be skipped over in
584  * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
585  */
586 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
587
588 /**
589  * g_return_val_if_fail:
590  * @expr: the expression to check
591  * @val: the value to return from the current function
592  *       if the expression is not true
593  *
594  * Verifies that the expression @expr, usually representing a precondition,
595  * evaluates to %TRUE. If the function does not return a value, use
596  * g_return_if_fail() instead.
597  *
598  * If @expr evaluates to %FALSE, the current function should be considered to
599  * have undefined behaviour (a programmer error). The only correct solution
600  * to such an error is to change the module that is calling the current
601  * function, so that it avoids this incorrect call.
602  *
603  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
604  * the result is usually that a critical message is logged and @val is
605  * returned from the current function.
606  *
607  * If `G_DISABLE_CHECKS` is defined then the check is not performed.  You
608  * should therefore not depend on any side effects of @expr.
609  *
610  * See g_return_if_fail() for guidance on how to debug failure of this check.
611  */
612 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
613
614 /**
615  * g_return_if_reached:
616  *
617  * Logs a critical message and returns from the current function.
618  * This can only be used in functions which do not return a value.
619  *
620  * See g_return_if_fail() for guidance on how to debug failure of this check.
621  */
622 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
623
624 /**
625  * g_return_val_if_reached:
626  * @val: the value to return from the current function
627  *
628  * Logs a critical message and returns @val.
629  *
630  * See g_return_if_fail() for guidance on how to debug failure of this check.
631  */
632 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
633
634 #else /* !G_DISABLE_CHECKS */
635
636 #define g_return_if_fail(expr) \
637   G_STMT_START { \
638     if (G_LIKELY (expr)) \
639       { } \
640     else \
641       { \
642         g_return_if_fail_warning (G_LOG_DOMAIN, \
643                                   G_STRFUNC, \
644                                   #expr); \
645         return; \
646       } \
647   } G_STMT_END
648
649 #define g_return_val_if_fail(expr, val) \
650   G_STMT_START { \
651     if (G_LIKELY (expr)) \
652       { } \
653     else \
654       { \
655         g_return_if_fail_warning (G_LOG_DOMAIN, \
656                                   G_STRFUNC, \
657                                   #expr); \
658         return (val); \
659       } \
660   } G_STMT_END
661
662 #define g_return_if_reached() \
663   G_STMT_START { \
664     g_log (G_LOG_DOMAIN, \
665            G_LOG_LEVEL_CRITICAL, \
666            "file %s: line %d (%s): should not be reached", \
667            __FILE__, \
668            __LINE__, \
669            G_STRFUNC); \
670     return; \
671   } G_STMT_END
672
673 #define g_return_val_if_reached(val) \
674   G_STMT_START { \
675     g_log (G_LOG_DOMAIN, \
676            G_LOG_LEVEL_CRITICAL, \
677            "file %s: line %d (%s): should not be reached", \
678            __FILE__, \
679            __LINE__, \
680            G_STRFUNC); \
681     return (val); \
682   } G_STMT_END
683
684 #endif /* !G_DISABLE_CHECKS */
685
686 G_END_DECLS
687
688 #endif /* __G_MESSAGES_H__ */