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