1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
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/.
27 #ifndef __G_MESSAGES_H__
28 #define __G_MESSAGES_H__
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
35 #include <glib/gatomic.h>
36 #include <glib/gtypes.h>
37 #include <glib/gmacros.h>
38 #include <glib/gvariant.h>
42 /* calculate a string size, guaranteed to fit format + args.
45 gsize g_printf_string_upper_bound (const gchar* format,
46 va_list args) G_GNUC_PRINTF(1, 0);
48 /* Log level shift offset for user defined
49 * log levels (0-7 are used by GLib).
51 #define G_LOG_LEVEL_USER_SHIFT (8)
53 /* Glib log levels and flags.
58 G_LOG_FLAG_RECURSION = 1 << 0,
59 G_LOG_FLAG_FATAL = 1 << 1,
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,
69 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
72 /* GLib log levels that are considered fatal by default */
73 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
75 typedef void (*GLogFunc) (const gchar *log_domain,
76 GLogLevelFlags log_level,
83 guint g_log_set_handler (const gchar *log_domain,
84 GLogLevelFlags log_levels,
87 GLIB_AVAILABLE_IN_2_46
88 guint g_log_set_handler_full (const gchar *log_domain,
89 GLogLevelFlags log_levels,
92 GDestroyNotify destroy);
94 void g_log_remove_handler (const gchar *log_domain,
97 void g_log_default_handler (const gchar *log_domain,
98 GLogLevelFlags log_level,
100 gpointer unused_data);
101 GLIB_AVAILABLE_IN_ALL
102 GLogFunc g_log_set_default_handler (GLogFunc log_func,
104 GLIB_AVAILABLE_IN_ALL
105 void g_log (const gchar *log_domain,
106 GLogLevelFlags log_level,
108 ...) G_GNUC_PRINTF (3, 4);
109 GLIB_AVAILABLE_IN_ALL
110 void g_logv (const gchar *log_domain,
111 GLogLevelFlags log_level,
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);
120 /* Structured logging mechanism. */
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.
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).
131 * If a #GLogWriterFunc ignores a log entry, it should return
132 * %G_LOG_WRITER_HANDLED.
138 G_LOG_WRITER_HANDLED = 1,
139 G_LOG_WRITER_UNHANDLED = 0,
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
148 * Structure representing a single field in a structured log entry. See
149 * g_log_structured() for details.
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
158 typedef struct _GLogField GLogField;
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()
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.
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.
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.
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.
192 * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
193 * %G_LOG_WRITER_UNHANDLED otherwise
197 typedef GLogWriterOutput (*GLogWriterFunc) (GLogLevelFlags log_level,
198 const GLogField *fields,
202 GLIB_AVAILABLE_IN_2_50
203 void g_log_structured (const gchar *log_domain,
204 GLogLevelFlags log_level,
206 GLIB_AVAILABLE_IN_2_50
207 void g_log_structured_array (GLogLevelFlags log_level,
208 const GLogField *fields,
211 GLIB_AVAILABLE_IN_2_50
212 void g_log_variant (const gchar *log_domain,
213 GLogLevelFlags log_level,
216 GLIB_AVAILABLE_IN_2_50
217 void g_log_set_writer_func (GLogWriterFunc func,
219 GDestroyNotify user_data_free);
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);
226 GLIB_AVAILABLE_IN_2_50
227 gchar *g_log_writer_format_fields (GLogLevelFlags log_level,
228 const GLogField *fields,
232 GLIB_AVAILABLE_IN_2_50
233 GLogWriterOutput g_log_writer_journald (GLogLevelFlags log_level,
234 const GLogField *fields,
237 GLIB_AVAILABLE_IN_2_50
238 GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags log_level,
239 const GLogField *fields,
242 GLIB_AVAILABLE_IN_2_50
243 GLogWriterOutput g_log_writer_default (GLogLevelFlags log_level,
244 const GLogField *fields,
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);
254 /* G_MESSAGES_DEBUG enablement */
255 GLIB_AVAILABLE_IN_2_72
256 gboolean g_log_get_debug_enabled (void);
257 GLIB_AVAILABLE_IN_2_72
258 void g_log_set_debug_enabled (gboolean enabled);
263 * A convenience form of g_log_structured(), recommended to be added to
264 * functions when debugging. It prints the current monotonic time and the code
265 * location using %G_STRLOC.
269 #define G_DEBUG_HERE() \
270 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
271 "CODE_FILE", __FILE__, \
272 "CODE_LINE", G_STRINGIFY (__LINE__), \
273 "CODE_FUNC", G_STRFUNC, \
274 "MESSAGE", "%" G_GINT64_FORMAT ": %s", \
275 g_get_monotonic_time (), G_STRLOC)
278 void _g_log_fallback_handler (const gchar *log_domain,
279 GLogLevelFlags log_level,
280 const gchar *message,
281 gpointer unused_data);
283 /* Internal functions, used to implement the following macros */
284 GLIB_AVAILABLE_IN_ALL
285 void g_return_if_fail_warning (const char *log_domain,
286 const char *pretty_function,
287 const char *expression) G_ANALYZER_NORETURN;
288 GLIB_AVAILABLE_IN_ALL
289 void g_warn_message (const char *domain,
293 const char *warnexpr) G_ANALYZER_NORETURN;
296 void g_assert_warning (const char *log_domain,
299 const char *pretty_function,
300 const char *expression);
302 GLIB_AVAILABLE_IN_2_56
303 void g_log_structured_standard (const gchar *log_domain,
304 GLogLevelFlags log_level,
308 const gchar *message_format,
309 ...) G_GNUC_PRINTF (6, 7);
312 #define G_LOG_DOMAIN ((gchar*) 0)
313 #endif /* G_LOG_DOMAIN */
315 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
316 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
317 #define g_error(...) G_STMT_START { \
318 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
319 __FILE__, G_STRINGIFY (__LINE__), \
320 G_STRFUNC, __VA_ARGS__); \
323 #define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
324 __FILE__, G_STRINGIFY (__LINE__), \
325 G_STRFUNC, __VA_ARGS__)
326 #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
327 __FILE__, G_STRINGIFY (__LINE__), \
328 G_STRFUNC, __VA_ARGS__)
329 #define g_warning(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
330 __FILE__, G_STRINGIFY (__LINE__), \
331 G_STRFUNC, __VA_ARGS__)
332 #define g_info(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
333 __FILE__, G_STRINGIFY (__LINE__), \
334 G_STRFUNC, __VA_ARGS__)
335 #define g_debug(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
336 __FILE__, G_STRINGIFY (__LINE__), \
337 G_STRFUNC, __VA_ARGS__)
339 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
340 * Put space before ending semicolon to avoid C++ build warnings.
342 #define g_error(...) G_STMT_START { \
343 g_log (G_LOG_DOMAIN, \
348 #define g_message(...) g_log (G_LOG_DOMAIN, \
349 G_LOG_LEVEL_MESSAGE, \
351 #define g_critical(...) g_log (G_LOG_DOMAIN, \
352 G_LOG_LEVEL_CRITICAL, \
354 #define g_warning(...) g_log (G_LOG_DOMAIN, \
355 G_LOG_LEVEL_WARNING, \
357 #define g_info(...) g_log (G_LOG_DOMAIN, \
360 #define g_debug(...) g_log (G_LOG_DOMAIN, \
364 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
365 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
366 #define g_error(format...) G_STMT_START { \
367 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
368 __FILE__, G_STRINGIFY (__LINE__), \
369 G_STRFUNC, format); \
372 #define g_message(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
373 __FILE__, G_STRINGIFY (__LINE__), \
375 #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
376 __FILE__, G_STRINGIFY (__LINE__), \
378 #define g_warning(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
379 __FILE__, G_STRINGIFY (__LINE__), \
381 #define g_info(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
382 __FILE__, G_STRINGIFY (__LINE__), \
384 #define g_debug(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
385 __FILE__, G_STRINGIFY (__LINE__), \
388 #define g_error(format...) G_STMT_START { \
389 g_log (G_LOG_DOMAIN, \
395 #define g_message(format...) g_log (G_LOG_DOMAIN, \
396 G_LOG_LEVEL_MESSAGE, \
398 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
399 G_LOG_LEVEL_CRITICAL, \
401 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
402 G_LOG_LEVEL_WARNING, \
404 #define g_info(format...) g_log (G_LOG_DOMAIN, \
407 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
411 #else /* no varargs macros */
412 static G_NORETURN void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
413 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
416 g_error (const gchar *format,
420 va_start (args, format);
421 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
427 g_message (const gchar *format,
431 va_start (args, format);
432 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
436 g_critical (const gchar *format,
440 va_start (args, format);
441 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
445 g_warning (const gchar *format,
449 va_start (args, format);
450 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
454 g_info (const gchar *format,
458 va_start (args, format);
459 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
463 g_debug (const gchar *format,
467 va_start (args, format);
468 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
471 #endif /* !__GNUC__ */
475 * @...: format string, followed by parameters to insert
476 * into the format string (as with printf())
478 * Logs a warning only once.
480 * g_warning_once() calls g_warning() with the passed message the first time
481 * the statement is executed; subsequent times it is a no-op.
483 * Note! On platforms where the compiler doesn't support variadic macros, the
484 * warning is printed each time instead of only once.
488 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
489 #define g_warning_once(...) \
491 static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
492 if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
494 g_warning (__VA_ARGS__); \
496 GLIB_AVAILABLE_MACRO_IN_2_64
497 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
498 #define g_warning_once(format...) \
500 static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
501 if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
503 g_warning (format); \
505 GLIB_AVAILABLE_MACRO_IN_2_64
507 #define g_warning_once g_warning
512 * @string: the message to output
514 * Specifies the type of the print handler functions.
515 * These are called with the complete formatted string to output.
517 typedef void (*GPrintFunc) (const gchar *string);
518 GLIB_AVAILABLE_IN_ALL
519 void g_print (const gchar *format,
520 ...) G_GNUC_PRINTF (1, 2);
521 GLIB_AVAILABLE_IN_ALL
522 GPrintFunc g_set_print_handler (GPrintFunc func);
523 GLIB_AVAILABLE_IN_ALL
524 void g_printerr (const gchar *format,
525 ...) G_GNUC_PRINTF (1, 2);
526 GLIB_AVAILABLE_IN_ALL
527 GPrintFunc g_set_printerr_handler (GPrintFunc func);
536 #define g_warn_if_reached() \
538 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
543 * @expr: the expression to check
545 * Logs a warning if the expression is not true.
549 #define g_warn_if_fail(expr) \
551 if G_LIKELY (expr) ; \
552 else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
555 #ifdef G_DISABLE_CHECKS
559 * @expr: the expression to check
561 * Verifies that the expression @expr, usually representing a precondition,
562 * evaluates to %TRUE. If the function returns a value, use
563 * g_return_val_if_fail() instead.
565 * If @expr evaluates to %FALSE, the current function should be considered to
566 * have undefined behaviour (a programmer error). The only correct solution
567 * to such an error is to change the module that is calling the current
568 * function, so that it avoids this incorrect call.
570 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
571 * the result is usually that a critical message is logged and the current
574 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
575 * should therefore not depend on any side effects of @expr.
577 * To debug failure of a g_return_if_fail() check, run the code under a debugger
578 * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
579 * environment (see [Running GLib Applications](glib-running.html)):
582 * G_DEBUG=fatal-warnings gdb ./my-program
585 * Any unrelated failures can be skipped over in
586 * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
588 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
591 * g_return_val_if_fail:
592 * @expr: the expression to check
593 * @val: the value to return from the current function
594 * if the expression is not true
596 * Verifies that the expression @expr, usually representing a precondition,
597 * evaluates to %TRUE. If the function does not return a value, use
598 * g_return_if_fail() instead.
600 * If @expr evaluates to %FALSE, the current function should be considered to
601 * have undefined behaviour (a programmer error). The only correct solution
602 * to such an error is to change the module that is calling the current
603 * function, so that it avoids this incorrect call.
605 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
606 * the result is usually that a critical message is logged and @val is
607 * returned from the current function.
609 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
610 * should therefore not depend on any side effects of @expr.
612 * See g_return_if_fail() for guidance on how to debug failure of this check.
614 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
617 * g_return_if_reached:
619 * Logs a critical message and returns from the current function.
620 * This can only be used in functions which do not return a value.
622 * See g_return_if_fail() for guidance on how to debug failure of this check.
624 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
627 * g_return_val_if_reached:
628 * @val: the value to return from the current function
630 * Logs a critical message and returns @val.
632 * See g_return_if_fail() for guidance on how to debug failure of this check.
634 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
636 #else /* !G_DISABLE_CHECKS */
638 #define g_return_if_fail(expr) \
640 if (G_LIKELY (expr)) \
644 g_return_if_fail_warning (G_LOG_DOMAIN, \
651 #define g_return_val_if_fail(expr, val) \
653 if (G_LIKELY (expr)) \
657 g_return_if_fail_warning (G_LOG_DOMAIN, \
664 #define g_return_if_reached() \
666 g_log (G_LOG_DOMAIN, \
667 G_LOG_LEVEL_CRITICAL, \
668 "file %s: line %d (%s): should not be reached", \
675 #define g_return_val_if_reached(val) \
677 g_log (G_LOG_DOMAIN, \
678 G_LOG_LEVEL_CRITICAL, \
679 "file %s: line %d (%s): should not be reached", \
686 #endif /* !G_DISABLE_CHECKS */
690 #endif /* __G_MESSAGES_H__ */