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