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