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