Add a G_GNUC_INTERNAL macro to mark function declarations as internal and
[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 void            g_log                   (const gchar    *log_domain,
92                                          GLogLevelFlags  log_level,
93                                          const gchar    *format,
94                                          ...) G_GNUC_PRINTF (3, 4);
95 void            g_logv                  (const gchar    *log_domain,
96                                          GLogLevelFlags  log_level,
97                                          const gchar    *format,
98                                          va_list         args);
99 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
100                                          GLogLevelFlags  fatal_mask);
101 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
102
103 /* internal */
104 void    _g_log_fallback_handler (const gchar   *log_domain,
105                                  GLogLevelFlags log_level,
106                                  const gchar   *message,
107                                  gpointer       unused_data) G_GNUC_INTERNAL;
108
109
110 #ifndef G_LOG_DOMAIN
111 #define G_LOG_DOMAIN    ((gchar*) 0)
112 #endif  /* G_LOG_DOMAIN */
113 #ifdef G_HAVE_ISO_VARARGS
114 #define g_error(...)    g_log (G_LOG_DOMAIN,         \
115                                G_LOG_LEVEL_ERROR,    \
116                                __VA_ARGS__)
117 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
118                                G_LOG_LEVEL_MESSAGE,  \
119                                __VA_ARGS__)
120 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
121                                G_LOG_LEVEL_CRITICAL, \
122                                __VA_ARGS__)
123 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
124                                G_LOG_LEVEL_WARNING,  \
125                                __VA_ARGS__)
126 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
127                                G_LOG_LEVEL_DEBUG,    \
128                                __VA_ARGS__)
129 #elif defined(G_HAVE_GNUC_VARARGS)
130 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
131                                        G_LOG_LEVEL_ERROR,    \
132                                        format)
133 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
134                                        G_LOG_LEVEL_MESSAGE,  \
135                                        format)
136 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
137                                        G_LOG_LEVEL_CRITICAL, \
138                                        format)
139 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
140                                        G_LOG_LEVEL_WARNING,  \
141                                        format)
142 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
143                                        G_LOG_LEVEL_WARNING,  \
144                                        format)
145 #else   /* no varargs macros */
146 static void
147 g_error (const gchar *format,
148          ...)
149 {
150   va_list args;
151   va_start (args, format);
152   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
153   va_end (args);
154 }
155 static void
156 g_message (const gchar *format,
157            ...)
158 {
159   va_list args;
160   va_start (args, format);
161   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
162   va_end (args);
163 }
164 static void
165 g_critical (const gchar *format,
166             ...)
167 {
168   va_list args;
169   va_start (args, format);
170   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
171   va_end (args);
172 }
173 static void
174 g_warning (const gchar *format,
175            ...)
176 {
177   va_list args;
178   va_start (args, format);
179   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
180   va_end (args);
181 }
182 static void
183 g_debug (const gchar *format,
184          ...)
185 {
186   va_list args;
187   va_start (args, format);
188   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
189   va_end (args);
190 }
191 #endif  /* !__GNUC__ */
192
193 typedef void    (*GPrintFunc)           (const gchar    *string);
194 void            g_print                 (const gchar    *format,
195                                          ...) G_GNUC_PRINTF (1, 2);
196 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
197 void            g_printerr              (const gchar    *format,
198                                          ...) G_GNUC_PRINTF (1, 2);
199 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
200
201
202 /* Provide macros for error handling. The "assert" macros will
203  *  exit on failure. The "return" macros will exit the current
204  *  function. Two different definitions are given for the macros
205  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
206  *  __PRETTY_FUNCTION__ capability.
207  */
208
209 #ifdef G_DISABLE_ASSERT
210
211 #define g_assert(expr)          G_STMT_START{ (void)0; }G_STMT_END
212 #define g_assert_not_reached()  G_STMT_START{ (void)0; }G_STMT_END
213
214 #else /* !G_DISABLE_ASSERT */
215
216 #ifdef __GNUC__
217
218 #define g_assert(expr)                  G_STMT_START{           \
219      if G_LIKELY(expr) { } else                                 \
220         g_log (G_LOG_DOMAIN,                                    \
221               G_LOG_LEVEL_ERROR,                                \
222               "file %s: line %d (%s): assertion failed: (%s)",  \
223               __FILE__,                                         \
224               __LINE__,                                         \
225               __PRETTY_FUNCTION__,                              \
226               #expr);                   }G_STMT_END
227
228 #define g_assert_not_reached()          G_STMT_START{           \
229      g_log (G_LOG_DOMAIN,                                       \
230             G_LOG_LEVEL_ERROR,                                  \
231             "file %s: line %d (%s): should not be reached",     \
232             __FILE__,                                           \
233             __LINE__,                                           \
234             __PRETTY_FUNCTION__);       }G_STMT_END
235
236 #else /* !__GNUC__ */
237
238 #define g_assert(expr)                  G_STMT_START{           \
239      if (expr) { } else                                         \
240        g_log (G_LOG_DOMAIN,                                     \
241               G_LOG_LEVEL_ERROR,                                \
242               "file %s: line %d: assertion failed: (%s)",       \
243               __FILE__,                                         \
244               __LINE__,                                         \
245               #expr);                   }G_STMT_END
246
247 #define g_assert_not_reached()          G_STMT_START{   \
248      g_log (G_LOG_DOMAIN,                               \
249             G_LOG_LEVEL_ERROR,                          \
250             "file %s: line %d: should not be reached",  \
251             __FILE__,                                   \
252             __LINE__);          }G_STMT_END
253
254 #endif /* __GNUC__ */
255
256 #endif /* !G_DISABLE_ASSERT */
257
258
259 #ifdef G_DISABLE_CHECKS
260
261 #define g_return_if_fail(expr)                  G_STMT_START{ (void)0; }G_STMT_END
262 #define g_return_val_if_fail(expr,val)          G_STMT_START{ (void)0; }G_STMT_END
263 #define g_return_if_reached()                   G_STMT_START{ return; }G_STMT_END
264 #define g_return_val_if_reached(val)            G_STMT_START{ return (val); }G_STMT_END
265
266 #else /* !G_DISABLE_CHECKS */
267
268 #ifdef __GNUC__
269
270 #define g_return_if_fail(expr)          G_STMT_START{                   \
271      if G_LIKELY(expr) { } else                                         \
272        {                                                                \
273          g_log (G_LOG_DOMAIN,                                           \
274                 G_LOG_LEVEL_CRITICAL,                                   \
275                 "file %s: line %d (%s): assertion `%s' failed",         \
276                 __FILE__,                                               \
277                 __LINE__,                                               \
278                 __PRETTY_FUNCTION__,                                    \
279                 #expr);                                                 \
280          return;                                                        \
281        };                               }G_STMT_END
282
283 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
284      if G_LIKELY(expr) { } else                                         \
285        {                                                                \
286          g_log (G_LOG_DOMAIN,                                           \
287                 G_LOG_LEVEL_CRITICAL,                                   \
288                 "file %s: line %d (%s): assertion `%s' failed",         \
289                 __FILE__,                                               \
290                 __LINE__,                                               \
291                 __PRETTY_FUNCTION__,                                    \
292                 #expr);                                                 \
293          return (val);                                                  \
294        };                               }G_STMT_END
295
296 #define g_return_if_reached()           G_STMT_START{                   \
297      g_log (G_LOG_DOMAIN,                                               \
298             G_LOG_LEVEL_CRITICAL,                                       \
299             "file %s: line %d (%s): should not be reached",             \
300             __FILE__,                                                   \
301             __LINE__,                                                   \
302             __PRETTY_FUNCTION__);                                       \
303      return;                            }G_STMT_END
304
305 #define g_return_val_if_reached(val)    G_STMT_START{                   \
306      g_log (G_LOG_DOMAIN,                                               \
307             G_LOG_LEVEL_CRITICAL,                                       \
308             "file %s: line %d (%s): should not be reached",             \
309             __FILE__,                                                   \
310             __LINE__,                                                   \
311             __PRETTY_FUNCTION__);                                       \
312      return (val);                      }G_STMT_END
313
314 #else /* !__GNUC__ */
315
316 #define g_return_if_fail(expr)          G_STMT_START{           \
317      if (expr) { } else                                         \
318        {                                                        \
319          g_log (G_LOG_DOMAIN,                                   \
320                 G_LOG_LEVEL_CRITICAL,                           \
321                 "file %s: line %d: assertion `%s' failed",      \
322                 __FILE__,                                       \
323                 __LINE__,                                       \
324                 #expr);                                         \
325          return;                                                \
326        };                               }G_STMT_END
327
328 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
329      if (expr) { } else                                         \
330        {                                                        \
331          g_log (G_LOG_DOMAIN,                                   \
332                 G_LOG_LEVEL_CRITICAL,                           \
333                 "file %s: line %d: assertion `%s' failed",      \
334                 __FILE__,                                       \
335                 __LINE__,                                       \
336                 #expr);                                         \
337          return (val);                                          \
338        };                               }G_STMT_END
339
340 #define g_return_if_reached()           G_STMT_START{           \
341      g_log (G_LOG_DOMAIN,                                       \
342             G_LOG_LEVEL_CRITICAL,                               \
343             "file %s: line %d: should not be reached",          \
344             __FILE__,                                           \
345             __LINE__);                                          \
346      return;                            }G_STMT_END
347
348 #define g_return_val_if_reached(val)    G_STMT_START{           \
349      g_log (G_LOG_DOMAIN,                                       \
350             G_LOG_LEVEL_CRITICAL,                               \
351             "file %s: line %d: should not be reached",          \
352             __FILE__,                                           \
353             __LINE__);                                          \
354      return (val);                      }G_STMT_END
355
356 #endif /* !__GNUC__ */
357
358 #endif /* !G_DISABLE_CHECKS */
359
360 G_END_DECLS
361
362 #endif /* __G_MESSAGES_H__ */
363