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