Add some more cases to the app-id unit tests
[glib.git] / glib / gmessages.h
blobc923aea5886a9029078dfabb1c73773e998e2185
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 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."
30 #endif
32 #include <stdarg.h>
33 #include <glib/gtypes.h>
34 #include <glib/gmacros.h>
35 #include <glib/gvariant.h>
37 G_BEGIN_DECLS
39 /* calculate a string size, guaranteed to fit format + args.
41 GLIB_AVAILABLE_IN_ALL
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.
52 typedef enum
54 /* log flags */
55 G_LOG_FLAG_RECURSION = 1 << 0,
56 G_LOG_FLAG_FATAL = 1 << 1,
58 /* GLib log levels */
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)
67 } GLogLevelFlags;
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,
74 const gchar *message,
75 gpointer user_data);
77 /* Logging mechanism
79 GLIB_AVAILABLE_IN_ALL
80 guint g_log_set_handler (const gchar *log_domain,
81 GLogLevelFlags log_levels,
82 GLogFunc log_func,
83 gpointer user_data);
84 GLIB_AVAILABLE_IN_2_46
85 guint g_log_set_handler_full (const gchar *log_domain,
86 GLogLevelFlags log_levels,
87 GLogFunc log_func,
88 gpointer user_data,
89 GDestroyNotify destroy);
90 GLIB_AVAILABLE_IN_ALL
91 void g_log_remove_handler (const gchar *log_domain,
92 guint handler_id);
93 GLIB_AVAILABLE_IN_ALL
94 void g_log_default_handler (const gchar *log_domain,
95 GLogLevelFlags log_level,
96 const gchar *message,
97 gpointer unused_data);
98 GLIB_AVAILABLE_IN_ALL
99 GLogFunc g_log_set_default_handler (GLogFunc log_func,
100 gpointer user_data);
101 GLIB_AVAILABLE_IN_ALL
102 void g_log (const gchar *log_domain,
103 GLogLevelFlags log_level,
104 const gchar *format,
105 ...) G_GNUC_PRINTF (3, 4);
106 GLIB_AVAILABLE_IN_ALL
107 void g_logv (const gchar *log_domain,
108 GLogLevelFlags log_level,
109 const gchar *format,
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. */
120 * GLogWriterOutput:
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.
131 * Since: 2.50
133 typedef enum
135 G_LOG_WRITER_HANDLED = 1,
136 G_LOG_WRITER_UNHANDLED = 0,
137 } GLogWriterOutput;
140 * GLogField:
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
151 * value.
153 * Since: 2.50
155 typedef struct _GLogField GLogField;
156 struct _GLogField
158 const gchar *key;
159 gconstpointer value;
160 gssize length;
164 * GLogWriterFunc:
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 * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
183 * %G_LOG_WRITER_UNHANDLED otherwise
184 * Since: 2.50
186 typedef GLogWriterOutput (*GLogWriterFunc) (GLogLevelFlags log_level,
187 const GLogField *fields,
188 gsize n_fields,
189 gpointer user_data);
191 GLIB_AVAILABLE_IN_2_50
192 void g_log_structured (const gchar *log_domain,
193 GLogLevelFlags log_level,
194 ...);
195 GLIB_AVAILABLE_IN_2_50
196 void g_log_structured_array (GLogLevelFlags log_level,
197 const GLogField *fields,
198 gsize n_fields);
200 GLIB_AVAILABLE_IN_2_50
201 void g_log_variant (const gchar *log_domain,
202 GLogLevelFlags log_level,
203 GVariant *fields);
205 GLIB_AVAILABLE_IN_2_50
206 void g_log_set_writer_func (GLogWriterFunc func,
207 gpointer user_data,
208 GDestroyNotify user_data_free);
210 GLIB_AVAILABLE_IN_2_50
211 gboolean g_log_writer_supports_color (gint output_fd);
212 GLIB_AVAILABLE_IN_2_50
213 gboolean g_log_writer_is_journald (gint output_fd);
215 GLIB_AVAILABLE_IN_2_50
216 gchar *g_log_writer_format_fields (GLogLevelFlags log_level,
217 const GLogField *fields,
218 gsize n_fields,
219 gboolean use_color);
221 GLIB_AVAILABLE_IN_2_50
222 GLogWriterOutput g_log_writer_journald (GLogLevelFlags log_level,
223 const GLogField *fields,
224 gsize n_fields,
225 gpointer user_data);
226 GLIB_AVAILABLE_IN_2_50
227 GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags log_level,
228 const GLogField *fields,
229 gsize n_fields,
230 gpointer user_data);
231 GLIB_AVAILABLE_IN_2_50
232 GLogWriterOutput g_log_writer_default (GLogLevelFlags log_level,
233 const GLogField *fields,
234 gsize n_fields,
235 gpointer user_data);
238 * G_DEBUG_HERE:
240 * A convenience form of g_log_structured(), recommended to be added to
241 * functions when debugging. It prints the current monotonic time and the code
242 * location using %G_STRLOC.
244 * Since: 2.50
246 #define G_DEBUG_HERE() \
247 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
248 "CODE_FILE", __FILE__, \
249 "CODE_LINE", G_STRINGIFY (__LINE__), \
250 "CODE_FUNC", G_STRFUNC, \
251 "MESSAGE", "%" G_GINT64_FORMAT ": %s", \
252 g_get_monotonic_time (), G_STRLOC)
254 /* internal */
255 void _g_log_fallback_handler (const gchar *log_domain,
256 GLogLevelFlags log_level,
257 const gchar *message,
258 gpointer unused_data);
260 /* Internal functions, used to implement the following macros */
261 GLIB_AVAILABLE_IN_ALL
262 void g_return_if_fail_warning (const char *log_domain,
263 const char *pretty_function,
264 const char *expression) G_ANALYZER_NORETURN;
265 GLIB_AVAILABLE_IN_ALL
266 void g_warn_message (const char *domain,
267 const char *file,
268 int line,
269 const char *func,
270 const char *warnexpr) G_ANALYZER_NORETURN;
271 GLIB_DEPRECATED
272 void g_assert_warning (const char *log_domain,
273 const char *file,
274 const int line,
275 const char *pretty_function,
276 const char *expression) G_GNUC_NORETURN;
279 #ifndef G_LOG_DOMAIN
280 #define G_LOG_DOMAIN ((gchar*) 0)
281 #endif /* G_LOG_DOMAIN */
283 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
284 #ifdef G_LOG_USE_STRUCTURED
285 #define g_error(...) G_STMT_START { \
286 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
287 "CODE_FILE", __FILE__, \
288 "CODE_LINE", G_STRINGIFY (__LINE__), \
289 "CODE_FUNC", G_STRFUNC, \
290 "MESSAGE", __VA_ARGS__); \
291 for (;;) ; \
292 } G_STMT_END
293 #define g_message(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
294 "CODE_FILE", __FILE__, \
295 "CODE_LINE", G_STRINGIFY (__LINE__), \
296 "CODE_FUNC", G_STRFUNC, \
297 "MESSAGE", __VA_ARGS__)
298 #define g_critical(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
299 "CODE_FILE", __FILE__, \
300 "CODE_LINE", G_STRINGIFY (__LINE__), \
301 "CODE_FUNC", G_STRFUNC, \
302 "MESSAGE", __VA_ARGS__)
303 #define g_warning(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
304 "CODE_FILE", __FILE__, \
305 "CODE_LINE", G_STRINGIFY (__LINE__), \
306 "CODE_FUNC", G_STRFUNC, \
307 "MESSAGE", __VA_ARGS__)
308 #define g_info(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
309 "CODE_FILE", __FILE__, \
310 "CODE_LINE", G_STRINGIFY (__LINE__), \
311 "CODE_FUNC", G_STRFUNC, \
312 "MESSAGE", __VA_ARGS__)
313 #define g_debug(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
314 "CODE_FILE", __FILE__, \
315 "CODE_LINE", G_STRINGIFY (__LINE__), \
316 "CODE_FUNC", G_STRFUNC, \
317 "MESSAGE", __VA_ARGS__)
318 #else
319 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
320 * Put space before ending semicolon to avoid C++ build warnings.
322 #define g_error(...) G_STMT_START { \
323 g_log (G_LOG_DOMAIN, \
324 G_LOG_LEVEL_ERROR, \
325 __VA_ARGS__); \
326 for (;;) ; \
327 } G_STMT_END
328 #define g_message(...) g_log (G_LOG_DOMAIN, \
329 G_LOG_LEVEL_MESSAGE, \
330 __VA_ARGS__)
331 #define g_critical(...) g_log (G_LOG_DOMAIN, \
332 G_LOG_LEVEL_CRITICAL, \
333 __VA_ARGS__)
334 #define g_warning(...) g_log (G_LOG_DOMAIN, \
335 G_LOG_LEVEL_WARNING, \
336 __VA_ARGS__)
337 #define g_info(...) g_log (G_LOG_DOMAIN, \
338 G_LOG_LEVEL_INFO, \
339 __VA_ARGS__)
340 #define g_debug(...) g_log (G_LOG_DOMAIN, \
341 G_LOG_LEVEL_DEBUG, \
342 __VA_ARGS__)
343 #endif
344 #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
345 #ifdef G_LOG_USE_STRUCTURED
346 #define g_error(format...) G_STMT_START { \
347 g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
348 "CODE_FILE", __FILE__, \
349 "CODE_LINE", G_STRINGIFY (__LINE__), \
350 "CODE_FUNC", G_STRFUNC, \
351 "MESSAGE", format); \
352 for (;;) ; \
353 } G_STMT_END
354 #define g_message(format...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
355 "CODE_FILE", __FILE__, \
356 "CODE_LINE", G_STRINGIFY (__LINE__), \
357 "CODE_FUNC", G_STRFUNC, \
358 "MESSAGE", format)
359 #define g_critical(format...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
360 "CODE_FILE", __FILE__, \
361 "CODE_LINE", G_STRINGIFY (__LINE__), \
362 "CODE_FUNC", G_STRFUNC, \
363 "MESSAGE", format)
364 #define g_warning(format...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
365 "CODE_FILE", __FILE__, \
366 "CODE_LINE", G_STRINGIFY (__LINE__), \
367 "CODE_FUNC", G_STRFUNC, \
368 "MESSAGE", format)
369 #define g_info(format...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
370 "CODE_FILE", __FILE__, \
371 "CODE_LINE", G_STRINGIFY (__LINE__), \
372 "CODE_FUNC", G_STRFUNC, \
373 "MESSAGE", format)
374 #define g_debug(format...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
375 "CODE_FILE", __FILE__, \
376 "CODE_LINE", G_STRINGIFY (__LINE__), \
377 "CODE_FUNC", G_STRFUNC, \
378 "MESSAGE", format)
379 #else
380 #define g_error(format...) G_STMT_START { \
381 g_log (G_LOG_DOMAIN, \
382 G_LOG_LEVEL_ERROR, \
383 format); \
384 for (;;) ; \
385 } G_STMT_END
387 #define g_message(format...) g_log (G_LOG_DOMAIN, \
388 G_LOG_LEVEL_MESSAGE, \
389 format)
390 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
391 G_LOG_LEVEL_CRITICAL, \
392 format)
393 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
394 G_LOG_LEVEL_WARNING, \
395 format)
396 #define g_info(format...) g_log (G_LOG_DOMAIN, \
397 G_LOG_LEVEL_INFO, \
398 format)
399 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
400 G_LOG_LEVEL_DEBUG, \
401 format)
402 #endif
403 #else /* no varargs macros */
404 static void g_error (const gchar *format, ...) G_GNUC_NORETURN G_ANALYZER_NORETURN;
405 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
407 static void
408 g_error (const gchar *format,
409 ...)
411 va_list args;
412 va_start (args, format);
413 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
414 va_end (args);
416 for(;;) ;
418 static void
419 g_message (const gchar *format,
420 ...)
422 va_list args;
423 va_start (args, format);
424 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
425 va_end (args);
427 static void
428 g_critical (const gchar *format,
429 ...)
431 va_list args;
432 va_start (args, format);
433 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
434 va_end (args);
436 static void
437 g_warning (const gchar *format,
438 ...)
440 va_list args;
441 va_start (args, format);
442 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
443 va_end (args);
445 static void
446 g_info (const gchar *format,
447 ...)
449 va_list args;
450 va_start (args, format);
451 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
452 va_end (args);
454 static void
455 g_debug (const gchar *format,
456 ...)
458 va_list args;
459 va_start (args, format);
460 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
461 va_end (args);
463 #endif /* !__GNUC__ */
466 * GPrintFunc:
467 * @string: the message to output
469 * Specifies the type of the print handler functions.
470 * These are called with the complete formatted string to output.
472 typedef void (*GPrintFunc) (const gchar *string);
473 GLIB_AVAILABLE_IN_ALL
474 void g_print (const gchar *format,
475 ...) G_GNUC_PRINTF (1, 2);
476 GLIB_AVAILABLE_IN_ALL
477 GPrintFunc g_set_print_handler (GPrintFunc func);
478 GLIB_AVAILABLE_IN_ALL
479 void g_printerr (const gchar *format,
480 ...) G_GNUC_PRINTF (1, 2);
481 GLIB_AVAILABLE_IN_ALL
482 GPrintFunc g_set_printerr_handler (GPrintFunc func);
485 * g_warn_if_reached:
487 * Logs a warning.
489 * Since: 2.16
491 #define g_warn_if_reached() \
492 do { \
493 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
494 } while (0)
497 * g_warn_if_fail:
498 * @expr: the expression to check
500 * Logs a warning if the expression is not true.
502 * Since: 2.16
504 #define g_warn_if_fail(expr) \
505 do { \
506 if G_LIKELY (expr) ; \
507 else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
508 } while (0)
510 #ifdef G_DISABLE_CHECKS
513 * g_return_if_fail:
514 * @expr: the expression to check
516 * Verifies that the expression @expr, usually representing a precondition,
517 * evaluates to %TRUE. If the function returns a value, use
518 * g_return_val_if_fail() instead.
520 * If @expr evaluates to %FALSE, the current function should be considered to
521 * have undefined behaviour (a programmer error). The only correct solution
522 * to such an error is to change the module that is calling the current
523 * function, so that it avoids this incorrect call.
525 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
526 * the result is usually that a critical message is logged and the current
527 * function returns.
529 * If G_DISABLE_CHECKS is defined then the check is not performed. You
530 * should therefore not depend on any side effects of @expr.
532 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
535 * g_return_val_if_fail:
536 * @expr: the expression to check
537 * @val: the value to return from the current function
538 * if the expression is not true
540 * Verifies that the expression @expr, usually representing a precondition,
541 * evaluates to %TRUE. If the function does not return a value, use
542 * g_return_if_fail() instead.
544 * If @expr evaluates to %FALSE, the current function should be considered to
545 * have undefined behaviour (a programmer error). The only correct solution
546 * to such an error is to change the module that is calling the current
547 * function, so that it avoids this incorrect call.
549 * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
550 * the result is usually that a critical message is logged and @val is
551 * returned from the current function.
553 * If G_DISABLE_CHECKS is defined then the check is not performed. You
554 * should therefore not depend on any side effects of @expr.
556 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
559 * g_return_if_reached:
561 * Logs a critical message and returns from the current function.
562 * This can only be used in functions which do not return a value.
564 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
567 * g_return_val_if_reached:
568 * @val: the value to return from the current function
570 * Logs a critical message and returns @val.
572 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
574 #else /* !G_DISABLE_CHECKS */
576 #define g_return_if_fail(expr) G_STMT_START{ \
577 if G_LIKELY(expr) { } else \
579 g_return_if_fail_warning (G_LOG_DOMAIN, \
580 G_STRFUNC, \
581 #expr); \
582 return; \
583 }; }G_STMT_END
585 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
586 if G_LIKELY(expr) { } else \
588 g_return_if_fail_warning (G_LOG_DOMAIN, \
589 G_STRFUNC, \
590 #expr); \
591 return (val); \
592 }; }G_STMT_END
594 #define g_return_if_reached() G_STMT_START{ \
595 g_log (G_LOG_DOMAIN, \
596 G_LOG_LEVEL_CRITICAL, \
597 "file %s: line %d (%s): should not be reached", \
598 __FILE__, \
599 __LINE__, \
600 G_STRFUNC); \
601 return; }G_STMT_END
603 #define g_return_val_if_reached(val) G_STMT_START{ \
604 g_log (G_LOG_DOMAIN, \
605 G_LOG_LEVEL_CRITICAL, \
606 "file %s: line %d (%s): should not be reached", \
607 __FILE__, \
608 __LINE__, \
609 G_STRFUNC); \
610 return (val); }G_STMT_END
612 #endif /* !G_DISABLE_CHECKS */
614 G_END_DECLS
616 #endif /* __G_MESSAGES_H__ */