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>
33 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
35 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
36 #pragma GCC system_header
41 /* calculate a string size, guarranteed to fit format + args.
43 gsize g_printf_string_upper_bound (const gchar* format,
46 /* Log level shift offset for user defined
47 * log levels (0-7 are used by GLib).
49 #define G_LOG_LEVEL_USER_SHIFT (8)
51 /* Glib log levels and flags.
56 G_LOG_FLAG_RECURSION = 1 << 0,
57 G_LOG_FLAG_FATAL = 1 << 1,
60 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
61 G_LOG_LEVEL_CRITICAL = 1 << 3,
62 G_LOG_LEVEL_WARNING = 1 << 4,
63 G_LOG_LEVEL_MESSAGE = 1 << 5,
64 G_LOG_LEVEL_INFO = 1 << 6,
65 G_LOG_LEVEL_DEBUG = 1 << 7,
67 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
70 /* GLib log levels that are considered fatal by default */
71 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
73 typedef void (*GLogFunc) (const gchar *log_domain,
74 GLogLevelFlags log_level,
80 guint g_log_set_handler (const gchar *log_domain,
81 GLogLevelFlags log_levels,
84 void g_log_remove_handler (const gchar *log_domain,
86 void g_log_default_handler (const gchar *log_domain,
87 GLogLevelFlags log_level,
89 gpointer unused_data);
90 void g_log (const gchar *log_domain,
91 GLogLevelFlags log_level,
93 ...) G_GNUC_PRINTF (3, 4);
94 void g_logv (const gchar *log_domain,
95 GLogLevelFlags log_level,
98 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
99 GLogLevelFlags fatal_mask);
100 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
103 void _g_log_fallback_handler (const gchar *log_domain,
104 GLogLevelFlags log_level,
105 const gchar *message,
106 gpointer unused_data);
110 #define G_LOG_DOMAIN ((gchar*) 0)
111 #endif /* G_LOG_DOMAIN */
112 #ifdef G_HAVE_ISO_VARARGS
113 #define g_error(...) g_log (G_LOG_DOMAIN, \
116 #define g_message(...) g_log (G_LOG_DOMAIN, \
117 G_LOG_LEVEL_MESSAGE, \
119 #define g_critical(...) g_log (G_LOG_DOMAIN, \
120 G_LOG_LEVEL_CRITICAL, \
122 #define g_warning(...) g_log (G_LOG_DOMAIN, \
123 G_LOG_LEVEL_WARNING, \
125 #elif defined(G_HAVE_GNUC_VARARGS)
126 #define g_error(format...) g_log (G_LOG_DOMAIN, \
129 #define g_message(format...) g_log (G_LOG_DOMAIN, \
130 G_LOG_LEVEL_MESSAGE, \
132 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
133 G_LOG_LEVEL_CRITICAL, \
135 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
136 G_LOG_LEVEL_WARNING, \
138 #else /* no varargs macros */
140 g_error (const gchar *format,
144 va_start (args, format);
145 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
149 g_message (const gchar *format,
153 va_start (args, format);
154 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
158 g_critical (const gchar *format,
162 va_start (args, format);
163 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
167 g_warning (const gchar *format,
171 va_start (args, format);
172 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
175 #endif /* !__GNUC__ */
177 typedef void (*GPrintFunc) (const gchar *string);
178 void g_print (const gchar *format,
179 ...) G_GNUC_PRINTF (1, 2);
180 GPrintFunc g_set_print_handler (GPrintFunc func);
181 void g_printerr (const gchar *format,
182 ...) G_GNUC_PRINTF (1, 2);
183 GPrintFunc g_set_printerr_handler (GPrintFunc func);
186 /* Provide macros for error handling. The "assert" macros will
187 * exit on failure. The "return" macros will exit the current
188 * function. Two different definitions are given for the macros
189 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
190 * __PRETTY_FUNCTION__ capability.
193 #ifdef G_DISABLE_ASSERT
195 #define g_assert(expr) G_STMT_START{ (void)0; }G_STMT_END
196 #define g_assert_not_reached() G_STMT_START{ (void)0; }G_STMT_END
198 #else /* !G_DISABLE_ASSERT */
202 #define g_assert(expr) G_STMT_START{ \
204 g_log (G_LOG_DOMAIN, \
206 "file %s: line %d (%s): assertion failed: (%s)", \
209 __PRETTY_FUNCTION__, \
212 #define g_assert_not_reached() G_STMT_START{ \
213 g_log (G_LOG_DOMAIN, \
215 "file %s: line %d (%s): should not be reached", \
218 __PRETTY_FUNCTION__); }G_STMT_END
220 #else /* !__GNUC__ */
222 #define g_assert(expr) G_STMT_START{ \
224 g_log (G_LOG_DOMAIN, \
226 "file %s: line %d: assertion failed: (%s)", \
231 #define g_assert_not_reached() G_STMT_START{ \
232 g_log (G_LOG_DOMAIN, \
234 "file %s: line %d: should not be reached", \
236 __LINE__); }G_STMT_END
238 #endif /* __GNUC__ */
240 #endif /* !G_DISABLE_ASSERT */
243 #ifdef G_DISABLE_CHECKS
245 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
246 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
247 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
248 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
250 #else /* !G_DISABLE_CHECKS */
254 #define g_return_if_fail(expr) G_STMT_START{ \
257 g_log (G_LOG_DOMAIN, \
258 G_LOG_LEVEL_CRITICAL, \
259 "file %s: line %d (%s): assertion `%s' failed", \
262 __PRETTY_FUNCTION__, \
267 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
270 g_log (G_LOG_DOMAIN, \
271 G_LOG_LEVEL_CRITICAL, \
272 "file %s: line %d (%s): assertion `%s' failed", \
275 __PRETTY_FUNCTION__, \
280 #define g_return_if_reached() G_STMT_START{ \
281 g_log (G_LOG_DOMAIN, \
282 G_LOG_LEVEL_CRITICAL, \
283 "file %s: line %d (%s): should not be reached", \
286 __PRETTY_FUNCTION__); \
289 #define g_return_val_if_reached(val) G_STMT_START{ \
290 g_log (G_LOG_DOMAIN, \
291 G_LOG_LEVEL_CRITICAL, \
292 "file %s: line %d (%s): should not be reached", \
295 __PRETTY_FUNCTION__); \
296 return (val); }G_STMT_END
298 #else /* !__GNUC__ */
300 #define g_return_if_fail(expr) G_STMT_START{ \
303 g_log (G_LOG_DOMAIN, \
304 G_LOG_LEVEL_CRITICAL, \
305 "file %s: line %d: assertion `%s' failed", \
312 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
315 g_log (G_LOG_DOMAIN, \
316 G_LOG_LEVEL_CRITICAL, \
317 "file %s: line %d: assertion `%s' failed", \
324 #define g_return_if_reached() G_STMT_START{ \
325 g_log (G_LOG_DOMAIN, \
326 G_LOG_LEVEL_CRITICAL, \
327 "file %s: line %d: should not be reached", \
332 #define g_return_val_if_reached(val) G_STMT_START{ \
333 g_log (G_LOG_DOMAIN, \
334 G_LOG_LEVEL_CRITICAL, \
335 "file %s: line %d: should not be reached", \
338 return (val); }G_STMT_END
340 #endif /* !__GNUC__ */
342 #endif /* !G_DISABLE_CHECKS */
346 #endif /* __G_MESSAGES_H__ */