hook gvariant vectors up to kdbus
[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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #ifndef __G_MESSAGES_H__
26 #define __G_MESSAGES_H__
27
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
31
32 #include <stdarg.h>
33 #include <glib/gtypes.h>
34 #include <glib/gmacros.h>
35
36 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
37  */
38 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
39 #pragma GCC system_header
40 #endif
41
42 G_BEGIN_DECLS
43
44 /* calculate a string size, guaranteed to fit format + args.
45  */
46 GLIB_AVAILABLE_IN_ALL
47 gsize   g_printf_string_upper_bound (const gchar* format,
48                                      va_list      args) G_GNUC_PRINTF(1, 0);
49
50 /* Log level shift offset for user defined
51  * log levels (0-7 are used by GLib).
52  */
53 #define G_LOG_LEVEL_USER_SHIFT  (8)
54
55 /* Glib log levels and flags.
56  */
57 typedef enum
58 {
59   /* log flags */
60   G_LOG_FLAG_RECURSION          = 1 << 0,
61   G_LOG_FLAG_FATAL              = 1 << 1,
62
63   /* GLib log levels */
64   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
65   G_LOG_LEVEL_CRITICAL          = 1 << 3,
66   G_LOG_LEVEL_WARNING           = 1 << 4,
67   G_LOG_LEVEL_MESSAGE           = 1 << 5,
68   G_LOG_LEVEL_INFO              = 1 << 6,
69   G_LOG_LEVEL_DEBUG             = 1 << 7,
70
71   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
72 } GLogLevelFlags;
73
74 /* GLib log levels that are considered fatal by default */
75 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
76
77 typedef void            (*GLogFunc)             (const gchar   *log_domain,
78                                                  GLogLevelFlags log_level,
79                                                  const gchar   *message,
80                                                  gpointer       user_data);
81
82 /* Logging mechanism
83  */
84 GLIB_AVAILABLE_IN_ALL
85 guint           g_log_set_handler       (const gchar    *log_domain,
86                                          GLogLevelFlags  log_levels,
87                                          GLogFunc        log_func,
88                                          gpointer        user_data);
89 GLIB_AVAILABLE_IN_ALL
90 void            g_log_remove_handler    (const gchar    *log_domain,
91                                          guint           handler_id);
92 GLIB_AVAILABLE_IN_ALL
93 void            g_log_default_handler   (const gchar    *log_domain,
94                                          GLogLevelFlags  log_level,
95                                          const gchar    *message,
96                                          gpointer        unused_data);
97 GLIB_AVAILABLE_IN_ALL
98 GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
99                                            gpointer      user_data);
100 GLIB_AVAILABLE_IN_ALL
101 void            g_log                   (const gchar    *log_domain,
102                                          GLogLevelFlags  log_level,
103                                          const gchar    *format,
104                                          ...) G_GNUC_PRINTF (3, 4);
105 GLIB_AVAILABLE_IN_ALL
106 void            g_logv                  (const gchar    *log_domain,
107                                          GLogLevelFlags  log_level,
108                                          const gchar    *format,
109                                          va_list         args) G_GNUC_PRINTF(3, 0);
110 GLIB_AVAILABLE_IN_ALL
111 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
112                                          GLogLevelFlags  fatal_mask);
113 GLIB_AVAILABLE_IN_ALL
114 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
115
116 /* internal */
117 void    _g_log_fallback_handler (const gchar   *log_domain,
118                                                  GLogLevelFlags log_level,
119                                                  const gchar   *message,
120                                                  gpointer       unused_data);
121
122 /* Internal functions, used to implement the following macros */
123 GLIB_AVAILABLE_IN_ALL
124 void g_return_if_fail_warning (const char *log_domain,
125                                const char *pretty_function,
126                                const char *expression) G_ANALYZER_NORETURN;
127 GLIB_AVAILABLE_IN_ALL
128 void g_warn_message           (const char     *domain,
129                                const char     *file,
130                                int             line,
131                                const char     *func,
132                                const char     *warnexpr) G_ANALYZER_NORETURN;
133 GLIB_DEPRECATED
134 void g_assert_warning         (const char *log_domain,
135                                const char *file,
136                                const int   line,
137                                const char *pretty_function,
138                                const char *expression) G_GNUC_NORETURN;
139
140
141 #ifndef G_LOG_DOMAIN
142 #define G_LOG_DOMAIN    ((gchar*) 0)
143 #endif  /* G_LOG_DOMAIN */
144
145 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
146 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
147  * Put space before ending semicolon to avoid C++ build warnings.
148  */
149 #define g_error(...)  G_STMT_START {                 \
150                         g_log (G_LOG_DOMAIN,         \
151                                G_LOG_LEVEL_ERROR,    \
152                                __VA_ARGS__);         \
153                         for (;;) ;                   \
154                       } G_STMT_END
155                         
156 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
157                                G_LOG_LEVEL_MESSAGE,  \
158                                __VA_ARGS__)
159 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
160                                G_LOG_LEVEL_CRITICAL, \
161                                __VA_ARGS__)
162 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
163                                G_LOG_LEVEL_WARNING,  \
164                                __VA_ARGS__)
165 #define g_info(...)     g_log (G_LOG_DOMAIN,         \
166                                G_LOG_LEVEL_INFO,     \
167                                __VA_ARGS__)
168 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
169                                G_LOG_LEVEL_DEBUG,    \
170                                __VA_ARGS__)
171 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
172 #define g_error(format...)    G_STMT_START {                 \
173                                 g_log (G_LOG_DOMAIN,         \
174                                        G_LOG_LEVEL_ERROR,    \
175                                        format);              \
176                                 for (;;) ;                   \
177                               } G_STMT_END
178                               
179 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
180                                        G_LOG_LEVEL_MESSAGE,  \
181                                        format)
182 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
183                                        G_LOG_LEVEL_CRITICAL, \
184                                        format)
185 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
186                                        G_LOG_LEVEL_WARNING,  \
187                                        format)
188 #define g_info(format...)       g_log (G_LOG_DOMAIN,         \
189                                        G_LOG_LEVEL_INFO,     \
190                                        format)
191 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
192                                        G_LOG_LEVEL_DEBUG,    \
193                                        format)
194 #else   /* no varargs macros */
195 static void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
196 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
197
198 static void
199 g_error (const gchar *format,
200          ...)
201 {
202   va_list args;
203   va_start (args, format);
204   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
205   va_end (args);
206
207   for(;;) ;
208 }
209 static void
210 g_message (const gchar *format,
211            ...)
212 {
213   va_list args;
214   va_start (args, format);
215   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
216   va_end (args);
217 }
218 static void
219 g_critical (const gchar *format,
220             ...)
221 {
222   va_list args;
223   va_start (args, format);
224   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
225   va_end (args);
226 }
227 static void
228 g_warning (const gchar *format,
229            ...)
230 {
231   va_list args;
232   va_start (args, format);
233   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
234   va_end (args);
235 }
236 static void
237 g_info (const gchar *format,
238         ...)
239 {
240   va_list args;
241   va_start (args, format);
242   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
243   va_end (args);
244 }
245 static void
246 g_debug (const gchar *format,
247          ...)
248 {
249   va_list args;
250   va_start (args, format);
251   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
252   va_end (args);
253 }
254 #endif  /* !__GNUC__ */
255
256 /**
257  * GPrintFunc:
258  * @string: the message to output
259  *
260  * Specifies the type of the print handler functions.
261  * These are called with the complete formatted string to output.
262  */
263 typedef void    (*GPrintFunc)           (const gchar    *string);
264 GLIB_AVAILABLE_IN_ALL
265 void            g_print                 (const gchar    *format,
266                                          ...) G_GNUC_PRINTF (1, 2);
267 GLIB_AVAILABLE_IN_ALL
268 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
269 GLIB_AVAILABLE_IN_ALL
270 void            g_printerr              (const gchar    *format,
271                                          ...) G_GNUC_PRINTF (1, 2);
272 GLIB_AVAILABLE_IN_ALL
273 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
274
275 /**
276  * g_warn_if_reached:
277  *
278  * Logs a critical warning.
279  *
280  * Since: 2.16
281  */
282 #define g_warn_if_reached() \
283   do { \
284     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
285   } while (0)
286
287 /**
288  * g_warn_if_fail:
289  * @expr: the expression to check
290  *
291  * Logs a warning if the expression is not true.
292  *
293  * Since: 2.16
294  */
295 #define g_warn_if_fail(expr) \
296   do { \
297     if G_LIKELY (expr) ; \
298     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
299   } while (0)
300
301 #ifdef G_DISABLE_CHECKS
302
303 /**
304  * g_return_if_fail:
305  * @expr: the expression to check
306  *
307  * Verifies that the expression @expr, usually representing a precondition,
308  * evaluates to %TRUE. If the function returns a value, use
309  * g_return_val_if_fail() instead.
310  *
311  * If @expr evaluates to %FALSE, the current function should be considered to
312  * have undefined behaviour (a programmer error). The only correct solution
313  * to such an error is to change the module that is calling the current
314  * function, so that it avoids this incorrect call.
315  *
316  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
317  * the result is usually that a critical message is logged and the current
318  * function returns.
319  *
320  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
321  * should therefore not depend on any side effects of @expr.
322  */
323 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
324
325 /**
326  * g_return_val_if_fail:
327  * @expr: the expression to check
328  * @val: the value to return from the current function
329  *       if the expression is not true
330  *
331  * Verifies that the expression @expr, usually representing a precondition,
332  * evaluates to %TRUE. If the function does not return a value, use
333  * g_return_if_fail() instead.
334  *
335  * If @expr evaluates to %FALSE, the current function should be considered to
336  * have undefined behaviour (a programmer error). The only correct solution
337  * to such an error is to change the module that is calling the current
338  * function, so that it avoids this incorrect call.
339  *
340  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
341  * the result is usually that a critical message is logged and @val is
342  * returned from the current function.
343  *
344  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
345  * should therefore not depend on any side effects of @expr.  See
346  * g_return_if_fail_se() for a version that guarantees side effects.
347  */
348 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
349
350 /**
351  * g_return_if_fail_se:
352  * @expr: the expression to check
353  *
354  * Verifies that the expression @expr, usually representing a
355  * precondition, evaluates to %TRUE.
356  *
357  * This is the same as g_return_if_fail() except that @expr is
358  * guaranteed to be evaluated, so it may contain side effects.
359  *
360  * Note: it is still undefined if this function will actually return or
361  * not, or if any side effects of @val will be evaluated.  There is only
362  * a guarantee with respect to side effects of @expr.
363  *
364  * Since: 2.44
365  */
366 #define g_return_if_fail_se(expr)         ((void) (expr))
367
368 /**
369  * g_return_val_if_fail_se:
370  * @expr: the expression to check
371  * @val: the value to return from the current function
372  *       if the expression is not true
373  *
374  * Verifies that the expression @expr, usually representing a
375  * precondition, evaluates to %TRUE.
376  *
377  * This is the same as g_return_val_if_fail() except that @expr is
378  * guaranteed to be evaluated, so it may contain side effects.
379  *
380  * Note: it is still undefined if this function will actually return or
381  * not, or if any side effects of @val will be evaluated.  There is only
382  * a guarantee with respect to side effects of @expr.
383  *
384  * Since: 2.44
385  */
386 #define g_return_val_if_fail_se(expr,val) ((void) (expr))
387
388 /**
389  * g_return_if_reached:
390  *
391  * Logs a critical message and returns from the current function.
392  * This can only be used in functions which do not return a value.
393  */
394 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
395
396 /**
397  * g_return_val_if_reached:
398  * @val: the value to return from the current function
399  *
400  * Logs a critical message and returns @val.
401  */
402 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
403
404 #else /* !G_DISABLE_CHECKS */
405
406 #define g_return_if_fail(expr)          G_STMT_START{                   \
407      if G_LIKELY(expr) { } else                                         \
408        {                                                                \
409          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
410                                    G_STRFUNC,                           \
411                                    #expr);                              \
412          return;                                                        \
413        };                               }G_STMT_END
414
415 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
416      if G_LIKELY(expr) { } else                                         \
417        {                                                                \
418          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
419                                    G_STRFUNC,                           \
420                                    #expr);                              \
421          return (val);                                                  \
422        };                               }G_STMT_END
423
424 #define g_return_if_fail_se(expr)         g_return_if_fail((expr))
425 #define g_return_val_if_fail_se(expr,val) g_return_val_if_fail((expr), (val))
426
427 #define g_return_if_reached()           G_STMT_START{                   \
428      g_log (G_LOG_DOMAIN,                                               \
429             G_LOG_LEVEL_CRITICAL,                                       \
430             "file %s: line %d (%s): should not be reached",             \
431             __FILE__,                                                   \
432             __LINE__,                                                   \
433             G_STRFUNC);                                                 \
434      return;                            }G_STMT_END
435
436 #define g_return_val_if_reached(val)    G_STMT_START{                   \
437      g_log (G_LOG_DOMAIN,                                               \
438             G_LOG_LEVEL_CRITICAL,                                       \
439             "file %s: line %d (%s): should not be reached",             \
440             __FILE__,                                                   \
441             __LINE__,                                                   \
442             G_STRFUNC);                                                 \
443      return (val);                      }G_STMT_END
444
445 #endif /* !G_DISABLE_CHECKS */
446
447 G_END_DECLS
448
449 #endif /* __G_MESSAGES_H__ */