gmessages: Add G_GNUC_NORETURN to g_error static function declaration
[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_2_46
90 guint           g_log_set_handler_full  (const gchar    *log_domain,
91                                          GLogLevelFlags  log_levels,
92                                          GLogFunc        log_func,
93                                          gpointer        user_data,
94                                          GDestroyNotify  destroy);
95 GLIB_AVAILABLE_IN_ALL
96 void            g_log_remove_handler    (const gchar    *log_domain,
97                                          guint           handler_id);
98 GLIB_AVAILABLE_IN_ALL
99 void            g_log_default_handler   (const gchar    *log_domain,
100                                          GLogLevelFlags  log_level,
101                                          const gchar    *message,
102                                          gpointer        unused_data);
103 GLIB_AVAILABLE_IN_ALL
104 GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
105                                            gpointer      user_data);
106 GLIB_AVAILABLE_IN_ALL
107 void            g_log                   (const gchar    *log_domain,
108                                          GLogLevelFlags  log_level,
109                                          const gchar    *format,
110                                          ...) G_GNUC_PRINTF (3, 4);
111 GLIB_AVAILABLE_IN_ALL
112 void            g_logv                  (const gchar    *log_domain,
113                                          GLogLevelFlags  log_level,
114                                          const gchar    *format,
115                                          va_list         args) G_GNUC_PRINTF(3, 0);
116 GLIB_AVAILABLE_IN_ALL
117 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
118                                          GLogLevelFlags  fatal_mask);
119 GLIB_AVAILABLE_IN_ALL
120 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
121
122 /* internal */
123 void    _g_log_fallback_handler (const gchar   *log_domain,
124                                                  GLogLevelFlags log_level,
125                                                  const gchar   *message,
126                                                  gpointer       unused_data);
127
128 /* Internal functions, used to implement the following macros */
129 GLIB_AVAILABLE_IN_ALL
130 void g_return_if_fail_warning (const char *log_domain,
131                                const char *pretty_function,
132                                const char *expression) G_ANALYZER_NORETURN;
133 GLIB_AVAILABLE_IN_ALL
134 void g_warn_message           (const char     *domain,
135                                const char     *file,
136                                int             line,
137                                const char     *func,
138                                const char     *warnexpr) G_ANALYZER_NORETURN;
139 GLIB_DEPRECATED
140 void g_assert_warning         (const char *log_domain,
141                                const char *file,
142                                const int   line,
143                                const char *pretty_function,
144                                const char *expression) G_GNUC_NORETURN;
145
146
147 #ifndef G_LOG_DOMAIN
148 #define G_LOG_DOMAIN    ((gchar*) 0)
149 #endif  /* G_LOG_DOMAIN */
150
151 #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
152 /* for(;;) ; so that GCC knows that control doesn't go past g_error().
153  * Put space before ending semicolon to avoid C++ build warnings.
154  */
155 #define g_error(...)  G_STMT_START {                 \
156                         g_log (G_LOG_DOMAIN,         \
157                                G_LOG_LEVEL_ERROR,    \
158                                __VA_ARGS__);         \
159                         for (;;) ;                   \
160                       } G_STMT_END
161                         
162 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
163                                G_LOG_LEVEL_MESSAGE,  \
164                                __VA_ARGS__)
165 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
166                                G_LOG_LEVEL_CRITICAL, \
167                                __VA_ARGS__)
168 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
169                                G_LOG_LEVEL_WARNING,  \
170                                __VA_ARGS__)
171 #define g_info(...)     g_log (G_LOG_DOMAIN,         \
172                                G_LOG_LEVEL_INFO,     \
173                                __VA_ARGS__)
174 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
175                                G_LOG_LEVEL_DEBUG,    \
176                                __VA_ARGS__)
177 #elif defined(G_HAVE_GNUC_VARARGS)  && !G_ANALYZER_ANALYZING
178 #define g_error(format...)    G_STMT_START {                 \
179                                 g_log (G_LOG_DOMAIN,         \
180                                        G_LOG_LEVEL_ERROR,    \
181                                        format);              \
182                                 for (;;) ;                   \
183                               } G_STMT_END
184                               
185 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
186                                        G_LOG_LEVEL_MESSAGE,  \
187                                        format)
188 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
189                                        G_LOG_LEVEL_CRITICAL, \
190                                        format)
191 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
192                                        G_LOG_LEVEL_WARNING,  \
193                                        format)
194 #define g_info(format...)       g_log (G_LOG_DOMAIN,         \
195                                        G_LOG_LEVEL_INFO,     \
196                                        format)
197 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
198                                        G_LOG_LEVEL_DEBUG,    \
199                                        format)
200 #else   /* no varargs macros */
201 static void g_error (const gchar *format, ...) G_GNUC_NORETURN G_ANALYZER_NORETURN;
202 static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
203
204 static void
205 g_error (const gchar *format,
206          ...)
207 {
208   va_list args;
209   va_start (args, format);
210   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
211   va_end (args);
212
213   for(;;) ;
214 }
215 static void
216 g_message (const gchar *format,
217            ...)
218 {
219   va_list args;
220   va_start (args, format);
221   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
222   va_end (args);
223 }
224 static void
225 g_critical (const gchar *format,
226             ...)
227 {
228   va_list args;
229   va_start (args, format);
230   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
231   va_end (args);
232 }
233 static void
234 g_warning (const gchar *format,
235            ...)
236 {
237   va_list args;
238   va_start (args, format);
239   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
240   va_end (args);
241 }
242 static void
243 g_info (const gchar *format,
244         ...)
245 {
246   va_list args;
247   va_start (args, format);
248   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
249   va_end (args);
250 }
251 static void
252 g_debug (const gchar *format,
253          ...)
254 {
255   va_list args;
256   va_start (args, format);
257   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
258   va_end (args);
259 }
260 #endif  /* !__GNUC__ */
261
262 /**
263  * GPrintFunc:
264  * @string: the message to output
265  *
266  * Specifies the type of the print handler functions.
267  * These are called with the complete formatted string to output.
268  */
269 typedef void    (*GPrintFunc)           (const gchar    *string);
270 GLIB_AVAILABLE_IN_ALL
271 void            g_print                 (const gchar    *format,
272                                          ...) G_GNUC_PRINTF (1, 2);
273 GLIB_AVAILABLE_IN_ALL
274 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
275 GLIB_AVAILABLE_IN_ALL
276 void            g_printerr              (const gchar    *format,
277                                          ...) G_GNUC_PRINTF (1, 2);
278 GLIB_AVAILABLE_IN_ALL
279 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
280
281 /**
282  * g_warn_if_reached:
283  *
284  * Logs a critical warning.
285  *
286  * Since: 2.16
287  */
288 #define g_warn_if_reached() \
289   do { \
290     g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
291   } while (0)
292
293 /**
294  * g_warn_if_fail:
295  * @expr: the expression to check
296  *
297  * Logs a warning if the expression is not true.
298  *
299  * Since: 2.16
300  */
301 #define g_warn_if_fail(expr) \
302   do { \
303     if G_LIKELY (expr) ; \
304     else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
305   } while (0)
306
307 #ifdef G_DISABLE_CHECKS
308
309 /**
310  * g_return_if_fail:
311  * @expr: the expression to check
312  *
313  * Verifies that the expression @expr, usually representing a precondition,
314  * evaluates to %TRUE. If the function returns a value, use
315  * g_return_val_if_fail() instead.
316  *
317  * If @expr evaluates to %FALSE, the current function should be considered to
318  * have undefined behaviour (a programmer error). The only correct solution
319  * to such an error is to change the module that is calling the current
320  * function, so that it avoids this incorrect call.
321  *
322  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
323  * the result is usually that a critical message is logged and the current
324  * function returns.
325  *
326  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
327  * should therefore not depend on any side effects of @expr.
328  */
329 #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
330
331 /**
332  * g_return_val_if_fail:
333  * @expr: the expression to check
334  * @val: the value to return from the current function
335  *       if the expression is not true
336  *
337  * Verifies that the expression @expr, usually representing a precondition,
338  * evaluates to %TRUE. If the function does not return a value, use
339  * g_return_if_fail() instead.
340  *
341  * If @expr evaluates to %FALSE, the current function should be considered to
342  * have undefined behaviour (a programmer error). The only correct solution
343  * to such an error is to change the module that is calling the current
344  * function, so that it avoids this incorrect call.
345  *
346  * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
347  * the result is usually that a critical message is logged and @val is
348  * returned from the current function.
349  *
350  * If G_DISABLE_CHECKS is defined then the check is not performed.  You
351  * should therefore not depend on any side effects of @expr.
352  */
353 #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
354
355 /**
356  * g_return_if_reached:
357  *
358  * Logs a critical message and returns from the current function.
359  * This can only be used in functions which do not return a value.
360  */
361 #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
362
363 /**
364  * g_return_val_if_reached:
365  * @val: the value to return from the current function
366  *
367  * Logs a critical message and returns @val.
368  */
369 #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
370
371 #else /* !G_DISABLE_CHECKS */
372
373 #define g_return_if_fail(expr)          G_STMT_START{                   \
374      if G_LIKELY(expr) { } else                                         \
375        {                                                                \
376          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
377                                    G_STRFUNC,                           \
378                                    #expr);                              \
379          return;                                                        \
380        };                               }G_STMT_END
381
382 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
383      if G_LIKELY(expr) { } else                                         \
384        {                                                                \
385          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
386                                    G_STRFUNC,                           \
387                                    #expr);                              \
388          return (val);                                                  \
389        };                               }G_STMT_END
390
391 #define g_return_if_reached()           G_STMT_START{                   \
392      g_log (G_LOG_DOMAIN,                                               \
393             G_LOG_LEVEL_CRITICAL,                                       \
394             "file %s: line %d (%s): should not be reached",             \
395             __FILE__,                                                   \
396             __LINE__,                                                   \
397             G_STRFUNC);                                                 \
398      return;                            }G_STMT_END
399
400 #define g_return_val_if_reached(val)    G_STMT_START{                   \
401      g_log (G_LOG_DOMAIN,                                               \
402             G_LOG_LEVEL_CRITICAL,                                       \
403             "file %s: line %d (%s): should not be reached",             \
404             __FILE__,                                                   \
405             __LINE__,                                                   \
406             G_STRFUNC);                                                 \
407      return (val);                      }G_STMT_END
408
409 #endif /* !G_DISABLE_CHECKS */
410
411 G_END_DECLS
412
413 #endif /* __G_MESSAGES_H__ */