Imported Upstream version 2.64.5
[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  * Since: 2.50
193  */
194 typedef GLogWriterOutput (*GLogWriterFunc)     (GLogLevelFlags   log_level,
195                                                 const GLogField *fields,
196                                                 gsize            n_fields,
197                                                 gpointer         user_data);
198
199 GLIB_AVAILABLE_IN_2_50
200 void             g_log_structured              (const gchar     *log_domain,
201                                                 GLogLevelFlags   log_level,
202                                                 ...);
203 GLIB_AVAILABLE_IN_2_50
204 void             g_log_structured_array        (GLogLevelFlags   log_level,
205                                                 const GLogField *fields,
206                                                 gsize            n_fields);
207
208 GLIB_AVAILABLE_IN_2_50
209 void             g_log_variant                 (const gchar     *log_domain,
210                                                 GLogLevelFlags   log_level,
211                                                 GVariant        *fields);
212
213 GLIB_AVAILABLE_IN_2_50
214 void             g_log_set_writer_func         (GLogWriterFunc   func,
215                                                 gpointer         user_data,
216                                                 GDestroyNotify   user_data_free);
217
218 GLIB_AVAILABLE_IN_2_50
219 gboolean         g_log_writer_supports_color   (gint             output_fd);
220 GLIB_AVAILABLE_IN_2_50
221 gboolean         g_log_writer_is_journald      (gint             output_fd);
222
223 GLIB_AVAILABLE_IN_2_50
224 gchar           *g_log_writer_format_fields    (GLogLevelFlags   log_level,
225                                                 const GLogField *fields,
226                                                 gsize            n_fields,
227                                                 gboolean         use_color);
228
229 GLIB_AVAILABLE_IN_2_50
230 GLogWriterOutput g_log_writer_journald         (GLogLevelFlags   log_level,
231                                                 const GLogField *fields,
232                                                 gsize            n_fields,
233                                                 gpointer         user_data);
234 GLIB_AVAILABLE_IN_2_50
235 GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags   log_level,
236                                                 const GLogField *fields,
237                                                 gsize            n_fields,
238                                                 gpointer         user_data);
239 GLIB_AVAILABLE_IN_2_50
240 GLogWriterOutput g_log_writer_default          (GLogLevelFlags   log_level,
241                                                 const GLogField *fields,
242                                                 gsize            n_fields,
243                                                 gpointer         user_data);
244
245 /**
246  * G_DEBUG_HERE:
247  *
248  * A convenience form of g_log_structured(), recommended to be added to
249  * functions when debugging. It prints the current monotonic time and the code
250  * location using %G_STRLOC.
251  *
252  * Since: 2.50
253  */
254 #define G_DEBUG_HERE()                                          \
255   g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,            \
256                     "CODE_FILE", __FILE__,                      \
257                     "CODE_LINE", G_STRINGIFY (__LINE__),        \
258                     "CODE_FUNC", G_STRFUNC,                      \
259                     "MESSAGE", "%" G_GINT64_FORMAT ": %s",      \
260                     g_get_monotonic_time (), G_STRLOC)
261
262 /* internal */
263 void    _g_log_fallback_handler (const gchar   *log_domain,
264                                                  GLogLevelFlags log_level,
265                                                  const gchar   *message,
266                                                  gpointer       unused_data);
267
268 /* Internal functions, used to implement the following macros */
269 GLIB_AVAILABLE_IN_ALL
270 void g_return_if_fail_warning (const char *log_domain,
271                                const char *pretty_function,
272                                const char *expression) G_ANALYZER_NORETURN;
273 GLIB_AVAILABLE_IN_ALL
274 void g_warn_message           (const char     *domain,
275                                const char     *file,
276                                int             line,
277                                const char     *func,
278                                const char     *warnexpr) G_ANALYZER_NORETURN;
279 GLIB_DEPRECATED
280 void g_assert_warning         (const char *log_domain,
281                                const char *file,
282                                const int   line,
283                                const char *pretty_function,
284                                const char *expression) G_GNUC_NORETURN;
285
286 GLIB_AVAILABLE_IN_2_56
287 void g_log_structured_standard (const gchar    *log_domain,
288                                 GLogLevelFlags  log_level,
289                                 const gchar    *file,
290                                 const gchar    *line,
291                                 const gchar    *func,
292                                 const gchar    *message_format,
293                                 ...) G_GNUC_PRINTF (6, 7);
294
295 #ifndef G_LOG_DOMAIN
296 #define G_LOG_DOMAIN    ((gchar*) 0)
297 #endif  /* G_LOG_DOMAIN */
298
299 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
300 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
301 #define g_error(...)  G_STMT_START {                                            \
302                         g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
303                                                    __FILE__, G_STRINGIFY (__LINE__), \
304                                                    G_STRFUNC, __VA_ARGS__); \
305                         for (;;) ;                                              \
306                       } G_STMT_END
307 #define g_message(...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
308                                                    __FILE__, G_STRINGIFY (__LINE__), \
309                                                    G_STRFUNC, __VA_ARGS__)
310 #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
311                                                    __FILE__, G_STRINGIFY (__LINE__), \
312                                                    G_STRFUNC, __VA_ARGS__)
313 #define g_warning(...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
314                                                    __FILE__, G_STRINGIFY (__LINE__), \
315                                                    G_STRFUNC, __VA_ARGS__)
316 #define g_info(...)     g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
317                                                    __FILE__, G_STRINGIFY (__LINE__), \
318                                                    G_STRFUNC, __VA_ARGS__)
319 #define g_debug(...)    g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
320                                                    __FILE__, G_STRINGIFY (__LINE__), \
321                                                    G_STRFUNC, __VA_ARGS__)
322 #else
323 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
324  * Put space before ending semicolon to avoid C++ build warnings.
325  */
326 #define g_error(...)  G_STMT_START {                 \
327                         g_log (G_LOG_DOMAIN,         \
328                                G_LOG_LEVEL_ERROR,    \
329                                __VA_ARGS__);         \
330                         for (;;) ;                   \
331                       } G_STMT_END
332 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
333                                G_LOG_LEVEL_MESSAGE,  \
334                                __VA_ARGS__)
335 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
336                                G_LOG_LEVEL_CRITICAL, \
337                                __VA_ARGS__)
338 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
339                                G_LOG_LEVEL_WARNING,  \
340                                __VA_ARGS__)
341 #define g_info(...)     g_log (G_LOG_DOMAIN,         \
342                                G_LOG_LEVEL_INFO,     \
343                                __VA_ARGS__)
344 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
345                                G_LOG_LEVEL_DEBUG,    \
346                                __VA_ARGS__)
347 #endif
348 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
349 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
350 #define g_error(format...)   G_STMT_START {                                          \
351                                g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
352                                                           __FILE__, G_STRINGIFY (__LINE__), \
353                                                           G_STRFUNC, format); \
354                                for (;;) ;                                            \
355                              } G_STMT_END
356 #define g_message(format...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
357                                                          __FILE__, G_STRINGIFY (__LINE__), \
358                                                          G_STRFUNC, format)
359 #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
360                                                          __FILE__, G_STRINGIFY (__LINE__), \
361                                                          G_STRFUNC, format)
362 #define g_warning(format...)  g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
363                                                          __FILE__, G_STRINGIFY (__LINE__), \
364                                                          G_STRFUNC, format)
365 #define g_info(format...)     g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
366                                                          __FILE__, G_STRINGIFY (__LINE__), \
367                                                          G_STRFUNC, format)
368 #define g_debug(format...)    g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
369                                                          __FILE__, G_STRINGIFY (__LINE__), \
370                                                          G_STRFUNC, format)
371 #else
372 #define g_error(format...)    G_STMT_START {                 \
373                                 g_log (G_LOG_DOMAIN,         \
374                                        G_LOG_LEVEL_ERROR,    \
375                                        format);              \
376                                 for (;;) ;                   \
377                               } G_STMT_END
378
379 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
380                                        G_LOG_LEVEL_MESSAGE,  \
381                                        format)
382 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
383                                        G_LOG_LEVEL_CRITICAL, \
384                                        format)
385 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
386                                        G_LOG_LEVEL_WARNING,  \
387                                        format)
388 #define g_info(format...)       g_log (G_LOG_DOMAIN,         \
389                                        G_LOG_LEVEL_INFO,     \
390                                        format)
391 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
392                                        G_LOG_LEVEL_DEBUG,    \
393                                        format)
394 #endif
395 #else   /* no varargs macros */
396 static void g_error (const gchar *format, ...) G_GNUC_NORETURN G_ANALYZER_NORETURN;
397 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
398
399 static inline void
400 g_error (const gchar *format,
401          ...)
402 {
403   va_list args;
404   va_start (args, format);
405   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
406   va_end (args);
407
408   for(;;) ;
409 }
410 static inline void
411 g_message (const gchar *format,
412            ...)
413 {
414   va_list args;
415   va_start (args, format);
416   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
417   va_end (args);
418 }
419 static inline void
420 g_critical (const gchar *format,
421             ...)
422 {
423   va_list args;
424   va_start (args, format);
425   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
426   va_end (args);
427 }
428 static inline void
429 g_warning (const gchar *format,
430            ...)
431 {
432   va_list args;
433   va_start (args, format);
434   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
435   va_end (args);
436 }
437 static inline void
438 g_info (const gchar *format,
439         ...)
440 {
441   va_list args;
442   va_start (args, format);
443   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
444   va_end (args);
445 }
446 static inline void
447 g_debug (const gchar *format,
448          ...)
449 {
450   va_list args;
451   va_start (args, format);
452   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
453   va_end (args);
454 }
455 #endif  /* !__GNUC__ */
456
457 /**
458  * g_warning_once:
459  * @...: format string, followed by parameters to insert
460  *     into the format string (as with printf())
461  *
462  * Logs a warning only once.
463  *
464  * g_warning_once() calls g_warning() with the passed message the first time
465  * the statement is executed; subsequent times it is a no-op.
466  *
467  * Note! On platforms where the compiler doesn't support variadic macros, the
468  * warning is printed each time instead of only once.
469  *
470  * Since: 2.64
471  */
472 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
473 #define g_warning_once(...) \
474   G_STMT_START { \
475     static volatile int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; \
476     if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
477                                            0, 1)) \
478       g_warning (__VA_ARGS__); \
479   } G_STMT_END \
480   GLIB_AVAILABLE_MACRO_IN_2_64
481 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
482 #define g_warning_once(format...) \
483   G_STMT_START { \
484     static volatile int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; \
485     if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
486                                            0, 1)) \
487       g_warning (format); \
488   } G_STMT_END \
489   GLIB_AVAILABLE_MACRO_IN_2_64
490 #else
491 #define g_warning_once g_warning
492 #endif
493
494 /**
495  * GPrintFunc:
496  * @string: the message to output
497  *
498  * Specifies the type of the print handler functions.
499  * These are called with the complete formatted string to output.
500  */
501 typedef void    (*GPrintFunc)           (const gchar    *string);
502 GLIB_AVAILABLE_IN_ALL
503 void            g_print                 (const gchar    *format,
504                                          ...) G_GNUC_PRINTF (1, 2);
505 GLIB_AVAILABLE_IN_ALL
506 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
507 GLIB_AVAILABLE_IN_ALL
508 void            g_printerr              (const gchar    *format,
509                                          ...) G_GNUC_PRINTF (1, 2);
510 GLIB_AVAILABLE_IN_ALL
511 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
512
513 /**
514  * g_warn_if_reached:
515  *
516  * Logs a warning.
517  *
518  * Since: 2.16
519  */
520 #define g_warn_if_reached() \
521   do { \
522     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
523   } while (0)
524
525 /**
526  * g_warn_if_fail:
527  * @expr: the expression to check
528  *
529  * Logs a warning if the expression is not true.
530  *
531  * Since: 2.16
532  */
533 #define g_warn_if_fail(expr) \
534   do { \
535     if G_LIKELY (expr) ; \
536     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
537   } while (0)
538
539 #ifdef G_DISABLE_CHECKS
540
541 /**
542  * g_return_if_fail:
543  * @expr: the expression to check
544  *
545  * Verifies that the expression @expr, usually representing a precondition,
546  * evaluates to %TRUE. If the function returns a value, use
547  * g_return_val_if_fail() instead.
548  *
549  * If @expr evaluates to %FALSE, the current function should be considered to
550  * have undefined behaviour (a programmer error). The only correct solution
551  * to such an error is to change the module that is calling the current
552  * function, so that it avoids this incorrect call.
553  *
554  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
555  * the result is usually that a critical message is logged and the current
556  * function returns.
557  *
558  * If `G_DISABLE_CHECKS` is defined then the check is not performed.  You
559  * should therefore not depend on any side effects of @expr.
560  *
561  * To debug failure of a g_return_if_fail() check, run the code under a debugger
562  * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
563  * environment (see [Running GLib Applications](glib-running.html)):
564  *
565  * |[
566  *   G_DEBUG=fatal-warnings gdb ./my-program
567  * ]|
568  *
569  * Any unrelated failures can be skipped over in
570  * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
571  */
572 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
573
574 /**
575  * g_return_val_if_fail:
576  * @expr: the expression to check
577  * @val: the value to return from the current function
578  *       if the expression is not true
579  *
580  * Verifies that the expression @expr, usually representing a precondition,
581  * evaluates to %TRUE. If the function does not return a value, use
582  * g_return_if_fail() instead.
583  *
584  * If @expr evaluates to %FALSE, the current function should be considered to
585  * have undefined behaviour (a programmer error). The only correct solution
586  * to such an error is to change the module that is calling the current
587  * function, so that it avoids this incorrect call.
588  *
589  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
590  * the result is usually that a critical message is logged and @val is
591  * returned from the current function.
592  *
593  * If `G_DISABLE_CHECKS` is defined then the check is not performed.  You
594  * should therefore not depend on any side effects of @expr.
595  *
596  * See g_return_if_fail() for guidance on how to debug failure of this check.
597  */
598 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
599
600 /**
601  * g_return_if_reached:
602  *
603  * Logs a critical message and returns from the current function.
604  * This can only be used in functions which do not return a value.
605  *
606  * See g_return_if_fail() for guidance on how to debug failure of this check.
607  */
608 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
609
610 /**
611  * g_return_val_if_reached:
612  * @val: the value to return from the current function
613  *
614  * Logs a critical message and returns @val.
615  *
616  * See g_return_if_fail() for guidance on how to debug failure of this check.
617  */
618 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
619
620 #else /* !G_DISABLE_CHECKS */
621
622 #define g_return_if_fail(expr) \
623   G_STMT_START { \
624     if (G_LIKELY (expr)) \
625       { } \
626     else \
627       { \
628         g_return_if_fail_warning (G_LOG_DOMAIN, \
629                                   G_STRFUNC, \
630                                   #expr); \
631         return; \
632       } \
633   } G_STMT_END
634
635 #define g_return_val_if_fail(expr, val) \
636   G_STMT_START { \
637     if (G_LIKELY (expr)) \
638       { } \
639     else \
640       { \
641         g_return_if_fail_warning (G_LOG_DOMAIN, \
642                                   G_STRFUNC, \
643                                   #expr); \
644         return (val); \
645       } \
646   } G_STMT_END
647
648 #define g_return_if_reached() \
649   G_STMT_START { \
650     g_log (G_LOG_DOMAIN, \
651            G_LOG_LEVEL_CRITICAL, \
652            "file %s: line %d (%s): should not be reached", \
653            __FILE__, \
654            __LINE__, \
655            G_STRFUNC); \
656     return; \
657   } G_STMT_END
658
659 #define g_return_val_if_reached(val) \
660   G_STMT_START { \
661     g_log (G_LOG_DOMAIN, \
662            G_LOG_LEVEL_CRITICAL, \
663            "file %s: line %d (%s): should not be reached", \
664            __FILE__, \
665            __LINE__, \
666            G_STRFUNC); \
667     return (val); \
668   } G_STMT_END
669
670 #endif /* !G_DISABLE_CHECKS */
671
672 G_END_DECLS
673
674 #endif /* __G_MESSAGES_H__ */