1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
25 #ifndef __G_MESSAGES_H__
26 #define __G_MESSAGES_H__
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
33 #include <glib/gatomic.h>
34 #include <glib/gtypes.h>
35 #include <glib/gmacros.h>
36 #include <glib/gvariant.h>
40 /* calculate a string size, guaranteed to fit format + args.
43 gsize g_printf_string_upper_bound (const gchar* format,
44 va_list args) G_GNUC_PRINTF(1, 0);
46 /* Log level shift offset for user defined
47 * log levels (0-7 are used by GLib).
49 #define G_LOG_LEVEL_USER_SHIFT (8)
51 /* Glib log levels and flags.
56 G_LOG_FLAG_RECURSION = 1 << 0,
57 G_LOG_FLAG_FATAL = 1 << 1,
60 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
61 G_LOG_LEVEL_CRITICAL = 1 << 3,
62 G_LOG_LEVEL_WARNING = 1 << 4,
63 G_LOG_LEVEL_MESSAGE = 1 << 5,
64 G_LOG_LEVEL_INFO = 1 << 6,
65 G_LOG_LEVEL_DEBUG = 1 << 7,
67 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
70 /* GLib log levels that are considered fatal by default */
71 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
73 typedef void (*GLogFunc) (const gchar *log_domain,
74 GLogLevelFlags log_level,
81 guint g_log_set_handler (const gchar *log_domain,
82 GLogLevelFlags log_levels,
85 GLIB_AVAILABLE_IN_2_46
86 guint g_log_set_handler_full (const gchar *log_domain,
87 GLogLevelFlags log_levels,
90 GDestroyNotify destroy);
92 void g_log_remove_handler (const gchar *log_domain,
95 void g_log_default_handler (const gchar *log_domain,
96 GLogLevelFlags log_level,
98 gpointer unused_data);
100 GLogFunc g_log_set_default_handler (GLogFunc log_func,
102 GLIB_AVAILABLE_IN_ALL
103 void g_log (const gchar *log_domain,
104 GLogLevelFlags log_level,
106 ...) G_GNUC_PRINTF (3, 4);
107 GLIB_AVAILABLE_IN_ALL
108 void g_logv (const gchar *log_domain,
109 GLogLevelFlags log_level,
111 va_list args) G_GNUC_PRINTF(3, 0);
112 GLIB_AVAILABLE_IN_ALL
113 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
114 GLogLevelFlags fatal_mask);
115 GLIB_AVAILABLE_IN_ALL
116 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
118 /* Structured logging mechanism. */
122 * @G_LOG_WRITER_HANDLED: Log writer has handled the log entry.
123 * @G_LOG_WRITER_UNHANDLED: Log writer could not handle the log entry.
125 * Return values from #GLogWriterFuncs to indicate whether the given log entry
126 * was successfully handled by the writer, or whether there was an error in
127 * handling it (and hence a fallback writer should be used).
129 * If a #GLogWriterFunc ignores a log entry, it should return
130 * %G_LOG_WRITER_HANDLED.
136 G_LOG_WRITER_HANDLED = 1,
137 G_LOG_WRITER_UNHANDLED = 0,
142 * @key: field name (UTF-8 string)
143 * @value: field value (arbitrary bytes)
144 * @length: length of @value, in bytes, or -1 if it is nul-terminated
146 * Structure representing a single field in a structured log entry. See
147 * g_log_structured() for details.
149 * Log fields may contain arbitrary values, including binary with embedded nul
150 * bytes. If the field contains a string, the string must be UTF-8 encoded and
151 * have a trailing nul byte. Otherwise, @length must be set to a non-negative
156 typedef struct _GLogField GLogField;
166 * @log_level: log level of the message
167 * @fields: (array length=n_fields): fields forming the message
168 * @n_fields: number of @fields
169 * @user_data: user data passed to g_log_set_writer_func()
171 * Writer function for log entries. A log entry is a collection of one or more
172 * #GLogFields, using the standard [field names from journal
173 * specification](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html).
174 * See g_log_structured() for more information.
176 * Writer functions must ignore fields which they do not recognise, unless they
177 * can write arbitrary binary output, as field values may be arbitrary binary.
179 * @log_level is guaranteed to be included in @fields as the `PRIORITY` field,
180 * but is provided separately for convenience of deciding whether or where to
181 * output the log entry.
183 * Writer functions should return %G_LOG_WRITER_HANDLED if they handled the log
184 * message successfully or if they deliberately ignored it. If there was an
185 * error handling the message (for example, if the writer function is meant to
186 * send messages to a remote logging server and there is a network error), it
187 * should return %G_LOG_WRITER_UNHANDLED. This allows writer functions to be
188 * chained and fall back to simpler handlers in case of failure.
190 * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
191 * %G_LOG_WRITER_UNHANDLED otherwise
195 typedef GLogWriterOutput (*GLogWriterFunc) (GLogLevelFlags log_level,
196 const GLogField *fields,
200 GLIB_AVAILABLE_IN_2_50
201 void g_log_structured (const gchar *log_domain,
202 GLogLevelFlags log_level,
204 GLIB_AVAILABLE_IN_2_50
205 void g_log_structured_array (GLogLevelFlags log_level,
206 const GLogField *fields,
209 GLIB_AVAILABLE_IN_2_50
210 void g_log_variant (const gchar *log_domain,
211 GLogLevelFlags log_level,
214 GLIB_AVAILABLE_IN_2_50
215 void g_log_set_writer_func (GLogWriterFunc func,
217 GDestroyNotify user_data_free);
219 GLIB_AVAILABLE_IN_2_50
220 gboolean g_log_writer_supports_color (gint output_fd);
221 GLIB_AVAILABLE_IN_2_50
222 gboolean g_log_writer_is_journald (gint output_fd);
224 GLIB_AVAILABLE_IN_2_50
225 gchar *g_log_writer_format_fields (GLogLevelFlags log_level,
226 const GLogField *fields,
230 GLIB_AVAILABLE_IN_2_50
231 GLogWriterOutput g_log_writer_journald (GLogLevelFlags log_level,
232 const GLogField *fields,
235 GLIB_AVAILABLE_IN_2_50
236 GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags log_level,
237 const GLogField *fields,
240 GLIB_AVAILABLE_IN_2_50
241 GLogWriterOutput g_log_writer_default (GLogLevelFlags log_level,
242 const GLogField *fields,
246 GLIB_AVAILABLE_IN_2_68
247 void g_log_writer_default_set_use_stderr (gboolean use_stderr);
248 GLIB_AVAILABLE_IN_2_68
249 gboolean g_log_writer_default_would_drop (GLogLevelFlags log_level,
250 const char *log_domain);
255 * A convenience form of g_log_structured(), recommended to be added to
256 * functions when debugging. It prints the current monotonic time and the code
257 * location using %G_STRLOC.
261 #define G_DEBUG_HERE() \
262 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
263 "CODE_FILE", __FILE__, \
264 "CODE_LINE", G_STRINGIFY (__LINE__), \
265 "CODE_FUNC", G_STRFUNC, \
266 "MESSAGE", "%" G_GINT64_FORMAT ": %s", \
267 g_get_monotonic_time (), G_STRLOC)
270 void _g_log_fallback_handler (const gchar *log_domain,
271 GLogLevelFlags log_level,
272 const gchar *message,
273 gpointer unused_data);
275 /* Internal functions, used to implement the following macros */
276 GLIB_AVAILABLE_IN_ALL
277 void g_return_if_fail_warning (const char *log_domain,
278 const char *pretty_function,
279 const char *expression) G_ANALYZER_NORETURN;
280 GLIB_AVAILABLE_IN_ALL
281 void g_warn_message (const char *domain,
285 const char *warnexpr) G_ANALYZER_NORETURN;
288 void g_assert_warning (const char *log_domain,
291 const char *pretty_function,
292 const char *expression);
294 GLIB_AVAILABLE_IN_2_56
295 void g_log_structured_standard (const gchar *log_domain,
296 GLogLevelFlags log_level,
300 const gchar *message_format,
301 ...) G_GNUC_PRINTF (6, 7);
304 #define G_LOG_DOMAIN ((gchar*) 0)
305 #endif /* G_LOG_DOMAIN */
307 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
308 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
309 #define g_error(...) G_STMT_START { \
310 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
311 __FILE__, G_STRINGIFY (__LINE__), \
312 G_STRFUNC, __VA_ARGS__); \
315 #define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
316 __FILE__, G_STRINGIFY (__LINE__), \
317 G_STRFUNC, __VA_ARGS__)
318 #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
319 __FILE__, G_STRINGIFY (__LINE__), \
320 G_STRFUNC, __VA_ARGS__)
321 #define g_warning(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
322 __FILE__, G_STRINGIFY (__LINE__), \
323 G_STRFUNC, __VA_ARGS__)
324 #define g_info(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
325 __FILE__, G_STRINGIFY (__LINE__), \
326 G_STRFUNC, __VA_ARGS__)
327 #define g_debug(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
328 __FILE__, G_STRINGIFY (__LINE__), \
329 G_STRFUNC, __VA_ARGS__)
331 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
332 * Put space before ending semicolon to avoid C++ build warnings.
334 #define g_error(...) G_STMT_START { \
335 g_log (G_LOG_DOMAIN, \
340 #define g_message(...) g_log (G_LOG_DOMAIN, \
341 G_LOG_LEVEL_MESSAGE, \
343 #define g_critical(...) g_log (G_LOG_DOMAIN, \
344 G_LOG_LEVEL_CRITICAL, \
346 #define g_warning(...) g_log (G_LOG_DOMAIN, \
347 G_LOG_LEVEL_WARNING, \
349 #define g_info(...) g_log (G_LOG_DOMAIN, \
352 #define g_debug(...) g_log (G_LOG_DOMAIN, \
356 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
357 #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
358 #define g_error(format...) G_STMT_START { \
359 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
360 __FILE__, G_STRINGIFY (__LINE__), \
361 G_STRFUNC, format); \
364 #define g_message(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
365 __FILE__, G_STRINGIFY (__LINE__), \
367 #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
368 __FILE__, G_STRINGIFY (__LINE__), \
370 #define g_warning(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
371 __FILE__, G_STRINGIFY (__LINE__), \
373 #define g_info(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
374 __FILE__, G_STRINGIFY (__LINE__), \
376 #define g_debug(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
377 __FILE__, G_STRINGIFY (__LINE__), \
380 #define g_error(format...) G_STMT_START { \
381 g_log (G_LOG_DOMAIN, \
387 #define g_message(format...) g_log (G_LOG_DOMAIN, \
388 G_LOG_LEVEL_MESSAGE, \
390 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
391 G_LOG_LEVEL_CRITICAL, \
393 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
394 G_LOG_LEVEL_WARNING, \
396 #define g_info(format...) g_log (G_LOG_DOMAIN, \
399 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
403 #else /* no varargs macros */
404 static G_NORETURN void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
405 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
408 g_error (const gchar *format,
412 va_start (args, format);
413 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
419 g_message (const gchar *format,
423 va_start (args, format);
424 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
428 g_critical (const gchar *format,
432 va_start (args, format);
433 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
437 g_warning (const gchar *format,
441 va_start (args, format);
442 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
446 g_info (const gchar *format,
450 va_start (args, format);
451 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
455 g_debug (const gchar *format,
459 va_start (args, format);
460 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
463 #endif /* !__GNUC__ */
467 * @...: format string, followed by parameters to insert
468 * into the format string (as with printf())
470 * Logs a warning only once.
472 * g_warning_once() calls g_warning() with the passed message the first time
473 * the statement is executed; subsequent times it is a no-op.
475 * Note! On platforms where the compiler doesn't support variadic macros, the
476 * warning is printed each time instead of only once.
480 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
481 #define g_warning_once(...) \
483 static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
484 if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
486 g_warning (__VA_ARGS__); \
488 GLIB_AVAILABLE_MACRO_IN_2_64
489 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
490 #define g_warning_once(format...) \
492 static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
493 if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
495 g_warning (format); \
497 GLIB_AVAILABLE_MACRO_IN_2_64
499 #define g_warning_once g_warning
504 * @string: the message to output
506 * Specifies the type of the print handler functions.
507 * These are called with the complete formatted string to output.
509 typedef void (*GPrintFunc) (const gchar *string);
510 GLIB_AVAILABLE_IN_ALL
511 void g_print (const gchar *format,
512 ...) G_GNUC_PRINTF (1, 2);
513 GLIB_AVAILABLE_IN_ALL
514 GPrintFunc g_set_print_handler (GPrintFunc func);
515 GLIB_AVAILABLE_IN_ALL
516 void g_printerr (const gchar *format,
517 ...) G_GNUC_PRINTF (1, 2);
518 GLIB_AVAILABLE_IN_ALL
519 GPrintFunc g_set_printerr_handler (GPrintFunc func);
528 #define g_warn_if_reached() \
530 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
535 * @expr: the expression to check
537 * Logs a warning if the expression is not true.
541 #define g_warn_if_fail(expr) \
543 if G_LIKELY (expr) ; \
544 else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
547 #ifdef G_DISABLE_CHECKS
551 * @expr: the expression to check
553 * Verifies that the expression @expr, usually representing a precondition,
554 * evaluates to %TRUE. If the function returns a value, use
555 * g_return_val_if_fail() instead.
557 * If @expr evaluates to %FALSE, the current function should be considered to
558 * have undefined behaviour (a programmer error). The only correct solution
559 * to such an error is to change the module that is calling the current
560 * function, so that it avoids this incorrect call.
562 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
563 * the result is usually that a critical message is logged and the current
566 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
567 * should therefore not depend on any side effects of @expr.
569 * To debug failure of a g_return_if_fail() check, run the code under a debugger
570 * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
571 * environment (see [Running GLib Applications](glib-running.html)):
574 * G_DEBUG=fatal-warnings gdb ./my-program
577 * Any unrelated failures can be skipped over in
578 * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
580 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
583 * g_return_val_if_fail:
584 * @expr: the expression to check
585 * @val: the value to return from the current function
586 * if the expression is not true
588 * Verifies that the expression @expr, usually representing a precondition,
589 * evaluates to %TRUE. If the function does not return a value, use
590 * g_return_if_fail() instead.
592 * If @expr evaluates to %FALSE, the current function should be considered to
593 * have undefined behaviour (a programmer error). The only correct solution
594 * to such an error is to change the module that is calling the current
595 * function, so that it avoids this incorrect call.
597 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
598 * the result is usually that a critical message is logged and @val is
599 * returned from the current function.
601 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
602 * should therefore not depend on any side effects of @expr.
604 * See g_return_if_fail() for guidance on how to debug failure of this check.
606 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
609 * g_return_if_reached:
611 * Logs a critical message and returns from the current function.
612 * This can only be used in functions which do not return a value.
614 * See g_return_if_fail() for guidance on how to debug failure of this check.
616 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
619 * g_return_val_if_reached:
620 * @val: the value to return from the current function
622 * Logs a critical message and returns @val.
624 * See g_return_if_fail() for guidance on how to debug failure of this check.
626 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
628 #else /* !G_DISABLE_CHECKS */
630 #define g_return_if_fail(expr) \
632 if (G_LIKELY (expr)) \
636 g_return_if_fail_warning (G_LOG_DOMAIN, \
643 #define g_return_val_if_fail(expr, val) \
645 if (G_LIKELY (expr)) \
649 g_return_if_fail_warning (G_LOG_DOMAIN, \
656 #define g_return_if_reached() \
658 g_log (G_LOG_DOMAIN, \
659 G_LOG_LEVEL_CRITICAL, \
660 "file %s: line %d (%s): should not be reached", \
667 #define g_return_val_if_reached(val) \
669 g_log (G_LOG_DOMAIN, \
670 G_LOG_LEVEL_CRITICAL, \
671 "file %s: line %d (%s): should not be reached", \
678 #endif /* !G_DISABLE_CHECKS */
682 #endif /* __G_MESSAGES_H__ */