eb2ca8f9a6a7d102909dca305604989ea6751893
[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 G_GNUC_INTERNAL void    _g_log_fallback_handler (const gchar   *log_domain,
107                                                  GLogLevelFlags log_level,
108                                                  const gchar   *message,
109                                                  gpointer       unused_data);
110
111 /* Internal functions, used to implement the following macros */
112 void g_return_if_fail_warning (const char *log_domain,
113                                const char *pretty_function,
114                                const char *expression);
115 void g_warn_message           (const char     *domain,
116                                const char     *file,
117                                int             line,
118                                const char     *func,
119                                const char     *warnexpr);
120 #ifndef G_DISABLE_DEPRECATED
121 void g_assert_warning         (const char *log_domain,
122                                const char *file,
123                                const int   line,
124                                const char *pretty_function,
125                                const char *expression) G_GNUC_NORETURN;
126 #endif /* !G_DISABLE_DEPRECATED */
127
128
129 #ifndef G_LOG_DOMAIN
130 #define G_LOG_DOMAIN    ((gchar*) 0)
131 #endif  /* G_LOG_DOMAIN */
132 #ifdef G_HAVE_ISO_VARARGS
133 /* for(;;); so that GCC knows that control doesn't go past g_error() */
134 #define g_error(...)  G_STMT_START {                 \
135                         g_log (G_LOG_DOMAIN,         \
136                                G_LOG_LEVEL_ERROR,    \
137                                __VA_ARGS__);         \
138                         for (;;);                    \
139                       } G_STMT_END
140                         
141 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
142                                G_LOG_LEVEL_MESSAGE,  \
143                                __VA_ARGS__)
144 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
145                                G_LOG_LEVEL_CRITICAL, \
146                                __VA_ARGS__)
147 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
148                                G_LOG_LEVEL_WARNING,  \
149                                __VA_ARGS__)
150 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
151                                G_LOG_LEVEL_DEBUG,    \
152                                __VA_ARGS__)
153 #elif defined(G_HAVE_GNUC_VARARGS)
154 #define g_error(format...)    G_STMT_START {                 \
155                                 g_log (G_LOG_DOMAIN,         \
156                                        G_LOG_LEVEL_ERROR,    \
157                                        format);              \
158                                 for (;;);                    \
159                               } G_STMT_END
160                               
161 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
162                                        G_LOG_LEVEL_MESSAGE,  \
163                                        format)
164 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
165                                        G_LOG_LEVEL_CRITICAL, \
166                                        format)
167 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
168                                        G_LOG_LEVEL_WARNING,  \
169                                        format)
170 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
171                                        G_LOG_LEVEL_DEBUG,    \
172                                        format)
173 #else   /* no varargs macros */
174 static void
175 g_error (const gchar *format,
176          ...)
177 {
178   va_list args;
179   va_start (args, format);
180   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
181   va_end (args);
182
183   for(;;);
184 }
185 static void
186 g_message (const gchar *format,
187            ...)
188 {
189   va_list args;
190   va_start (args, format);
191   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
192   va_end (args);
193 }
194 static void
195 g_critical (const gchar *format,
196             ...)
197 {
198   va_list args;
199   va_start (args, format);
200   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
201   va_end (args);
202 }
203 static void
204 g_warning (const gchar *format,
205            ...)
206 {
207   va_list args;
208   va_start (args, format);
209   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
210   va_end (args);
211 }
212 static void
213 g_debug (const gchar *format,
214          ...)
215 {
216   va_list args;
217   va_start (args, format);
218   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
219   va_end (args);
220 }
221 #endif  /* !__GNUC__ */
222
223 typedef void    (*GPrintFunc)           (const gchar    *string);
224 void            g_print                 (const gchar    *format,
225                                          ...) G_GNUC_PRINTF (1, 2);
226 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
227 void            g_printerr              (const gchar    *format,
228                                          ...) G_GNUC_PRINTF (1, 2);
229 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
230
231
232 /* Provide macros for graceful error handling.
233  * The "return" macros will return from the current function.
234  * Two different definitions are given for the macros in
235  * order to support gcc's __PRETTY_FUNCTION__ capability.
236  */
237
238 #define g_warn_if_reached()     do { g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
239 #define g_warn_if_fail(expr)    do { if G_LIKELY (expr) ; else \
240                                        g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); } while (0)
241
242 #ifdef G_DISABLE_CHECKS
243
244 #define g_return_if_fail(expr)                  G_STMT_START{ (void)0; }G_STMT_END
245 #define g_return_val_if_fail(expr,val)          G_STMT_START{ (void)0; }G_STMT_END
246 #define g_return_if_reached()                   G_STMT_START{ return; }G_STMT_END
247 #define g_return_val_if_reached(val)            G_STMT_START{ return (val); }G_STMT_END
248
249 #else /* !G_DISABLE_CHECKS */
250
251 #ifdef __GNUC__
252
253 #define g_return_if_fail(expr)          G_STMT_START{                   \
254      if G_LIKELY(expr) { } else                                         \
255        {                                                                \
256          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
257                                    __PRETTY_FUNCTION__,                 \
258                                    #expr);                              \
259          return;                                                        \
260        };                               }G_STMT_END
261
262 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
263      if G_LIKELY(expr) { } else                                         \
264        {                                                                \
265          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
266                                    __PRETTY_FUNCTION__,                 \
267                                    #expr);                              \
268          return (val);                                                  \
269        };                               }G_STMT_END
270
271 #define g_return_if_reached()           G_STMT_START{                   \
272      g_log (G_LOG_DOMAIN,                                               \
273             G_LOG_LEVEL_CRITICAL,                                       \
274             "file %s: line %d (%s): should not be reached",             \
275             __FILE__,                                                   \
276             __LINE__,                                                   \
277             __PRETTY_FUNCTION__);                                       \
278      return;                            }G_STMT_END
279
280 #define g_return_val_if_reached(val)    G_STMT_START{                   \
281      g_log (G_LOG_DOMAIN,                                               \
282             G_LOG_LEVEL_CRITICAL,                                       \
283             "file %s: line %d (%s): should not be reached",             \
284             __FILE__,                                                   \
285             __LINE__,                                                   \
286             __PRETTY_FUNCTION__);                                       \
287      return (val);                      }G_STMT_END
288
289 #else /* !__GNUC__ */
290
291 #define g_return_if_fail(expr)          G_STMT_START{           \
292      if (expr) { } else                                         \
293        {                                                        \
294          g_log (G_LOG_DOMAIN,                                   \
295                 G_LOG_LEVEL_CRITICAL,                           \
296                 "file %s: line %d: assertion `%s' failed",      \
297                 __FILE__,                                       \
298                 __LINE__,                                       \
299                 #expr);                                         \
300          return;                                                \
301        };                               }G_STMT_END
302
303 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
304      if (expr) { } else                                         \
305        {                                                        \
306          g_log (G_LOG_DOMAIN,                                   \
307                 G_LOG_LEVEL_CRITICAL,                           \
308                 "file %s: line %d: assertion `%s' failed",      \
309                 __FILE__,                                       \
310                 __LINE__,                                       \
311                 #expr);                                         \
312          return (val);                                          \
313        };                               }G_STMT_END
314
315 #define g_return_if_reached()           G_STMT_START{           \
316      g_log (G_LOG_DOMAIN,                                       \
317             G_LOG_LEVEL_CRITICAL,                               \
318             "file %s: line %d: should not be reached",          \
319             __FILE__,                                           \
320             __LINE__);                                          \
321      return;                            }G_STMT_END
322
323 #define g_return_val_if_reached(val)    G_STMT_START{           \
324      g_log (G_LOG_DOMAIN,                                       \
325             G_LOG_LEVEL_CRITICAL,                               \
326             "file %s: line %d: should not be reached",          \
327             __FILE__,                                           \
328             __LINE__);                                          \
329      return (val);                      }G_STMT_END
330
331 #endif /* !__GNUC__ */
332
333 #endif /* !G_DISABLE_CHECKS */
334
335 G_END_DECLS
336
337 #endif /* __G_MESSAGES_H__ */
338