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 void g_log (const gchar *log_domain,
92 GLogLevelFlags log_level,
94 ...) G_GNUC_PRINTF (3, 4);
95 void g_logv (const gchar *log_domain,
96 GLogLevelFlags log_level,
99 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
100 GLogLevelFlags fatal_mask);
101 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
104 void _g_log_fallback_handler (const gchar *log_domain,
105 GLogLevelFlags log_level,
106 const gchar *message,
107 gpointer unused_data) G_GNUC_INTERNAL;
111 #define G_LOG_DOMAIN ((gchar*) 0)
112 #endif /* G_LOG_DOMAIN */
113 #ifdef G_HAVE_ISO_VARARGS
114 #define g_error(...) g_log (G_LOG_DOMAIN, \
117 #define g_message(...) g_log (G_LOG_DOMAIN, \
118 G_LOG_LEVEL_MESSAGE, \
120 #define g_critical(...) g_log (G_LOG_DOMAIN, \
121 G_LOG_LEVEL_CRITICAL, \
123 #define g_warning(...) g_log (G_LOG_DOMAIN, \
124 G_LOG_LEVEL_WARNING, \
126 #define g_debug(...) g_log (G_LOG_DOMAIN, \
129 #elif defined(G_HAVE_GNUC_VARARGS)
130 #define g_error(format...) g_log (G_LOG_DOMAIN, \
133 #define g_message(format...) g_log (G_LOG_DOMAIN, \
134 G_LOG_LEVEL_MESSAGE, \
136 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
137 G_LOG_LEVEL_CRITICAL, \
139 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
140 G_LOG_LEVEL_WARNING, \
142 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
143 G_LOG_LEVEL_WARNING, \
145 #else /* no varargs macros */
147 g_error (const gchar *format,
151 va_start (args, format);
152 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
156 g_message (const gchar *format,
160 va_start (args, format);
161 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
165 g_critical (const gchar *format,
169 va_start (args, format);
170 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
174 g_warning (const gchar *format,
178 va_start (args, format);
179 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
183 g_debug (const gchar *format,
187 va_start (args, format);
188 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
191 #endif /* !__GNUC__ */
193 typedef void (*GPrintFunc) (const gchar *string);
194 void g_print (const gchar *format,
195 ...) G_GNUC_PRINTF (1, 2);
196 GPrintFunc g_set_print_handler (GPrintFunc func);
197 void g_printerr (const gchar *format,
198 ...) G_GNUC_PRINTF (1, 2);
199 GPrintFunc g_set_printerr_handler (GPrintFunc func);
202 /* Provide macros for error handling. The "assert" macros will
203 * exit on failure. The "return" macros will exit the current
204 * function. Two different definitions are given for the macros
205 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
206 * __PRETTY_FUNCTION__ capability.
209 #ifdef G_DISABLE_ASSERT
211 #define g_assert(expr) G_STMT_START{ (void)0; }G_STMT_END
212 #define g_assert_not_reached() G_STMT_START{ (void)0; }G_STMT_END
214 #else /* !G_DISABLE_ASSERT */
218 #define g_assert(expr) G_STMT_START{ \
219 if G_LIKELY(expr) { } else \
220 g_log (G_LOG_DOMAIN, \
222 "file %s: line %d (%s): assertion failed: (%s)", \
225 __PRETTY_FUNCTION__, \
228 #define g_assert_not_reached() G_STMT_START{ \
229 g_log (G_LOG_DOMAIN, \
231 "file %s: line %d (%s): should not be reached", \
234 __PRETTY_FUNCTION__); }G_STMT_END
236 #else /* !__GNUC__ */
238 #define g_assert(expr) G_STMT_START{ \
240 g_log (G_LOG_DOMAIN, \
242 "file %s: line %d: assertion failed: (%s)", \
247 #define g_assert_not_reached() G_STMT_START{ \
248 g_log (G_LOG_DOMAIN, \
250 "file %s: line %d: should not be reached", \
252 __LINE__); }G_STMT_END
254 #endif /* __GNUC__ */
256 #endif /* !G_DISABLE_ASSERT */
259 #ifdef G_DISABLE_CHECKS
261 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
262 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
263 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
264 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
266 #else /* !G_DISABLE_CHECKS */
270 #define g_return_if_fail(expr) G_STMT_START{ \
271 if G_LIKELY(expr) { } else \
273 g_log (G_LOG_DOMAIN, \
274 G_LOG_LEVEL_CRITICAL, \
275 "file %s: line %d (%s): assertion `%s' failed", \
278 __PRETTY_FUNCTION__, \
283 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
284 if G_LIKELY(expr) { } else \
286 g_log (G_LOG_DOMAIN, \
287 G_LOG_LEVEL_CRITICAL, \
288 "file %s: line %d (%s): assertion `%s' failed", \
291 __PRETTY_FUNCTION__, \
296 #define g_return_if_reached() G_STMT_START{ \
297 g_log (G_LOG_DOMAIN, \
298 G_LOG_LEVEL_CRITICAL, \
299 "file %s: line %d (%s): should not be reached", \
302 __PRETTY_FUNCTION__); \
305 #define g_return_val_if_reached(val) G_STMT_START{ \
306 g_log (G_LOG_DOMAIN, \
307 G_LOG_LEVEL_CRITICAL, \
308 "file %s: line %d (%s): should not be reached", \
311 __PRETTY_FUNCTION__); \
312 return (val); }G_STMT_END
314 #else /* !__GNUC__ */
316 #define g_return_if_fail(expr) G_STMT_START{ \
319 g_log (G_LOG_DOMAIN, \
320 G_LOG_LEVEL_CRITICAL, \
321 "file %s: line %d: assertion `%s' failed", \
328 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
331 g_log (G_LOG_DOMAIN, \
332 G_LOG_LEVEL_CRITICAL, \
333 "file %s: line %d: assertion `%s' failed", \
340 #define g_return_if_reached() G_STMT_START{ \
341 g_log (G_LOG_DOMAIN, \
342 G_LOG_LEVEL_CRITICAL, \
343 "file %s: line %d: should not be reached", \
348 #define g_return_val_if_reached(val) G_STMT_START{ \
349 g_log (G_LOG_DOMAIN, \
350 G_LOG_LEVEL_CRITICAL, \
351 "file %s: line %d: should not be reached", \
354 return (val); }G_STMT_END
356 #endif /* !__GNUC__ */
358 #endif /* !G_DISABLE_CHECKS */
362 #endif /* __G_MESSAGES_H__ */