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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/gtypes.h>
36 #include <glib/gmacros.h>
38 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
40 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
41 #pragma GCC system_header
46 /* calculate a string size, guaranteed to fit format + args.
49 gsize g_printf_string_upper_bound (const gchar* format,
50 va_list args) G_GNUC_PRINTF(1, 0);
52 /* Log level shift offset for user defined
53 * log levels (0-7 are used by GLib).
55 #define G_LOG_LEVEL_USER_SHIFT (8)
57 /* Glib log levels and flags.
62 G_LOG_FLAG_RECURSION = 1 << 0,
63 G_LOG_FLAG_FATAL = 1 << 1,
66 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
67 G_LOG_LEVEL_CRITICAL = 1 << 3,
68 G_LOG_LEVEL_WARNING = 1 << 4,
69 G_LOG_LEVEL_MESSAGE = 1 << 5,
70 G_LOG_LEVEL_INFO = 1 << 6,
71 G_LOG_LEVEL_DEBUG = 1 << 7,
73 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
76 /* GLib log levels that are considered fatal by default */
77 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
79 typedef void (*GLogFunc) (const gchar *log_domain,
80 GLogLevelFlags log_level,
87 guint g_log_set_handler (const gchar *log_domain,
88 GLogLevelFlags log_levels,
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);
119 G_GNUC_INTERNAL void _g_log_fallback_handler (const gchar *log_domain,
120 GLogLevelFlags log_level,
121 const gchar *message,
122 gpointer unused_data);
124 /* Internal functions, used to implement the following macros */
125 GLIB_AVAILABLE_IN_ALL
126 void g_return_if_fail_warning (const char *log_domain,
127 const char *pretty_function,
128 const char *expression);
129 GLIB_AVAILABLE_IN_ALL
130 void g_warn_message (const char *domain,
134 const char *warnexpr);
136 void g_assert_warning (const char *log_domain,
139 const char *pretty_function,
140 const char *expression) G_GNUC_NORETURN;
144 #define G_LOG_DOMAIN ((gchar*) 0)
145 #endif /* G_LOG_DOMAIN */
146 #ifdef G_HAVE_ISO_VARARGS
147 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
148 * Put space before ending semicolon to avoid C++ build warnings.
150 #define g_error(...) G_STMT_START { \
151 g_log (G_LOG_DOMAIN, \
157 #define g_message(...) g_log (G_LOG_DOMAIN, \
158 G_LOG_LEVEL_MESSAGE, \
160 #define g_critical(...) g_log (G_LOG_DOMAIN, \
161 G_LOG_LEVEL_CRITICAL, \
163 #define g_warning(...) g_log (G_LOG_DOMAIN, \
164 G_LOG_LEVEL_WARNING, \
166 #define g_debug(...) g_log (G_LOG_DOMAIN, \
169 #elif defined(G_HAVE_GNUC_VARARGS)
170 #define g_error(format...) G_STMT_START { \
171 g_log (G_LOG_DOMAIN, \
177 #define g_message(format...) g_log (G_LOG_DOMAIN, \
178 G_LOG_LEVEL_MESSAGE, \
180 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
181 G_LOG_LEVEL_CRITICAL, \
183 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
184 G_LOG_LEVEL_WARNING, \
186 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
189 #else /* no varargs macros */
191 g_error (const gchar *format,
195 va_start (args, format);
196 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
202 g_message (const gchar *format,
206 va_start (args, format);
207 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
211 g_critical (const gchar *format,
215 va_start (args, format);
216 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
220 g_warning (const gchar *format,
224 va_start (args, format);
225 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
229 g_debug (const gchar *format,
233 va_start (args, format);
234 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
237 #endif /* !__GNUC__ */
241 * @string: the message to output
243 * Specifies the type of the print handler functions.
244 * These are called with the complete formatted string to output.
246 typedef void (*GPrintFunc) (const gchar *string);
247 GLIB_AVAILABLE_IN_ALL
248 void g_print (const gchar *format,
249 ...) G_GNUC_PRINTF (1, 2);
250 GLIB_AVAILABLE_IN_ALL
251 GPrintFunc g_set_print_handler (GPrintFunc func);
252 GLIB_AVAILABLE_IN_ALL
253 void g_printerr (const gchar *format,
254 ...) G_GNUC_PRINTF (1, 2);
255 GLIB_AVAILABLE_IN_ALL
256 GPrintFunc g_set_printerr_handler (GPrintFunc func);
261 * Logs a critical warning.
265 #define g_warn_if_reached() \
267 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
272 * @expr: the expression to check
274 * Logs a warning if the expression is not true.
278 #define g_warn_if_fail(expr) \
280 if G_LIKELY (expr) ; \
281 else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
284 #ifdef G_DISABLE_CHECKS
288 * @expr: the expression to check
290 * Verifies that the expression evaluates to %TRUE. If the expression
291 * evaluates to %FALSE, a critical message is logged and the current
292 * function returns. This can only be used in functions which do not
295 * If G_DISABLE_CHECKS is defined then the check is not performed. You
296 * should therefore not depend on any side effects of @expr.
298 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
301 * g_return_val_if_fail:
302 * @expr: the expression to check
303 * @val: the value to return from the current function
304 * if the expression is not true
306 * Verifies that the expression evaluates to %TRUE. If the expression
307 * evaluates to %FALSE, a critical message is logged and @val is
308 * returned from the current function.
310 * If G_DISABLE_CHECKS is defined then the check is not performed. You
311 * should therefore not depend on any side effects of @expr.
313 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
316 * g_return_if_reached:
318 * Logs a critical message and returns from the current function.
319 * This can only be used in functions which do not return a value.
321 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
324 * g_return_val_if_reached:
325 * @val: the value to return from the current function
327 * Logs a critical message and returns @val.
329 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
331 #else /* !G_DISABLE_CHECKS */
335 #define g_return_if_fail(expr) G_STMT_START{ \
336 if G_LIKELY(expr) { } else \
338 g_return_if_fail_warning (G_LOG_DOMAIN, \
339 __PRETTY_FUNCTION__, \
344 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
345 if G_LIKELY(expr) { } else \
347 g_return_if_fail_warning (G_LOG_DOMAIN, \
348 __PRETTY_FUNCTION__, \
353 #define g_return_if_reached() G_STMT_START{ \
354 g_log (G_LOG_DOMAIN, \
355 G_LOG_LEVEL_CRITICAL, \
356 "file %s: line %d (%s): should not be reached", \
359 __PRETTY_FUNCTION__); \
362 #define g_return_val_if_reached(val) G_STMT_START{ \
363 g_log (G_LOG_DOMAIN, \
364 G_LOG_LEVEL_CRITICAL, \
365 "file %s: line %d (%s): should not be reached", \
368 __PRETTY_FUNCTION__); \
369 return (val); }G_STMT_END
371 #else /* !__GNUC__ */
373 #define g_return_if_fail(expr) G_STMT_START{ \
376 g_log (G_LOG_DOMAIN, \
377 G_LOG_LEVEL_CRITICAL, \
378 "file %s: line %d: assertion `%s' failed", \
385 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
388 g_log (G_LOG_DOMAIN, \
389 G_LOG_LEVEL_CRITICAL, \
390 "file %s: line %d: assertion `%s' failed", \
397 #define g_return_if_reached() G_STMT_START{ \
398 g_log (G_LOG_DOMAIN, \
399 G_LOG_LEVEL_CRITICAL, \
400 "file %s: line %d: should not be reached", \
405 #define g_return_val_if_reached(val) G_STMT_START{ \
406 g_log (G_LOG_DOMAIN, \
407 G_LOG_LEVEL_CRITICAL, \
408 "file %s: line %d: should not be reached", \
411 return (val); }G_STMT_END
413 #endif /* !__GNUC__ */
415 #endif /* !G_DISABLE_CHECKS */
419 #endif /* __G_MESSAGES_H__ */