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