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__
31 #include <glib/gtypes.h>
32 #include <glib/gmacros.h>
34 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
36 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
37 #pragma GCC system_header
42 /* calculate a string size, guaranteed to fit format + args.
44 gsize g_printf_string_upper_bound (const gchar* format,
47 /* Log level shift offset for user defined
48 * log levels (0-7 are used by GLib).
50 #define G_LOG_LEVEL_USER_SHIFT (8)
52 /* Glib log levels and flags.
57 G_LOG_FLAG_RECURSION = 1 << 0,
58 G_LOG_FLAG_FATAL = 1 << 1,
61 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
62 G_LOG_LEVEL_CRITICAL = 1 << 3,
63 G_LOG_LEVEL_WARNING = 1 << 4,
64 G_LOG_LEVEL_MESSAGE = 1 << 5,
65 G_LOG_LEVEL_INFO = 1 << 6,
66 G_LOG_LEVEL_DEBUG = 1 << 7,
68 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
71 /* GLib log levels that are considered fatal by default */
72 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
74 typedef void (*GLogFunc) (const gchar *log_domain,
75 GLogLevelFlags log_level,
81 guint g_log_set_handler (const gchar *log_domain,
82 GLogLevelFlags log_levels,
85 void g_log_remove_handler (const gchar *log_domain,
87 void g_log_default_handler (const gchar *log_domain,
88 GLogLevelFlags log_level,
90 gpointer unused_data);
91 GLogFunc g_log_set_default_handler (GLogFunc log_func,
93 void g_log (const gchar *log_domain,
94 GLogLevelFlags log_level,
96 ...) G_GNUC_PRINTF (3, 4);
97 void g_logv (const gchar *log_domain,
98 GLogLevelFlags log_level,
101 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
102 GLogLevelFlags fatal_mask);
103 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
106 G_GNUC_INTERNAL void _g_log_fallback_handler (const gchar *log_domain,
107 GLogLevelFlags log_level,
108 const gchar *message,
109 gpointer unused_data);
111 /* Internal functions, used to implement the following macros */
112 void g_return_if_fail_warning (const char *log_domain,
113 const char *pretty_function,
114 const char *expression);
115 void g_warn_message (const char *domain,
119 const char *warnexpr);
120 #ifndef G_DISABLE_DEPRECATED
121 void g_assert_warning (const char *log_domain,
124 const char *pretty_function,
125 const char *expression) G_GNUC_NORETURN;
126 #endif /* !G_DISABLE_DEPRECATED */
130 #define G_LOG_DOMAIN ((gchar*) 0)
131 #endif /* G_LOG_DOMAIN */
132 #ifdef G_HAVE_ISO_VARARGS
133 #define g_error(...) g_log (G_LOG_DOMAIN, \
136 #define g_message(...) g_log (G_LOG_DOMAIN, \
137 G_LOG_LEVEL_MESSAGE, \
139 #define g_critical(...) g_log (G_LOG_DOMAIN, \
140 G_LOG_LEVEL_CRITICAL, \
142 #define g_warning(...) g_log (G_LOG_DOMAIN, \
143 G_LOG_LEVEL_WARNING, \
145 #define g_debug(...) g_log (G_LOG_DOMAIN, \
148 #elif defined(G_HAVE_GNUC_VARARGS)
149 #define g_error(format...) g_log (G_LOG_DOMAIN, \
152 #define g_message(format...) g_log (G_LOG_DOMAIN, \
153 G_LOG_LEVEL_MESSAGE, \
155 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
156 G_LOG_LEVEL_CRITICAL, \
158 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
159 G_LOG_LEVEL_WARNING, \
161 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
164 #else /* no varargs macros */
166 g_error (const gchar *format,
170 va_start (args, format);
171 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
175 g_message (const gchar *format,
179 va_start (args, format);
180 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
184 g_critical (const gchar *format,
188 va_start (args, format);
189 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
193 g_warning (const gchar *format,
197 va_start (args, format);
198 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
202 g_debug (const gchar *format,
206 va_start (args, format);
207 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
210 #endif /* !__GNUC__ */
212 typedef void (*GPrintFunc) (const gchar *string);
213 void g_print (const gchar *format,
214 ...) G_GNUC_PRINTF (1, 2);
215 GPrintFunc g_set_print_handler (GPrintFunc func);
216 void g_printerr (const gchar *format,
217 ...) G_GNUC_PRINTF (1, 2);
218 GPrintFunc g_set_printerr_handler (GPrintFunc func);
221 /* Provide macros for graceful error handling.
222 * The "return" macros will return from the current function.
223 * Two different definitions are given for the macros in
224 * order to support gcc's __PRETTY_FUNCTION__ capability.
227 #define g_warn_if_reached() do { g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
228 #define g_warn_if_fail(expr) do { if G_LIKELY (expr) ; else \
229 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); } while (0)
231 #ifdef G_DISABLE_CHECKS
233 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
234 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
235 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
236 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
238 #else /* !G_DISABLE_CHECKS */
242 #define g_return_if_fail(expr) G_STMT_START{ \
243 if G_LIKELY(expr) { } else \
245 g_return_if_fail_warning (G_LOG_DOMAIN, \
246 __PRETTY_FUNCTION__, \
251 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
252 if G_LIKELY(expr) { } else \
254 g_return_if_fail_warning (G_LOG_DOMAIN, \
255 __PRETTY_FUNCTION__, \
260 #define g_return_if_reached() G_STMT_START{ \
261 g_log (G_LOG_DOMAIN, \
262 G_LOG_LEVEL_CRITICAL, \
263 "file %s: line %d (%s): should not be reached", \
266 __PRETTY_FUNCTION__); \
269 #define g_return_val_if_reached(val) G_STMT_START{ \
270 g_log (G_LOG_DOMAIN, \
271 G_LOG_LEVEL_CRITICAL, \
272 "file %s: line %d (%s): should not be reached", \
275 __PRETTY_FUNCTION__); \
276 return (val); }G_STMT_END
278 #else /* !__GNUC__ */
280 #define g_return_if_fail(expr) G_STMT_START{ \
283 g_log (G_LOG_DOMAIN, \
284 G_LOG_LEVEL_CRITICAL, \
285 "file %s: line %d: assertion `%s' failed", \
292 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
295 g_log (G_LOG_DOMAIN, \
296 G_LOG_LEVEL_CRITICAL, \
297 "file %s: line %d: assertion `%s' failed", \
304 #define g_return_if_reached() G_STMT_START{ \
305 g_log (G_LOG_DOMAIN, \
306 G_LOG_LEVEL_CRITICAL, \
307 "file %s: line %d: should not be reached", \
312 #define g_return_val_if_reached(val) G_STMT_START{ \
313 g_log (G_LOG_DOMAIN, \
314 G_LOG_LEVEL_CRITICAL, \
315 "file %s: line %d: should not be reached", \
318 return (val); }G_STMT_END
320 #endif /* !__GNUC__ */
322 #endif /* !G_DISABLE_CHECKS */
326 #endif /* __G_MESSAGES_H__ */