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