Tizen 2.1 base
[platform/upstream/glib2.0.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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_MESSAGES_H__
32 #define __G_MESSAGES_H__
33
34 #include <stdarg.h>
35 #include <glib/gtypes.h>
36 #include <glib/gmacros.h>
37
38 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
39  */
40 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
41 #pragma GCC system_header
42 #endif
43
44 G_BEGIN_DECLS
45
46 /* calculate a string size, guaranteed to fit format + args.
47  */
48 gsize   g_printf_string_upper_bound (const gchar* format,
49                                      va_list      args);
50
51 /* Log level shift offset for user defined
52  * log levels (0-7 are used by GLib).
53  */
54 #define G_LOG_LEVEL_USER_SHIFT  (8)
55
56 /* Glib log levels and flags.
57  */
58 typedef enum
59 {
60   /* log flags */
61   G_LOG_FLAG_RECURSION          = 1 << 0,
62   G_LOG_FLAG_FATAL              = 1 << 1,
63
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,
71
72   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
73 } GLogLevelFlags;
74
75 /* GLib log levels that are considered fatal by default */
76 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
77
78 typedef void            (*GLogFunc)             (const gchar   *log_domain,
79                                                  GLogLevelFlags log_level,
80                                                  const gchar   *message,
81                                                  gpointer       user_data);
82
83 /* Logging mechanism
84  */
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);
108
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);
114
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 GLIB_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
131
132 #ifndef G_LOG_DOMAIN
133 #define G_LOG_DOMAIN    ((gchar*) 0)
134 #endif  /* G_LOG_DOMAIN */
135 #ifdef G_HAVE_ISO_VARARGS
136 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
137  * Put space before ending semicolon to avoid C++ build warnings.
138  */
139 #define g_error(...)  G_STMT_START {                 \
140                         g_log (G_LOG_DOMAIN,         \
141                                G_LOG_LEVEL_ERROR,    \
142                                __VA_ARGS__);         \
143                         for (;;) ;                   \
144                       } G_STMT_END
145                         
146 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
147                                G_LOG_LEVEL_MESSAGE,  \
148                                __VA_ARGS__)
149 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
150                                G_LOG_LEVEL_CRITICAL, \
151                                __VA_ARGS__)
152 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
153                                G_LOG_LEVEL_WARNING,  \
154                                __VA_ARGS__)
155 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
156                                G_LOG_LEVEL_DEBUG,    \
157                                __VA_ARGS__)
158 #elif defined(G_HAVE_GNUC_VARARGS)
159 #define g_error(format...)    G_STMT_START {                 \
160                                 g_log (G_LOG_DOMAIN,         \
161                                        G_LOG_LEVEL_ERROR,    \
162                                        format);              \
163                                 for (;;) ;                   \
164                               } G_STMT_END
165                               
166 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
167                                        G_LOG_LEVEL_MESSAGE,  \
168                                        format)
169 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
170                                        G_LOG_LEVEL_CRITICAL, \
171                                        format)
172 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
173                                        G_LOG_LEVEL_WARNING,  \
174                                        format)
175 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
176                                        G_LOG_LEVEL_DEBUG,    \
177                                        format)
178 #else   /* no varargs macros */
179 static void
180 g_error (const gchar *format,
181          ...)
182 {
183   va_list args;
184   va_start (args, format);
185   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
186   va_end (args);
187
188   for(;;) ;
189 }
190 static void
191 g_message (const gchar *format,
192            ...)
193 {
194   va_list args;
195   va_start (args, format);
196   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
197   va_end (args);
198 }
199 static void
200 g_critical (const gchar *format,
201             ...)
202 {
203   va_list args;
204   va_start (args, format);
205   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
206   va_end (args);
207 }
208 static void
209 g_warning (const gchar *format,
210            ...)
211 {
212   va_list args;
213   va_start (args, format);
214   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
215   va_end (args);
216 }
217 static void
218 g_debug (const gchar *format,
219          ...)
220 {
221   va_list args;
222   va_start (args, format);
223   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
224   va_end (args);
225 }
226 #endif  /* !__GNUC__ */
227
228 /**
229  * GPrintFunc:
230  * @string: the message to output
231  *
232  * Specifies the type of the print handler functions.
233  * These are called with the complete formatted string to output.
234  */
235 typedef void    (*GPrintFunc)           (const gchar    *string);
236 void            g_print                 (const gchar    *format,
237                                          ...) G_GNUC_PRINTF (1, 2);
238 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
239 void            g_printerr              (const gchar    *format,
240                                          ...) G_GNUC_PRINTF (1, 2);
241 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
242
243 /**
244  * g_warn_if_reached:
245  *
246  * Logs a critical warning.
247  *
248  * Since: 2.16
249  */
250 #define g_warn_if_reached() \
251   do { \
252     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
253   } while (0)
254
255 /**
256  * g_warn_if_fail:
257  * @expr: the expression to check
258  *
259  * Logs a warning if the expression is not true.
260  *
261  * Since: 2.16
262  */
263 #define g_warn_if_fail(expr) \
264   do { \
265     if G_LIKELY (expr) ; \
266     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
267   } while (0)
268
269 #ifdef G_DISABLE_CHECKS
270
271 /**
272  * g_return_if_fail:
273  * @expr: the expression to check
274  *
275  * Verifies that the expression evaluates to %TRUE.  If the expression
276  * evaluates to %FALSE, a critical message is logged and the current
277  * function returns.  This can only be used in functions which do not
278  * return a value.
279  *
280  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
281  * should therefore not depend on any side effects of @expr.
282  */
283 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
284
285 /**
286  * g_return_val_if_fail:
287  * @expr: the expression to check
288  * @val: the value to return from the current function
289  *       if the expression is not true
290  *
291  * Verifies that the expression evaluates to %TRUE.  If the expression
292  * evaluates to %FALSE, a critical message is logged and @val is
293  * returned from the current function.
294  *
295  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
296  * should therefore not depend on any side effects of @expr.
297  */
298 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
299
300 /**
301  * g_return_if_reached:
302  *
303  * Logs a critical message and returns from the current function.
304  * This can only be used in functions which do not return a value.
305  */
306 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
307
308 /**
309  * g_return_val_if_reached:
310  * @val: the value to return from the current function
311  *
312  * Logs a critical message and returns @val.
313  */
314 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
315
316 #else /* !G_DISABLE_CHECKS */
317
318 #ifdef __GNUC__
319
320 #define g_return_if_fail(expr)          G_STMT_START{                   \
321      if G_LIKELY(expr) { } else                                         \
322        {                                                                \
323          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
324                                    __PRETTY_FUNCTION__,                 \
325                                    #expr);                              \
326          return;                                                        \
327        };                               }G_STMT_END
328
329 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
330      if G_LIKELY(expr) { } else                                         \
331        {                                                                \
332          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
333                                    __PRETTY_FUNCTION__,                 \
334                                    #expr);                              \
335          return (val);                                                  \
336        };                               }G_STMT_END
337
338 #define g_return_if_reached()           G_STMT_START{                   \
339      g_log (G_LOG_DOMAIN,                                               \
340             G_LOG_LEVEL_CRITICAL,                                       \
341             "file %s: line %d (%s): should not be reached",             \
342             __FILE__,                                                   \
343             __LINE__,                                                   \
344             __PRETTY_FUNCTION__);                                       \
345      return;                            }G_STMT_END
346
347 #define g_return_val_if_reached(val)    G_STMT_START{                   \
348      g_log (G_LOG_DOMAIN,                                               \
349             G_LOG_LEVEL_CRITICAL,                                       \
350             "file %s: line %d (%s): should not be reached",             \
351             __FILE__,                                                   \
352             __LINE__,                                                   \
353             __PRETTY_FUNCTION__);                                       \
354      return (val);                      }G_STMT_END
355
356 #else /* !__GNUC__ */
357
358 #define g_return_if_fail(expr)          G_STMT_START{           \
359      if (expr) { } else                                         \
360        {                                                        \
361          g_log (G_LOG_DOMAIN,                                   \
362                 G_LOG_LEVEL_CRITICAL,                           \
363                 "file %s: line %d: assertion `%s' failed",      \
364                 __FILE__,                                       \
365                 __LINE__,                                       \
366                 #expr);                                         \
367          return;                                                \
368        };                               }G_STMT_END
369
370 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
371      if (expr) { } else                                         \
372        {                                                        \
373          g_log (G_LOG_DOMAIN,                                   \
374                 G_LOG_LEVEL_CRITICAL,                           \
375                 "file %s: line %d: assertion `%s' failed",      \
376                 __FILE__,                                       \
377                 __LINE__,                                       \
378                 #expr);                                         \
379          return (val);                                          \
380        };                               }G_STMT_END
381
382 #define g_return_if_reached()           G_STMT_START{           \
383      g_log (G_LOG_DOMAIN,                                       \
384             G_LOG_LEVEL_CRITICAL,                               \
385             "file %s: line %d: should not be reached",          \
386             __FILE__,                                           \
387             __LINE__);                                          \
388      return;                            }G_STMT_END
389
390 #define g_return_val_if_reached(val)    G_STMT_START{           \
391      g_log (G_LOG_DOMAIN,                                       \
392             G_LOG_LEVEL_CRITICAL,                               \
393             "file %s: line %d: should not be reached",          \
394             __FILE__,                                           \
395             __LINE__);                                          \
396      return (val);                      }G_STMT_END
397
398 #endif /* !__GNUC__ */
399
400 #endif /* !G_DISABLE_CHECKS */
401
402 G_END_DECLS
403
404 #endif /* __G_MESSAGES_H__ */