Split glib.h into many header files mostly according to the resp.
[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 <gtypes.h>
32
33 G_BEGIN_DECLS
34
35 /* calculate a string size, guarranteed to fit format + args.
36  */
37 guint   g_printf_string_upper_bound (const gchar* format,
38                                      va_list      args);
39
40 /* Log level shift offset for user defined
41  * log levels (0-7 are used by GLib).
42  */
43 #define G_LOG_LEVEL_USER_SHIFT  (8)
44
45 /* Glib log levels and flags.
46  */
47 typedef enum
48 {
49   /* log flags */
50   G_LOG_FLAG_RECURSION          = 1 << 0,
51   G_LOG_FLAG_FATAL              = 1 << 1,
52
53   /* GLib log levels */
54   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
55   G_LOG_LEVEL_CRITICAL          = 1 << 3,
56   G_LOG_LEVEL_WARNING           = 1 << 4,
57   G_LOG_LEVEL_MESSAGE           = 1 << 5,
58   G_LOG_LEVEL_INFO              = 1 << 6,
59   G_LOG_LEVEL_DEBUG             = 1 << 7,
60
61   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
62 } GLogLevelFlags;
63
64 /* GLib log levels that are considered fatal by default */
65 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
66
67 typedef void            (*GLogFunc)             (const gchar   *log_domain,
68                                                  GLogLevelFlags log_level,
69                                                  const gchar   *message,
70                                                  gpointer       user_data);
71
72 /* Logging mechanism
73  */
74 extern          const gchar             *g_log_domain_glib;
75 guint           g_log_set_handler       (const gchar    *log_domain,
76                                          GLogLevelFlags  log_levels,
77                                          GLogFunc        log_func,
78                                          gpointer        user_data);
79 void            g_log_remove_handler    (const gchar    *log_domain,
80                                          guint           handler_id);
81 void            g_log_default_handler   (const gchar    *log_domain,
82                                          GLogLevelFlags  log_level,
83                                          const gchar    *message,
84                                          gpointer        unused_data);
85 void            g_log                   (const gchar    *log_domain,
86                                          GLogLevelFlags  log_level,
87                                          const gchar    *format,
88                                          ...) G_GNUC_PRINTF (3, 4);
89 void            g_logv                  (const gchar    *log_domain,
90                                          GLogLevelFlags  log_level,
91                                          const gchar    *format,
92                                          va_list         args);
93 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
94                                          GLogLevelFlags  fatal_mask);
95 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
96
97 #ifndef G_LOG_DOMAIN
98 #define G_LOG_DOMAIN    ((gchar*) 0)
99 #endif  /* G_LOG_DOMAIN */
100 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
101 #define g_error(...)    g_log (G_LOG_DOMAIN,         \
102                                G_LOG_LEVEL_ERROR,    \
103                                __VA_ARGS__)
104 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
105                                G_LOG_LEVEL_MESSAGE,  \
106                                __VA_ARGS__)
107 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
108                                G_LOG_LEVEL_CRITICAL, \
109                                __VA_ARGS__)
110 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
111                                G_LOG_LEVEL_WARNING,  \
112                                __VA_ARGS__)
113 #elif __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4)
114 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
115                                        G_LOG_LEVEL_ERROR,    \
116                                        format)
117 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
118                                        G_LOG_LEVEL_MESSAGE,  \
119                                        format)
120 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
121                                        G_LOG_LEVEL_CRITICAL, \
122                                        format)
123 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
124                                        G_LOG_LEVEL_WARNING,  \
125                                        format)
126 #else   /* !__GNUC__ */
127 static void
128 g_error (const gchar *format,
129          ...)
130 {
131   va_list args;
132   va_start (args, format);
133   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
134   va_end (args);
135 }
136 static void
137 g_message (const gchar *format,
138            ...)
139 {
140   va_list args;
141   va_start (args, format);
142   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
143   va_end (args);
144 }
145 static void
146 g_critical (const gchar *format,
147             ...)
148 {
149   va_list args;
150   va_start (args, format);
151   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
152   va_end (args);
153 }
154 static void
155 g_warning (const gchar *format,
156            ...)
157 {
158   va_list args;
159   va_start (args, format);
160   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
161   va_end (args);
162 }
163 #endif  /* !__GNUC__ */
164
165 typedef void    (*GPrintFunc)           (const gchar    *string);
166 void            g_print                 (const gchar    *format,
167                                          ...) G_GNUC_PRINTF (1, 2);
168 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
169 void            g_printerr              (const gchar    *format,
170                                          ...) G_GNUC_PRINTF (1, 2);
171 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
172
173 /* deprecated compatibility functions, use g_log_set_handler() instead */
174 typedef void            (*GErrorFunc)           (const gchar *str);
175 typedef void            (*GWarningFunc)         (const gchar *str);
176 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
177 GWarningFunc g_set_warning_handler (GWarningFunc func);
178 GPrintFunc   g_set_message_handler (GPrintFunc func);
179
180 /* Provide macros for error handling. The "assert" macros will
181  *  exit on failure. The "return" macros will exit the current
182  *  function. Two different definitions are given for the macros
183  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
184  *  __PRETTY_FUNCTION__ capability.
185  */
186
187 #ifdef G_DISABLE_ASSERT
188
189 #define g_assert(expr)
190 #define g_assert_not_reached()
191
192 #else /* !G_DISABLE_ASSERT */
193
194 #ifdef __GNUC__
195
196 #define g_assert(expr)                  G_STMT_START{           \
197      if (!(expr))                                               \
198        g_log (G_LOG_DOMAIN,                                     \
199               G_LOG_LEVEL_ERROR,                                \
200               "file %s: line %d (%s): assertion failed: (%s)",  \
201               __FILE__,                                         \
202               __LINE__,                                         \
203               __PRETTY_FUNCTION__,                              \
204               #expr);                   }G_STMT_END
205
206 #define g_assert_not_reached()          G_STMT_START{           \
207      g_log (G_LOG_DOMAIN,                                       \
208             G_LOG_LEVEL_ERROR,                                  \
209             "file %s: line %d (%s): should not be reached",     \
210             __FILE__,                                           \
211             __LINE__,                                           \
212             __PRETTY_FUNCTION__);       }G_STMT_END
213
214 #else /* !__GNUC__ */
215
216 #define g_assert(expr)                  G_STMT_START{           \
217      if (!(expr))                                               \
218        g_log (G_LOG_DOMAIN,                                     \
219               G_LOG_LEVEL_ERROR,                                \
220               "file %s: line %d: assertion failed: (%s)",       \
221               __FILE__,                                         \
222               __LINE__,                                         \
223               #expr);                   }G_STMT_END
224
225 #define g_assert_not_reached()          G_STMT_START{   \
226      g_log (G_LOG_DOMAIN,                               \
227             G_LOG_LEVEL_ERROR,                          \
228             "file %s: line %d: should not be reached",  \
229             __FILE__,                                   \
230             __LINE__);          }G_STMT_END
231
232 #endif /* __GNUC__ */
233
234 #endif /* !G_DISABLE_ASSERT */
235
236
237 #ifdef G_DISABLE_CHECKS
238
239 #define g_return_if_fail(expr)
240 #define g_return_val_if_fail(expr,val)
241 #define g_return_if_reached() return
242 #define g_return_val_if_reached(val) return (val)
243
244 #else /* !G_DISABLE_CHECKS */
245
246 #ifdef __GNUC__
247
248 #define g_return_if_fail(expr)          G_STMT_START{                   \
249      if (!(expr))                                                       \
250        {                                                                \
251          g_log (G_LOG_DOMAIN,                                           \
252                 G_LOG_LEVEL_CRITICAL,                                   \
253                 "file %s: line %d (%s): assertion `%s' failed",         \
254                 __FILE__,                                               \
255                 __LINE__,                                               \
256                 __PRETTY_FUNCTION__,                                    \
257                 #expr);                                                 \
258          return;                                                        \
259        };                               }G_STMT_END
260
261 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
262      if (!(expr))                                                       \
263        {                                                                \
264          g_log (G_LOG_DOMAIN,                                           \
265                 G_LOG_LEVEL_CRITICAL,                                   \
266                 "file %s: line %d (%s): assertion `%s' failed",         \
267                 __FILE__,                                               \
268                 __LINE__,                                               \
269                 __PRETTY_FUNCTION__,                                    \
270                 #expr);                                                 \
271          return (val);                                                  \
272        };                               }G_STMT_END
273
274 #define g_return_if_reached()           G_STMT_START{                   \
275      g_log (G_LOG_DOMAIN,                                               \
276             G_LOG_LEVEL_CRITICAL,                                       \
277             "file %s: line %d (%s): should not be reached",             \
278             __FILE__,                                                   \
279             __LINE__,                                                   \
280             __PRETTY_FUNCTION__);                                       \
281      return;                            }G_STMT_END
282
283 #define g_return_val_if_reached(val)    G_STMT_START{                   \
284      g_log (G_LOG_DOMAIN,                                               \
285             G_LOG_LEVEL_CRITICAL,                                       \
286             "file %s: line %d (%s): should not be reached",             \
287             __FILE__,                                                   \
288             __LINE__,                                                   \
289             __PRETTY_FUNCTION__);                                       \
290      return (val);                      }G_STMT_END
291
292 #else /* !__GNUC__ */
293
294 #define g_return_if_fail(expr)          G_STMT_START{           \
295      if (!(expr))                                               \
296        {                                                        \
297          g_log (G_LOG_DOMAIN,                                   \
298                 G_LOG_LEVEL_CRITICAL,                           \
299                 "file %s: line %d: assertion `%s' failed",      \
300                 __FILE__,                                       \
301                 __LINE__,                                       \
302                 #expr);                                         \
303          return;                                                \
304        };                               }G_STMT_END
305
306 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
307      if (!(expr))                                               \
308        {                                                        \
309          g_log (G_LOG_DOMAIN,                                   \
310                 G_LOG_LEVEL_CRITICAL,                           \
311                 "file %s: line %d: assertion `%s' failed",      \
312                 __FILE__,                                       \
313                 __LINE__,                                       \
314                 #expr);                                         \
315          return (val);                                          \
316        };                               }G_STMT_END
317
318 #define g_return_if_reached()           G_STMT_START{           \
319      g_log (G_LOG_DOMAIN,                                       \
320             G_LOG_LEVEL_CRITICAL,                               \
321             "file %s: line %d: should not be reached",          \
322             __FILE__,                                           \
323             __LINE__);                                          \
324      return;                            }G_STMT_END
325
326 #define g_return_val_if_reached(val)    G_STMT_START{           \
327      g_log (G_LOG_DOMAIN,                                       \
328             G_LOG_LEVEL_CRITICAL,                               \
329             "file %s: line %d: should not be reached",          \
330             __FILE__,                                           \
331             __LINE__);                                          \
332      return (val);                      }G_STMT_END
333
334 #endif /* !__GNUC__ */
335
336 #endif /* !G_DISABLE_CHECKS */
337
338 G_END_DECLS
339
340 #endif /* __G_MESSAGES_H__ */
341