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 void _g_log_fallback_handler (const gchar *log_domain,
107 GLogLevelFlags log_level,
108 const gchar *message,
109 gpointer unused_data) G_GNUC_INTERNAL;
113 #define G_LOG_DOMAIN ((gchar*) 0)
114 #endif /* G_LOG_DOMAIN */
115 #ifdef G_HAVE_ISO_VARARGS
116 #define g_error(...) g_log (G_LOG_DOMAIN, \
119 #define g_message(...) g_log (G_LOG_DOMAIN, \
120 G_LOG_LEVEL_MESSAGE, \
122 #define g_critical(...) g_log (G_LOG_DOMAIN, \
123 G_LOG_LEVEL_CRITICAL, \
125 #define g_warning(...) g_log (G_LOG_DOMAIN, \
126 G_LOG_LEVEL_WARNING, \
128 #define g_debug(...) g_log (G_LOG_DOMAIN, \
131 #elif defined(G_HAVE_GNUC_VARARGS)
132 #define g_error(format...) g_log (G_LOG_DOMAIN, \
135 #define g_message(format...) g_log (G_LOG_DOMAIN, \
136 G_LOG_LEVEL_MESSAGE, \
138 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
139 G_LOG_LEVEL_CRITICAL, \
141 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
142 G_LOG_LEVEL_WARNING, \
144 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
145 G_LOG_LEVEL_WARNING, \
147 #else /* no varargs macros */
149 g_error (const gchar *format,
153 va_start (args, format);
154 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
158 g_message (const gchar *format,
162 va_start (args, format);
163 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
167 g_critical (const gchar *format,
171 va_start (args, format);
172 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
176 g_warning (const gchar *format,
180 va_start (args, format);
181 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
185 g_debug (const gchar *format,
189 va_start (args, format);
190 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
193 #endif /* !__GNUC__ */
195 typedef void (*GPrintFunc) (const gchar *string);
196 void g_print (const gchar *format,
197 ...) G_GNUC_PRINTF (1, 2);
198 GPrintFunc g_set_print_handler (GPrintFunc func);
199 void g_printerr (const gchar *format,
200 ...) G_GNUC_PRINTF (1, 2);
201 GPrintFunc g_set_printerr_handler (GPrintFunc func);
204 /* Provide macros for error handling. The "assert" macros will
205 * exit on failure. The "return" macros will exit the current
206 * function. Two different definitions are given for the macros
207 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
208 * __PRETTY_FUNCTION__ capability.
211 #ifdef G_DISABLE_ASSERT
213 #define g_assert(expr) G_STMT_START{ (void)0; }G_STMT_END
214 #define g_assert_not_reached() G_STMT_START{ (void)0; }G_STMT_END
216 #else /* !G_DISABLE_ASSERT */
220 #define g_assert(expr) G_STMT_START{ \
221 if G_LIKELY(expr) { } else \
222 g_log (G_LOG_DOMAIN, \
224 "file %s: line %d (%s): assertion failed: (%s)", \
227 __PRETTY_FUNCTION__, \
230 #define g_assert_not_reached() G_STMT_START{ \
231 g_log (G_LOG_DOMAIN, \
233 "file %s: line %d (%s): should not be reached", \
236 __PRETTY_FUNCTION__); }G_STMT_END
238 #else /* !__GNUC__ */
240 #define g_assert(expr) G_STMT_START{ \
242 g_log (G_LOG_DOMAIN, \
244 "file %s: line %d: assertion failed: (%s)", \
249 #define g_assert_not_reached() G_STMT_START{ \
250 g_log (G_LOG_DOMAIN, \
252 "file %s: line %d: should not be reached", \
254 __LINE__); }G_STMT_END
256 #endif /* __GNUC__ */
258 #endif /* !G_DISABLE_ASSERT */
261 #ifdef G_DISABLE_CHECKS
263 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
264 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
265 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
266 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
268 #else /* !G_DISABLE_CHECKS */
272 #define g_return_if_fail(expr) G_STMT_START{ \
273 if G_LIKELY(expr) { } else \
275 g_log (G_LOG_DOMAIN, \
276 G_LOG_LEVEL_CRITICAL, \
277 "file %s: line %d (%s): assertion `%s' failed", \
280 __PRETTY_FUNCTION__, \
285 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
286 if G_LIKELY(expr) { } else \
288 g_log (G_LOG_DOMAIN, \
289 G_LOG_LEVEL_CRITICAL, \
290 "file %s: line %d (%s): assertion `%s' failed", \
293 __PRETTY_FUNCTION__, \
298 #define g_return_if_reached() G_STMT_START{ \
299 g_log (G_LOG_DOMAIN, \
300 G_LOG_LEVEL_CRITICAL, \
301 "file %s: line %d (%s): should not be reached", \
304 __PRETTY_FUNCTION__); \
307 #define g_return_val_if_reached(val) G_STMT_START{ \
308 g_log (G_LOG_DOMAIN, \
309 G_LOG_LEVEL_CRITICAL, \
310 "file %s: line %d (%s): should not be reached", \
313 __PRETTY_FUNCTION__); \
314 return (val); }G_STMT_END
316 #else /* !__GNUC__ */
318 #define g_return_if_fail(expr) G_STMT_START{ \
321 g_log (G_LOG_DOMAIN, \
322 G_LOG_LEVEL_CRITICAL, \
323 "file %s: line %d: assertion `%s' failed", \
330 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
333 g_log (G_LOG_DOMAIN, \
334 G_LOG_LEVEL_CRITICAL, \
335 "file %s: line %d: assertion `%s' failed", \
342 #define g_return_if_reached() G_STMT_START{ \
343 g_log (G_LOG_DOMAIN, \
344 G_LOG_LEVEL_CRITICAL, \
345 "file %s: line %d: should not be reached", \
350 #define g_return_val_if_reached(val) G_STMT_START{ \
351 g_log (G_LOG_DOMAIN, \
352 G_LOG_LEVEL_CRITICAL, \
353 "file %s: line %d: should not be reached", \
356 return (val); }G_STMT_END
358 #endif /* !__GNUC__ */
360 #endif /* !G_DISABLE_CHECKS */
364 #endif /* __G_MESSAGES_H__ */