Call setlocale initially
[glib.git] / glib / gmessages.h
blob9acaec63757efa063b5ed619536d1d6eaaa1960d
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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
31 #ifndef __G_MESSAGES_H__
32 #define __G_MESSAGES_H__
34 #include <stdarg.h>
35 #include <glib/gtypes.h>
36 #include <glib/gmacros.h>
38 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
40 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
41 #pragma GCC system_header
42 #endif
44 G_BEGIN_DECLS
46 /* calculate a string size, guaranteed to fit format + args.
48 gsize g_printf_string_upper_bound (const gchar* format,
49 va_list args);
51 /* Log level shift offset for user defined
52 * log levels (0-7 are used by GLib).
54 #define G_LOG_LEVEL_USER_SHIFT (8)
56 /* Glib log levels and flags.
58 typedef enum
60 /* log flags */
61 G_LOG_FLAG_RECURSION = 1 << 0,
62 G_LOG_FLAG_FATAL = 1 << 1,
64 /* GLib log levels */
65 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
66 G_LOG_LEVEL_CRITICAL = 1 << 3,
67 G_LOG_LEVEL_WARNING = 1 << 4,
68 G_LOG_LEVEL_MESSAGE = 1 << 5,
69 G_LOG_LEVEL_INFO = 1 << 6,
70 G_LOG_LEVEL_DEBUG = 1 << 7,
72 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
73 } GLogLevelFlags;
75 /* GLib log levels that are considered fatal by default */
76 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
78 typedef void (*GLogFunc) (const gchar *log_domain,
79 GLogLevelFlags log_level,
80 const gchar *message,
81 gpointer user_data);
83 /* Logging mechanism
85 guint g_log_set_handler (const gchar *log_domain,
86 GLogLevelFlags log_levels,
87 GLogFunc log_func,
88 gpointer user_data);
89 void g_log_remove_handler (const gchar *log_domain,
90 guint handler_id);
91 void g_log_default_handler (const gchar *log_domain,
92 GLogLevelFlags log_level,
93 const gchar *message,
94 gpointer unused_data);
95 GLogFunc g_log_set_default_handler (GLogFunc log_func,
96 gpointer user_data);
97 void g_log (const gchar *log_domain,
98 GLogLevelFlags log_level,
99 const gchar *format,
100 ...) G_GNUC_PRINTF (3, 4);
101 void g_logv (const gchar *log_domain,
102 GLogLevelFlags log_level,
103 const gchar *format,
104 va_list args);
105 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
106 GLogLevelFlags fatal_mask);
107 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
109 /* internal */
110 G_GNUC_INTERNAL void _g_log_fallback_handler (const gchar *log_domain,
111 GLogLevelFlags log_level,
112 const gchar *message,
113 gpointer unused_data);
115 /* Internal functions, used to implement the following macros */
116 void g_return_if_fail_warning (const char *log_domain,
117 const char *pretty_function,
118 const char *expression);
119 void g_warn_message (const char *domain,
120 const char *file,
121 int line,
122 const char *func,
123 const char *warnexpr);
124 #ifndef G_DISABLE_DEPRECATED
125 void g_assert_warning (const char *log_domain,
126 const char *file,
127 const int line,
128 const char *pretty_function,
129 const char *expression) G_GNUC_NORETURN;
130 #endif /* !G_DISABLE_DEPRECATED */
133 #ifndef G_LOG_DOMAIN
134 #define G_LOG_DOMAIN ((gchar*) 0)
135 #endif /* G_LOG_DOMAIN */
136 #ifdef G_HAVE_ISO_VARARGS
137 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
138 * Put space before ending semicolon to avoid C++ build warnings.
140 #define g_error(...) G_STMT_START { \
141 g_log (G_LOG_DOMAIN, \
142 G_LOG_LEVEL_ERROR, \
143 __VA_ARGS__); \
144 for (;;) ; \
145 } G_STMT_END
147 #define g_message(...) g_log (G_LOG_DOMAIN, \
148 G_LOG_LEVEL_MESSAGE, \
149 __VA_ARGS__)
150 #define g_critical(...) g_log (G_LOG_DOMAIN, \
151 G_LOG_LEVEL_CRITICAL, \
152 __VA_ARGS__)
153 #define g_warning(...) g_log (G_LOG_DOMAIN, \
154 G_LOG_LEVEL_WARNING, \
155 __VA_ARGS__)
156 #define g_debug(...) g_log (G_LOG_DOMAIN, \
157 G_LOG_LEVEL_DEBUG, \
158 __VA_ARGS__)
159 #elif defined(G_HAVE_GNUC_VARARGS)
160 #define g_error(format...) G_STMT_START { \
161 g_log (G_LOG_DOMAIN, \
162 G_LOG_LEVEL_ERROR, \
163 format); \
164 for (;;) ; \
165 } G_STMT_END
167 #define g_message(format...) g_log (G_LOG_DOMAIN, \
168 G_LOG_LEVEL_MESSAGE, \
169 format)
170 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
171 G_LOG_LEVEL_CRITICAL, \
172 format)
173 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
174 G_LOG_LEVEL_WARNING, \
175 format)
176 #define g_debug(format...) g_log (G_LOG_DOMAIN, \
177 G_LOG_LEVEL_DEBUG, \
178 format)
179 #else /* no varargs macros */
180 static void
181 g_error (const gchar *format,
182 ...)
184 va_list args;
185 va_start (args, format);
186 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
187 va_end (args);
189 for(;;) ;
191 static void
192 g_message (const gchar *format,
193 ...)
195 va_list args;
196 va_start (args, format);
197 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
198 va_end (args);
200 static void
201 g_critical (const gchar *format,
202 ...)
204 va_list args;
205 va_start (args, format);
206 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
207 va_end (args);
209 static void
210 g_warning (const gchar *format,
211 ...)
213 va_list args;
214 va_start (args, format);
215 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
216 va_end (args);
218 static void
219 g_debug (const gchar *format,
220 ...)
222 va_list args;
223 va_start (args, format);
224 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
225 va_end (args);
227 #endif /* !__GNUC__ */
229 typedef void (*GPrintFunc) (const gchar *string);
230 void g_print (const gchar *format,
231 ...) G_GNUC_PRINTF (1, 2);
232 GPrintFunc g_set_print_handler (GPrintFunc func);
233 void g_printerr (const gchar *format,
234 ...) G_GNUC_PRINTF (1, 2);
235 GPrintFunc g_set_printerr_handler (GPrintFunc func);
238 /* Provide macros for graceful error handling.
239 * The "return" macros will return from the current function.
240 * Two different definitions are given for the macros in
241 * order to support gcc's __PRETTY_FUNCTION__ capability.
244 #define g_warn_if_reached() do { g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
245 #define g_warn_if_fail(expr) do { if G_LIKELY (expr) ; else \
246 g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); } while (0)
248 #ifdef G_DISABLE_CHECKS
250 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
251 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
252 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
253 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
255 #else /* !G_DISABLE_CHECKS */
257 #ifdef __GNUC__
259 #define g_return_if_fail(expr) G_STMT_START{ \
260 if G_LIKELY(expr) { } else \
262 g_return_if_fail_warning (G_LOG_DOMAIN, \
263 __PRETTY_FUNCTION__, \
264 #expr); \
265 return; \
266 }; }G_STMT_END
268 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
269 if G_LIKELY(expr) { } else \
271 g_return_if_fail_warning (G_LOG_DOMAIN, \
272 __PRETTY_FUNCTION__, \
273 #expr); \
274 return (val); \
275 }; }G_STMT_END
277 #define g_return_if_reached() G_STMT_START{ \
278 g_log (G_LOG_DOMAIN, \
279 G_LOG_LEVEL_CRITICAL, \
280 "file %s: line %d (%s): should not be reached", \
281 __FILE__, \
282 __LINE__, \
283 __PRETTY_FUNCTION__); \
284 return; }G_STMT_END
286 #define g_return_val_if_reached(val) G_STMT_START{ \
287 g_log (G_LOG_DOMAIN, \
288 G_LOG_LEVEL_CRITICAL, \
289 "file %s: line %d (%s): should not be reached", \
290 __FILE__, \
291 __LINE__, \
292 __PRETTY_FUNCTION__); \
293 return (val); }G_STMT_END
295 #else /* !__GNUC__ */
297 #define g_return_if_fail(expr) G_STMT_START{ \
298 if (expr) { } else \
300 g_log (G_LOG_DOMAIN, \
301 G_LOG_LEVEL_CRITICAL, \
302 "file %s: line %d: assertion `%s' failed", \
303 __FILE__, \
304 __LINE__, \
305 #expr); \
306 return; \
307 }; }G_STMT_END
309 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
310 if (expr) { } else \
312 g_log (G_LOG_DOMAIN, \
313 G_LOG_LEVEL_CRITICAL, \
314 "file %s: line %d: assertion `%s' failed", \
315 __FILE__, \
316 __LINE__, \
317 #expr); \
318 return (val); \
319 }; }G_STMT_END
321 #define g_return_if_reached() G_STMT_START{ \
322 g_log (G_LOG_DOMAIN, \
323 G_LOG_LEVEL_CRITICAL, \
324 "file %s: line %d: should not be reached", \
325 __FILE__, \
326 __LINE__); \
327 return; }G_STMT_END
329 #define g_return_val_if_reached(val) G_STMT_START{ \
330 g_log (G_LOG_DOMAIN, \
331 G_LOG_LEVEL_CRITICAL, \
332 "file %s: line %d: should not be reached", \
333 __FILE__, \
334 __LINE__); \
335 return (val); }G_STMT_END
337 #endif /* !__GNUC__ */
339 #endif /* !G_DISABLE_CHECKS */
341 G_END_DECLS
343 #endif /* __G_MESSAGES_H__ */