ab76c4c443455eb003969f7630183e6813157a5f
[platform/upstream/glib.git] / glib / gmessages.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #ifndef __G_MESSAGES_H__
28 #define __G_MESSAGES_H__
29
30 #include <stdarg.h>
31 #include <glib/gtypes.h>
32 #include <glib/gmacros.h>
33
34 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
35  */
36 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
37 #pragma GCC system_header
38 #endif
39
40 G_BEGIN_DECLS
41
42 /* calculate a string size, guaranteed to fit format + args.
43  */
44 gsize   g_printf_string_upper_bound (const gchar* format,
45                                      va_list      args);
46
47 /* Log level shift offset for user defined
48  * log levels (0-7 are used by GLib).
49  */
50 #define G_LOG_LEVEL_USER_SHIFT  (8)
51
52 /* Glib log levels and flags.
53  */
54 typedef enum
55 {
56   /* log flags */
57   G_LOG_FLAG_RECURSION          = 1 << 0,
58   G_LOG_FLAG_FATAL              = 1 << 1,
59
60   /* GLib log levels */
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,
67
68   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
69 } GLogLevelFlags;
70
71 /* GLib log levels that are considered fatal by default */
72 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
73
74 typedef void            (*GLogFunc)             (const gchar   *log_domain,
75                                                  GLogLevelFlags log_level,
76                                                  const gchar   *message,
77                                                  gpointer       user_data);
78
79 /* Logging mechanism
80  */
81 guint           g_log_set_handler       (const gchar    *log_domain,
82                                          GLogLevelFlags  log_levels,
83                                          GLogFunc        log_func,
84                                          gpointer        user_data);
85 void            g_log_remove_handler    (const gchar    *log_domain,
86                                          guint           handler_id);
87 void            g_log_default_handler   (const gchar    *log_domain,
88                                          GLogLevelFlags  log_level,
89                                          const gchar    *message,
90                                          gpointer        unused_data);
91 GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
92                                            gpointer      user_data);
93 void            g_log                   (const gchar    *log_domain,
94                                          GLogLevelFlags  log_level,
95                                          const gchar    *format,
96                                          ...) G_GNUC_PRINTF (3, 4);
97 void            g_logv                  (const gchar    *log_domain,
98                                          GLogLevelFlags  log_level,
99                                          const gchar    *format,
100                                          va_list         args);
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);
104
105 /* internal */
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;
110
111
112 #ifndef G_LOG_DOMAIN
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,         \
117                                G_LOG_LEVEL_ERROR,    \
118                                __VA_ARGS__)
119 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
120                                G_LOG_LEVEL_MESSAGE,  \
121                                __VA_ARGS__)
122 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
123                                G_LOG_LEVEL_CRITICAL, \
124                                __VA_ARGS__)
125 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
126                                G_LOG_LEVEL_WARNING,  \
127                                __VA_ARGS__)
128 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
129                                G_LOG_LEVEL_DEBUG,    \
130                                __VA_ARGS__)
131 #elif defined(G_HAVE_GNUC_VARARGS)
132 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
133                                        G_LOG_LEVEL_ERROR,    \
134                                        format)
135 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
136                                        G_LOG_LEVEL_MESSAGE,  \
137                                        format)
138 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
139                                        G_LOG_LEVEL_CRITICAL, \
140                                        format)
141 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
142                                        G_LOG_LEVEL_WARNING,  \
143                                        format)
144 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
145                                        G_LOG_LEVEL_WARNING,  \
146                                        format)
147 #else   /* no varargs macros */
148 static void
149 g_error (const gchar *format,
150          ...)
151 {
152   va_list args;
153   va_start (args, format);
154   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
155   va_end (args);
156 }
157 static void
158 g_message (const gchar *format,
159            ...)
160 {
161   va_list args;
162   va_start (args, format);
163   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
164   va_end (args);
165 }
166 static void
167 g_critical (const gchar *format,
168             ...)
169 {
170   va_list args;
171   va_start (args, format);
172   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
173   va_end (args);
174 }
175 static void
176 g_warning (const gchar *format,
177            ...)
178 {
179   va_list args;
180   va_start (args, format);
181   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
182   va_end (args);
183 }
184 static void
185 g_debug (const gchar *format,
186          ...)
187 {
188   va_list args;
189   va_start (args, format);
190   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
191   va_end (args);
192 }
193 #endif  /* !__GNUC__ */
194
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);
202
203
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.
209  */
210
211 #ifdef G_DISABLE_ASSERT
212
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
215
216 #else /* !G_DISABLE_ASSERT */
217
218 #ifdef __GNUC__
219
220 #define g_assert(expr)                  G_STMT_START{           \
221      if G_LIKELY(expr) { } else                                 \
222         g_log (G_LOG_DOMAIN,                                    \
223               G_LOG_LEVEL_ERROR,                                \
224               "file %s: line %d (%s): assertion failed: (%s)",  \
225               __FILE__,                                         \
226               __LINE__,                                         \
227               __PRETTY_FUNCTION__,                              \
228               #expr);                   }G_STMT_END
229
230 #define g_assert_not_reached()          G_STMT_START{           \
231      g_log (G_LOG_DOMAIN,                                       \
232             G_LOG_LEVEL_ERROR,                                  \
233             "file %s: line %d (%s): should not be reached",     \
234             __FILE__,                                           \
235             __LINE__,                                           \
236             __PRETTY_FUNCTION__);       }G_STMT_END
237
238 #else /* !__GNUC__ */
239
240 #define g_assert(expr)                  G_STMT_START{           \
241      if (expr) { } else                                         \
242        g_log (G_LOG_DOMAIN,                                     \
243               G_LOG_LEVEL_ERROR,                                \
244               "file %s: line %d: assertion failed: (%s)",       \
245               __FILE__,                                         \
246               __LINE__,                                         \
247               #expr);                   }G_STMT_END
248
249 #define g_assert_not_reached()          G_STMT_START{   \
250      g_log (G_LOG_DOMAIN,                               \
251             G_LOG_LEVEL_ERROR,                          \
252             "file %s: line %d: should not be reached",  \
253             __FILE__,                                   \
254             __LINE__);          }G_STMT_END
255
256 #endif /* __GNUC__ */
257
258 #endif /* !G_DISABLE_ASSERT */
259
260
261 #ifdef G_DISABLE_CHECKS
262
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
267
268 #else /* !G_DISABLE_CHECKS */
269
270 #ifdef __GNUC__
271
272 #define g_return_if_fail(expr)          G_STMT_START{                   \
273      if G_LIKELY(expr) { } else                                         \
274        {                                                                \
275          g_log (G_LOG_DOMAIN,                                           \
276                 G_LOG_LEVEL_CRITICAL,                                   \
277                 "file %s: line %d (%s): assertion `%s' failed",         \
278                 __FILE__,                                               \
279                 __LINE__,                                               \
280                 __PRETTY_FUNCTION__,                                    \
281                 #expr);                                                 \
282          return;                                                        \
283        };                               }G_STMT_END
284
285 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
286      if G_LIKELY(expr) { } else                                         \
287        {                                                                \
288          g_log (G_LOG_DOMAIN,                                           \
289                 G_LOG_LEVEL_CRITICAL,                                   \
290                 "file %s: line %d (%s): assertion `%s' failed",         \
291                 __FILE__,                                               \
292                 __LINE__,                                               \
293                 __PRETTY_FUNCTION__,                                    \
294                 #expr);                                                 \
295          return (val);                                                  \
296        };                               }G_STMT_END
297
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",             \
302             __FILE__,                                                   \
303             __LINE__,                                                   \
304             __PRETTY_FUNCTION__);                                       \
305      return;                            }G_STMT_END
306
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",             \
311             __FILE__,                                                   \
312             __LINE__,                                                   \
313             __PRETTY_FUNCTION__);                                       \
314      return (val);                      }G_STMT_END
315
316 #else /* !__GNUC__ */
317
318 #define g_return_if_fail(expr)          G_STMT_START{           \
319      if (expr) { } else                                         \
320        {                                                        \
321          g_log (G_LOG_DOMAIN,                                   \
322                 G_LOG_LEVEL_CRITICAL,                           \
323                 "file %s: line %d: assertion `%s' failed",      \
324                 __FILE__,                                       \
325                 __LINE__,                                       \
326                 #expr);                                         \
327          return;                                                \
328        };                               }G_STMT_END
329
330 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
331      if (expr) { } else                                         \
332        {                                                        \
333          g_log (G_LOG_DOMAIN,                                   \
334                 G_LOG_LEVEL_CRITICAL,                           \
335                 "file %s: line %d: assertion `%s' failed",      \
336                 __FILE__,                                       \
337                 __LINE__,                                       \
338                 #expr);                                         \
339          return (val);                                          \
340        };                               }G_STMT_END
341
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",          \
346             __FILE__,                                           \
347             __LINE__);                                          \
348      return;                            }G_STMT_END
349
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",          \
354             __FILE__,                                           \
355             __LINE__);                                          \
356      return (val);                      }G_STMT_END
357
358 #endif /* !__GNUC__ */
359
360 #endif /* !G_DISABLE_CHECKS */
361
362 G_END_DECLS
363
364 #endif /* __G_MESSAGES_H__ */
365