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>
35 /* calculate a string size, guarranteed to fit format + args.
37 gsize g_printf_string_upper_bound (const gchar* format,
40 /* Log level shift offset for user defined
41 * log levels (0-7 are used by GLib).
43 #define G_LOG_LEVEL_USER_SHIFT (8)
45 /* Glib log levels and flags.
50 G_LOG_FLAG_RECURSION = 1 << 0,
51 G_LOG_FLAG_FATAL = 1 << 1,
54 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
55 G_LOG_LEVEL_CRITICAL = 1 << 3,
56 G_LOG_LEVEL_WARNING = 1 << 4,
57 G_LOG_LEVEL_MESSAGE = 1 << 5,
58 G_LOG_LEVEL_INFO = 1 << 6,
59 G_LOG_LEVEL_DEBUG = 1 << 7,
61 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
64 /* GLib log levels that are considered fatal by default */
65 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
67 typedef void (*GLogFunc) (const gchar *log_domain,
68 GLogLevelFlags log_level,
74 GLIB_VAR const gchar *g_log_domain_glib;
75 guint g_log_set_handler (const gchar *log_domain,
76 GLogLevelFlags log_levels,
79 void g_log_remove_handler (const gchar *log_domain,
81 void g_log_default_handler (const gchar *log_domain,
82 GLogLevelFlags log_level,
84 gpointer unused_data);
85 void g_log (const gchar *log_domain,
86 GLogLevelFlags log_level,
88 ...) G_GNUC_PRINTF (3, 4);
89 void g_logv (const gchar *log_domain,
90 GLogLevelFlags log_level,
93 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
94 GLogLevelFlags fatal_mask);
95 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
98 #define G_LOG_DOMAIN ((gchar*) 0)
99 #endif /* G_LOG_DOMAIN */
100 #ifdef G_HAVE_ISO_VARARGS
101 #define g_error(...) g_log (G_LOG_DOMAIN, \
104 #define g_message(...) g_log (G_LOG_DOMAIN, \
105 G_LOG_LEVEL_MESSAGE, \
107 #define g_critical(...) g_log (G_LOG_DOMAIN, \
108 G_LOG_LEVEL_CRITICAL, \
110 #define g_warning(...) g_log (G_LOG_DOMAIN, \
111 G_LOG_LEVEL_WARNING, \
113 #elif defined(G_HAVE_GNUC_VARARGS)
114 #define g_error(format...) g_log (G_LOG_DOMAIN, \
117 #define g_message(format...) g_log (G_LOG_DOMAIN, \
118 G_LOG_LEVEL_MESSAGE, \
120 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
121 G_LOG_LEVEL_CRITICAL, \
123 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
124 G_LOG_LEVEL_WARNING, \
126 #else /* no varargs macros */
128 g_error (const gchar *format,
132 va_start (args, format);
133 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
137 g_message (const gchar *format,
141 va_start (args, format);
142 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
146 g_critical (const gchar *format,
150 va_start (args, format);
151 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
155 g_warning (const gchar *format,
159 va_start (args, format);
160 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
163 #endif /* !__GNUC__ */
165 typedef void (*GPrintFunc) (const gchar *string);
166 void g_print (const gchar *format,
167 ...) G_GNUC_PRINTF (1, 2);
168 GPrintFunc g_set_print_handler (GPrintFunc func);
169 void g_printerr (const gchar *format,
170 ...) G_GNUC_PRINTF (1, 2);
171 GPrintFunc g_set_printerr_handler (GPrintFunc func);
174 /* Provide macros for error handling. The "assert" macros will
175 * exit on failure. The "return" macros will exit the current
176 * function. Two different definitions are given for the macros
177 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
178 * __PRETTY_FUNCTION__ capability.
181 #ifdef G_DISABLE_ASSERT
183 #define g_assert(expr) G_STMT_START{ }G_STMT_END
184 #define g_assert_not_reached() G_STMT_START{ }G_STMT_END
186 #else /* !G_DISABLE_ASSERT */
190 #define g_assert(expr) G_STMT_START{ \
192 g_log (G_LOG_DOMAIN, \
194 "file %s: line %d (%s): assertion failed: (%s)", \
197 __PRETTY_FUNCTION__, \
200 #define g_assert_not_reached() G_STMT_START{ \
201 g_log (G_LOG_DOMAIN, \
203 "file %s: line %d (%s): should not be reached", \
206 __PRETTY_FUNCTION__); }G_STMT_END
208 #else /* !__GNUC__ */
210 #define g_assert(expr) G_STMT_START{ \
212 g_log (G_LOG_DOMAIN, \
214 "file %s: line %d: assertion failed: (%s)", \
219 #define g_assert_not_reached() G_STMT_START{ \
220 g_log (G_LOG_DOMAIN, \
222 "file %s: line %d: should not be reached", \
224 __LINE__); }G_STMT_END
226 #endif /* __GNUC__ */
228 #endif /* !G_DISABLE_ASSERT */
231 #ifdef G_DISABLE_CHECKS
233 #define g_return_if_fail(expr) G_STMT_START{ }G_STMT_END
234 #define g_return_val_if_fail(expr,val) G_STMT_START{ }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{ \
245 g_log (G_LOG_DOMAIN, \
246 G_LOG_LEVEL_CRITICAL, \
247 "file %s: line %d (%s): assertion `%s' failed", \
250 __PRETTY_FUNCTION__, \
255 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
258 g_log (G_LOG_DOMAIN, \
259 G_LOG_LEVEL_CRITICAL, \
260 "file %s: line %d (%s): assertion `%s' failed", \
263 __PRETTY_FUNCTION__, \
268 #define g_return_if_reached() G_STMT_START{ \
269 g_log (G_LOG_DOMAIN, \
270 G_LOG_LEVEL_CRITICAL, \
271 "file %s: line %d (%s): should not be reached", \
274 __PRETTY_FUNCTION__); \
277 #define g_return_val_if_reached(val) G_STMT_START{ \
278 g_log (G_LOG_DOMAIN, \
279 G_LOG_LEVEL_CRITICAL, \
280 "file %s: line %d (%s): should not be reached", \
283 __PRETTY_FUNCTION__); \
284 return (val); }G_STMT_END
286 #else /* !__GNUC__ */
288 #define g_return_if_fail(expr) G_STMT_START{ \
291 g_log (G_LOG_DOMAIN, \
292 G_LOG_LEVEL_CRITICAL, \
293 "file %s: line %d: assertion `%s' failed", \
300 #define g_return_val_if_fail(expr, val) 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_if_reached() G_STMT_START{ \
313 g_log (G_LOG_DOMAIN, \
314 G_LOG_LEVEL_CRITICAL, \
315 "file %s: line %d: should not be reached", \
320 #define g_return_val_if_reached(val) G_STMT_START{ \
321 g_log (G_LOG_DOMAIN, \
322 G_LOG_LEVEL_CRITICAL, \
323 "file %s: line %d: should not be reached", \
326 return (val); }G_STMT_END
328 #endif /* !__GNUC__ */
330 #endif /* !G_DISABLE_CHECKS */
334 #endif /* __G_MESSAGES_H__ */