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 guint g_log_set_handler (const gchar *log_domain,
75 GLogLevelFlags log_levels,
78 void g_log_remove_handler (const gchar *log_domain,
80 void g_log_default_handler (const gchar *log_domain,
81 GLogLevelFlags log_level,
83 gpointer unused_data);
84 void g_log (const gchar *log_domain,
85 GLogLevelFlags log_level,
87 ...) G_GNUC_PRINTF (3, 4);
88 void g_logv (const gchar *log_domain,
89 GLogLevelFlags log_level,
92 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
93 GLogLevelFlags fatal_mask);
94 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
97 #define G_LOG_DOMAIN ((gchar*) 0)
98 #endif /* G_LOG_DOMAIN */
99 #ifdef G_HAVE_ISO_VARARGS
100 #define g_error(...) g_log (G_LOG_DOMAIN, \
103 #define g_message(...) g_log (G_LOG_DOMAIN, \
104 G_LOG_LEVEL_MESSAGE, \
106 #define g_critical(...) g_log (G_LOG_DOMAIN, \
107 G_LOG_LEVEL_CRITICAL, \
109 #define g_warning(...) g_log (G_LOG_DOMAIN, \
110 G_LOG_LEVEL_WARNING, \
112 #elif defined(G_HAVE_GNUC_VARARGS)
113 #define g_error(format...) g_log (G_LOG_DOMAIN, \
116 #define g_message(format...) g_log (G_LOG_DOMAIN, \
117 G_LOG_LEVEL_MESSAGE, \
119 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
120 G_LOG_LEVEL_CRITICAL, \
122 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
123 G_LOG_LEVEL_WARNING, \
125 #else /* no varargs macros */
127 g_error (const gchar *format,
131 va_start (args, format);
132 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
136 g_message (const gchar *format,
140 va_start (args, format);
141 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
145 g_critical (const gchar *format,
149 va_start (args, format);
150 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
154 g_warning (const gchar *format,
158 va_start (args, format);
159 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
162 #endif /* !__GNUC__ */
164 typedef void (*GPrintFunc) (const gchar *string);
165 void g_print (const gchar *format,
166 ...) G_GNUC_PRINTF (1, 2);
167 GPrintFunc g_set_print_handler (GPrintFunc func);
168 void g_printerr (const gchar *format,
169 ...) G_GNUC_PRINTF (1, 2);
170 GPrintFunc g_set_printerr_handler (GPrintFunc func);
173 /* Provide macros for error handling. The "assert" macros will
174 * exit on failure. The "return" macros will exit the current
175 * function. Two different definitions are given for the macros
176 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
177 * __PRETTY_FUNCTION__ capability.
180 #ifdef G_DISABLE_ASSERT
182 #define g_assert(expr) G_STMT_START{ }G_STMT_END
183 #define g_assert_not_reached() G_STMT_START{ }G_STMT_END
185 #else /* !G_DISABLE_ASSERT */
189 #define g_assert(expr) G_STMT_START{ \
191 g_log (G_LOG_DOMAIN, \
193 "file %s: line %d (%s): assertion failed: (%s)", \
196 __PRETTY_FUNCTION__, \
199 #define g_assert_not_reached() G_STMT_START{ \
200 g_log (G_LOG_DOMAIN, \
202 "file %s: line %d (%s): should not be reached", \
205 __PRETTY_FUNCTION__); }G_STMT_END
207 #else /* !__GNUC__ */
209 #define g_assert(expr) G_STMT_START{ \
211 g_log (G_LOG_DOMAIN, \
213 "file %s: line %d: assertion failed: (%s)", \
218 #define g_assert_not_reached() G_STMT_START{ \
219 g_log (G_LOG_DOMAIN, \
221 "file %s: line %d: should not be reached", \
223 __LINE__); }G_STMT_END
225 #endif /* __GNUC__ */
227 #endif /* !G_DISABLE_ASSERT */
230 #ifdef G_DISABLE_CHECKS
232 #define g_return_if_fail(expr) G_STMT_START{ }G_STMT_END
233 #define g_return_val_if_fail(expr,val) G_STMT_START{ }G_STMT_END
234 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
235 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
237 #else /* !G_DISABLE_CHECKS */
241 #define g_return_if_fail(expr) G_STMT_START{ \
244 g_log (G_LOG_DOMAIN, \
245 G_LOG_LEVEL_CRITICAL, \
246 "file %s: line %d (%s): assertion `%s' failed", \
249 __PRETTY_FUNCTION__, \
254 #define g_return_val_if_fail(expr,val) 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_if_reached() G_STMT_START{ \
268 g_log (G_LOG_DOMAIN, \
269 G_LOG_LEVEL_CRITICAL, \
270 "file %s: line %d (%s): should not be reached", \
273 __PRETTY_FUNCTION__); \
276 #define g_return_val_if_reached(val) G_STMT_START{ \
277 g_log (G_LOG_DOMAIN, \
278 G_LOG_LEVEL_CRITICAL, \
279 "file %s: line %d (%s): should not be reached", \
282 __PRETTY_FUNCTION__); \
283 return (val); }G_STMT_END
285 #else /* !__GNUC__ */
287 #define g_return_if_fail(expr) G_STMT_START{ \
290 g_log (G_LOG_DOMAIN, \
291 G_LOG_LEVEL_CRITICAL, \
292 "file %s: line %d: assertion `%s' failed", \
299 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
302 g_log (G_LOG_DOMAIN, \
303 G_LOG_LEVEL_CRITICAL, \
304 "file %s: line %d: assertion `%s' failed", \
311 #define g_return_if_reached() G_STMT_START{ \
312 g_log (G_LOG_DOMAIN, \
313 G_LOG_LEVEL_CRITICAL, \
314 "file %s: line %d: should not be reached", \
319 #define g_return_val_if_reached(val) G_STMT_START{ \
320 g_log (G_LOG_DOMAIN, \
321 G_LOG_LEVEL_CRITICAL, \
322 "file %s: line %d: should not be reached", \
325 return (val); }G_STMT_END
327 #endif /* !__GNUC__ */
329 #endif /* !G_DISABLE_CHECKS */
333 #endif /* __G_MESSAGES_H__ */