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/gtypes.h>
34 #include <glib/gmacros.h>
35 #include <glib/gvariant.h>
39 /* calculate a string size, guaranteed to fit format + args.
42 gsize
g_printf_string_upper_bound (const gchar
* format
,
43 va_list args
) G_GNUC_PRINTF(1, 0);
45 /* Log level shift offset for user defined
46 * log levels (0-7 are used by GLib).
48 #define G_LOG_LEVEL_USER_SHIFT (8)
50 /* Glib log levels and flags.
55 G_LOG_FLAG_RECURSION
= 1 << 0,
56 G_LOG_FLAG_FATAL
= 1 << 1,
59 G_LOG_LEVEL_ERROR
= 1 << 2, /* always fatal */
60 G_LOG_LEVEL_CRITICAL
= 1 << 3,
61 G_LOG_LEVEL_WARNING
= 1 << 4,
62 G_LOG_LEVEL_MESSAGE
= 1 << 5,
63 G_LOG_LEVEL_INFO
= 1 << 6,
64 G_LOG_LEVEL_DEBUG
= 1 << 7,
66 G_LOG_LEVEL_MASK
= ~(G_LOG_FLAG_RECURSION
| G_LOG_FLAG_FATAL
)
69 /* GLib log levels that are considered fatal by default */
70 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
72 typedef void (*GLogFunc
) (const gchar
*log_domain
,
73 GLogLevelFlags log_level
,
80 guint
g_log_set_handler (const gchar
*log_domain
,
81 GLogLevelFlags log_levels
,
84 GLIB_AVAILABLE_IN_2_46
85 guint
g_log_set_handler_full (const gchar
*log_domain
,
86 GLogLevelFlags log_levels
,
89 GDestroyNotify destroy
);
91 void g_log_remove_handler (const gchar
*log_domain
,
94 void g_log_default_handler (const gchar
*log_domain
,
95 GLogLevelFlags log_level
,
97 gpointer unused_data
);
99 GLogFunc
g_log_set_default_handler (GLogFunc log_func
,
101 GLIB_AVAILABLE_IN_ALL
102 void g_log (const gchar
*log_domain
,
103 GLogLevelFlags log_level
,
105 ...) G_GNUC_PRINTF (3, 4);
106 GLIB_AVAILABLE_IN_ALL
107 void g_logv (const gchar
*log_domain
,
108 GLogLevelFlags log_level
,
110 va_list args
) G_GNUC_PRINTF(3, 0);
111 GLIB_AVAILABLE_IN_ALL
112 GLogLevelFlags
g_log_set_fatal_mask (const gchar
*log_domain
,
113 GLogLevelFlags fatal_mask
);
114 GLIB_AVAILABLE_IN_ALL
115 GLogLevelFlags
g_log_set_always_fatal (GLogLevelFlags fatal_mask
);
117 /* Structured logging mechanism. */
121 * @G_LOG_WRITER_HANDLED: Log writer has handled the log entry.
122 * @G_LOG_WRITER_UNHANDLED: Log writer could not handle the log entry.
124 * Return values from #GLogWriterFuncs to indicate whether the given log entry
125 * was successfully handled by the writer, or whether there was an error in
126 * handling it (and hence a fallback writer should be used).
128 * If a #GLogWriterFunc ignores a log entry, it should return
129 * %G_LOG_WRITER_HANDLED.
135 G_LOG_WRITER_HANDLED
= 1,
136 G_LOG_WRITER_UNHANDLED
= 0,
141 * @key: field name (UTF-8 string)
142 * @value: field value (arbitrary bytes)
143 * @length: length of @value, in bytes, or -1 if it is nul-terminated
145 * Structure representing a single field in a structured log entry. See
146 * g_log_structured() for details.
148 * Log fields may contain arbitrary values, including binary with embedded nul
149 * bytes. If the field contains a string, the string must be UTF-8 encoded and
150 * have a trailing nul byte. Otherwise, @length must be set to a non-negative
155 typedef struct _GLogField GLogField
;
165 * @log_level: log level of the message
166 * @fields: (array length=n_fields): fields forming the message
167 * @n_fields: number of @fields
168 * @user_data: user data passed to g_log_set_writer_func()
170 * Writer function for log entries. A log entry is a collection of one or more
171 * #GLogFields, using the standard [field names from journal
172 * specification](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html).
173 * See g_log_structured() for more information.
175 * Writer functions must ignore fields which they do not recognise, unless they
176 * can write arbitrary binary output, as field values may be arbitrary binary.
178 * @log_level is guaranteed to be included in @fields as the `PRIORITY` field,
179 * but is provided separately for convenience of deciding whether or where to
180 * output the log entry.
182 * Writer functions should return %G_LOG_WRITER_HANDLED if they handled the log
183 * message successfully or if they deliberately ignored it. If there was an
184 * error handling the message (for example, if the writer function is meant to
185 * send messages to a remote logging server and there is a network error), it
186 * should return %G_LOG_WRITER_UNHANDLED. This allows writer functions to be
187 * chained and fall back to simpler handlers in case of failure.
189 * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
190 * %G_LOG_WRITER_UNHANDLED otherwise
193 typedef GLogWriterOutput (*GLogWriterFunc
) (GLogLevelFlags log_level
,
194 const GLogField
*fields
,
198 GLIB_AVAILABLE_IN_2_50
199 void g_log_structured (const gchar
*log_domain
,
200 GLogLevelFlags log_level
,
202 GLIB_AVAILABLE_IN_2_50
203 void g_log_structured_array (GLogLevelFlags log_level
,
204 const GLogField
*fields
,
207 GLIB_AVAILABLE_IN_2_50
208 void g_log_variant (const gchar
*log_domain
,
209 GLogLevelFlags log_level
,
212 GLIB_AVAILABLE_IN_2_50
213 void g_log_set_writer_func (GLogWriterFunc func
,
215 GDestroyNotify user_data_free
);
217 GLIB_AVAILABLE_IN_2_50
218 gboolean
g_log_writer_supports_color (gint output_fd
);
219 GLIB_AVAILABLE_IN_2_50
220 gboolean
g_log_writer_is_journald (gint output_fd
);
222 GLIB_AVAILABLE_IN_2_50
223 gchar
*g_log_writer_format_fields (GLogLevelFlags log_level
,
224 const GLogField
*fields
,
228 GLIB_AVAILABLE_IN_2_50
229 GLogWriterOutput
g_log_writer_journald (GLogLevelFlags log_level
,
230 const GLogField
*fields
,
233 GLIB_AVAILABLE_IN_2_50
234 GLogWriterOutput
g_log_writer_standard_streams (GLogLevelFlags log_level
,
235 const GLogField
*fields
,
238 GLIB_AVAILABLE_IN_2_50
239 GLogWriterOutput
g_log_writer_default (GLogLevelFlags log_level
,
240 const GLogField
*fields
,
247 * A convenience form of g_log_structured(), recommended to be added to
248 * functions when debugging. It prints the current monotonic time and the code
249 * location using %G_STRLOC.
253 #define G_DEBUG_HERE() \
254 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
255 "CODE_FILE", __FILE__, \
256 "CODE_LINE", G_STRINGIFY (__LINE__), \
257 "CODE_FUNC", G_STRFUNC, \
258 "MESSAGE", "%" G_GINT64_FORMAT ": %s", \
259 g_get_monotonic_time (), G_STRLOC)
262 void _g_log_fallback_handler (const gchar
*log_domain
,
263 GLogLevelFlags log_level
,
264 const gchar
*message
,
265 gpointer unused_data
);
267 /* Internal functions, used to implement the following macros */
268 GLIB_AVAILABLE_IN_ALL
269 void g_return_if_fail_warning (const char *log_domain
,
270 const char *pretty_function
,
271 const char *expression
) G_ANALYZER_NORETURN
;
272 GLIB_AVAILABLE_IN_ALL
273 void g_warn_message (const char *domain
,
277 const char *warnexpr
) G_ANALYZER_NORETURN
;
279 void g_assert_warning (const char *log_domain
,
282 const char *pretty_function
,
283 const char *expression
) G_GNUC_NORETURN
;
285 GLIB_AVAILABLE_IN_2_56
286 void g_log_structured_standard (const gchar
*log_domain
,
287 GLogLevelFlags log_level
,
291 const gchar
*message_format
,
292 ...) G_GNUC_PRINTF (6, 7);
295 #define G_LOG_DOMAIN ((gchar*) 0)
296 #endif /* G_LOG_DOMAIN */
298 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
299 #ifdef G_LOG_USE_STRUCTURED
300 #define g_error(...) G_STMT_START { \
301 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
302 __FILE__, G_STRINGIFY (__LINE__), \
303 G_STRFUNC, __VA_ARGS__); \
306 #define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
307 __FILE__, G_STRINGIFY (__LINE__), \
308 G_STRFUNC, __VA_ARGS__)
309 #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
310 __FILE__, G_STRINGIFY (__LINE__), \
311 G_STRFUNC, __VA_ARGS__)
312 #define g_warning(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
313 __FILE__, G_STRINGIFY (__LINE__), \
314 G_STRFUNC, __VA_ARGS__)
315 #define g_info(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
316 __FILE__, G_STRINGIFY (__LINE__), \
317 G_STRFUNC, __VA_ARGS__)
318 #define g_debug(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
319 __FILE__, G_STRINGIFY (__LINE__), \
320 G_STRFUNC, __VA_ARGS__)
322 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
323 * Put space before ending semicolon to avoid C++ build warnings.
325 #define g_error(...) G_STMT_START { \
326 g_log (G_LOG_DOMAIN, \
331 #define g_message(...) g_log (G_LOG_DOMAIN, \
332 G_LOG_LEVEL_MESSAGE, \
334 #define g_critical(...) g_log (G_LOG_DOMAIN, \
335 G_LOG_LEVEL_CRITICAL, \
337 #define g_warning(...) g_log (G_LOG_DOMAIN, \
338 G_LOG_LEVEL_WARNING, \
340 #define g_info(...) g_log (G_LOG_DOMAIN, \
343 #define g_debug(...) g_log (G_LOG_DOMAIN, \
347 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
348 #ifdef G_LOG_USE_STRUCTURED
349 #define g_error(format...) G_STMT_START { \
350 g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
351 __FILE__, G_STRINGIFY (__LINE__), \
352 G_STRFUNC, format); \
355 #define g_message(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
356 __FILE__, G_STRINGIFY (__LINE__), \
358 #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
359 __FILE__, G_STRINGIFY (__LINE__), \
361 #define g_warning(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
362 __FILE__, G_STRINGIFY (__LINE__), \
364 #define g_info(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
365 __FILE__, G_STRINGIFY (__LINE__), \
367 #define g_debug(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
368 __FILE__, G_STRINGIFY (__LINE__), \
371 #define g_error(format...) G_STMT_START { \
372 g_log (G_LOG_DOMAIN, \
378 #define g_message(format...) g_log (G_LOG_DOMAIN, \
379 G_LOG_LEVEL_MESSAGE, \
381 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
382 G_LOG_LEVEL_CRITICAL, \
384 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
385 G_LOG_LEVEL_WARNING, \
387 #define g_info(format...) g_log (G_LOG_DOMAIN, \
390 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
394 #else /* no varargs macros */
395 static void g_error (const gchar
*format
, ...) G_GNUC_NORETURN G_ANALYZER_NORETURN
;
396 static void g_critical (const gchar
*format
, ...) G_ANALYZER_NORETURN
;
399 g_error (const gchar
*format
,
403 va_start (args
, format
);
404 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_ERROR
, format
, args
);
410 g_message (const gchar
*format
,
414 va_start (args
, format
);
415 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_MESSAGE
, format
, args
);
419 g_critical (const gchar
*format
,
423 va_start (args
, format
);
424 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, format
, args
);
428 g_warning (const gchar
*format
,
432 va_start (args
, format
);
433 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
, format
, args
);
437 g_info (const gchar
*format
,
441 va_start (args
, format
);
442 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, format
, args
);
446 g_debug (const gchar
*format
,
450 va_start (args
, format
);
451 g_logv (G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, format
, args
);
454 #endif /* !__GNUC__ */
458 * @string: the message to output
460 * Specifies the type of the print handler functions.
461 * These are called with the complete formatted string to output.
463 typedef void (*GPrintFunc
) (const gchar
*string
);
464 GLIB_AVAILABLE_IN_ALL
465 void g_print (const gchar
*format
,
466 ...) G_GNUC_PRINTF (1, 2);
467 GLIB_AVAILABLE_IN_ALL
468 GPrintFunc
g_set_print_handler (GPrintFunc func
);
469 GLIB_AVAILABLE_IN_ALL
470 void g_printerr (const gchar
*format
,
471 ...) G_GNUC_PRINTF (1, 2);
472 GLIB_AVAILABLE_IN_ALL
473 GPrintFunc
g_set_printerr_handler (GPrintFunc func
);
482 #define g_warn_if_reached() \
484 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
489 * @expr: the expression to check
491 * Logs a warning if the expression is not true.
495 #define g_warn_if_fail(expr) \
497 if G_LIKELY (expr) ; \
498 else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
501 #ifdef G_DISABLE_CHECKS
505 * @expr: the expression to check
507 * Verifies that the expression @expr, usually representing a precondition,
508 * evaluates to %TRUE. If the function returns a value, use
509 * g_return_val_if_fail() instead.
511 * If @expr evaluates to %FALSE, the current function should be considered to
512 * have undefined behaviour (a programmer error). The only correct solution
513 * to such an error is to change the module that is calling the current
514 * function, so that it avoids this incorrect call.
516 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
517 * the result is usually that a critical message is logged and the current
520 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
521 * should therefore not depend on any side effects of @expr.
523 * To debug failure of a g_return_if_fail() check, run the code under a debugger
524 * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
525 * environment (see [Running GLib Applications](glib-running.html)):
528 * G_DEBUG=fatal-warnings gdb ./my-program
531 * Any unrelated failures can be skipped over in
532 * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
534 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
537 * g_return_val_if_fail:
538 * @expr: the expression to check
539 * @val: the value to return from the current function
540 * if the expression is not true
542 * Verifies that the expression @expr, usually representing a precondition,
543 * evaluates to %TRUE. If the function does not return a value, use
544 * g_return_if_fail() instead.
546 * If @expr evaluates to %FALSE, the current function should be considered to
547 * have undefined behaviour (a programmer error). The only correct solution
548 * to such an error is to change the module that is calling the current
549 * function, so that it avoids this incorrect call.
551 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
552 * the result is usually that a critical message is logged and @val is
553 * returned from the current function.
555 * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
556 * should therefore not depend on any side effects of @expr.
558 * See g_return_if_fail() for guidance on how to debug failure of this check.
560 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
563 * g_return_if_reached:
565 * Logs a critical message and returns from the current function.
566 * This can only be used in functions which do not return a value.
568 * See g_return_if_fail() for guidance on how to debug failure of this check.
570 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
573 * g_return_val_if_reached:
574 * @val: the value to return from the current function
576 * Logs a critical message and returns @val.
578 * See g_return_if_fail() for guidance on how to debug failure of this check.
580 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
582 #else /* !G_DISABLE_CHECKS */
584 #define g_return_if_fail(expr) G_STMT_START{ \
585 if G_LIKELY(expr) { } else \
587 g_return_if_fail_warning (G_LOG_DOMAIN, \
593 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
594 if G_LIKELY(expr) { } else \
596 g_return_if_fail_warning (G_LOG_DOMAIN, \
602 #define g_return_if_reached() G_STMT_START{ \
603 g_log (G_LOG_DOMAIN, \
604 G_LOG_LEVEL_CRITICAL, \
605 "file %s: line %d (%s): should not be reached", \
611 #define g_return_val_if_reached(val) G_STMT_START{ \
612 g_log (G_LOG_DOMAIN, \
613 G_LOG_LEVEL_CRITICAL, \
614 "file %s: line %d (%s): should not be reached", \
618 return (val); }G_STMT_END
620 #endif /* !G_DISABLE_CHECKS */
624 #endif /* __G_MESSAGES_H__ */