gmessages.h

Go to the documentation of this file.
00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 /*
00022  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00023  * file for a list of people on the GLib Team.  See the ChangeLog
00024  * files for a list of changes.  These files are distributed with
00025  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
00026  */
00027 
00028 #ifndef __G_MESSAGES_H__
00029 #define __G_MESSAGES_H__
00030 
00031 #include <_ansi.h>
00032 #include <stdarg.h>
00033 #include <glib/gtypes.h>
00034 #include <glib/gmacros.h>
00035 
00036 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
00037  */
00038 #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
00039 #pragma GCC system_header
00040 #endif
00041 
00042 G_BEGIN_DECLS
00043 
00044 /* calculate a string size, guaranteed to fit format + args.
00045  */
00046 IMPORT_C gsize  g_printf_string_upper_bound (const gchar* format,
00047                                      va_list      args);
00048 
00049 /* Log level shift offset for user defined
00050  * log levels (0-7 are used by GLib).
00051  */
00052 #define G_LOG_LEVEL_USER_SHIFT  (8)
00053 
00054 /* Glib log levels and flags.
00055  */
00056 typedef enum
00057 {
00058   /* log flags */
00059   G_LOG_FLAG_RECURSION          = 1 << 0,
00060   G_LOG_FLAG_FATAL              = 1 << 1,
00061 
00062   /* GLib log levels */
00063   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
00064   G_LOG_LEVEL_CRITICAL          = 1 << 3,
00065   G_LOG_LEVEL_WARNING           = 1 << 4,
00066   G_LOG_LEVEL_MESSAGE           = 1 << 5,
00067   G_LOG_LEVEL_INFO              = 1 << 6,
00068   G_LOG_LEVEL_DEBUG             = 1 << 7,
00069 
00070   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
00071 } GLogLevelFlags;
00072 
00073 /* GLib log levels that are considered fatal by default */
00074 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
00075 
00076 typedef void            (*GLogFunc)             (const gchar   *log_domain,
00077                                                  GLogLevelFlags log_level,
00078                                                  const gchar   *message,
00079                                                  gpointer       user_data);
00080 
00081 /* Logging mechanism
00082  */
00083 IMPORT_C guint           g_log_set_handler       (const gchar    *log_domain,
00084                                          GLogLevelFlags  log_levels,
00085                                          GLogFunc        log_func,
00086                                          gpointer        user_data);
00087 IMPORT_C void            g_log_remove_handler    (const gchar    *log_domain,
00088                                          guint           handler_id);
00089 IMPORT_C void            g_log_default_handler   (const gchar    *log_domain,
00090                                          GLogLevelFlags  log_level,
00091                                          const gchar    *message,
00092                                          gpointer        unused_data);
00093 IMPORT_C GLogFunc        g_log_set_default_handler (GLogFunc      log_func,
00094                                            gpointer      user_data);
00095 IMPORT_C void            g_log                   (const gchar    *log_domain,
00096                                          GLogLevelFlags  log_level,
00097                                          const gchar    *format,
00098                                          ...) G_GNUC_PRINTF (3, 4);
00099 IMPORT_C void            g_logv                  (const gchar    *log_domain,
00100                                          GLogLevelFlags  log_level,
00101                                          const gchar    *format,
00102                                          va_list         args);
00103 IMPORT_C GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
00104                                          GLogLevelFlags  fatal_mask);
00105 IMPORT_C GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
00106 
00107 /* internal */
00108 void    _g_log_fallback_handler (const gchar   *log_domain,
00109                                  GLogLevelFlags log_level,
00110                                  const gchar   *message,
00111                                  gpointer       unused_data) G_GNUC_INTERNAL;
00112 
00113 /* Internal functions, used to implement the following macros */
00114 IMPORT_C void g_return_if_fail_warning (const char *log_domain,
00115                                const char *pretty_function,
00116                                const char *expression);
00117 IMPORT_C void g_assert_warning         (const char *log_domain,
00118                                const char *file,
00119                                const int   line,
00120                                const char *pretty_function,
00121                                const char *expression) G_GNUC_NORETURN;
00122 
00123 
00124 #ifndef G_LOG_DOMAIN
00125 #define G_LOG_DOMAIN    ((gchar*) 0)
00126 #endif  /* G_LOG_DOMAIN */
00127 #ifdef G_HAVE_ISO_VARARGS
00128 #define g_error(...)    g_log (G_LOG_DOMAIN,         \
00129                                G_LOG_LEVEL_ERROR,    \
00130                                __VA_ARGS__)
00131 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
00132                                G_LOG_LEVEL_MESSAGE,  \
00133                                __VA_ARGS__)
00134 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
00135                                G_LOG_LEVEL_CRITICAL, \
00136                                __VA_ARGS__)
00137 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
00138                                G_LOG_LEVEL_WARNING,  \
00139                                __VA_ARGS__)
00140 #define g_debug(...)    g_log (G_LOG_DOMAIN,         \
00141                                G_LOG_LEVEL_DEBUG,    \
00142                                __VA_ARGS__)
00143 #elif defined(G_HAVE_GNUC_VARARGS)
00144 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
00145                                        G_LOG_LEVEL_ERROR,    \
00146                                        format)
00147 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
00148                                        G_LOG_LEVEL_MESSAGE,  \
00149                                        format)
00150 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
00151                                        G_LOG_LEVEL_CRITICAL, \
00152                                        format)
00153 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
00154                                        G_LOG_LEVEL_WARNING,  \
00155                                        format)
00156 #define g_debug(format...)      g_log (G_LOG_DOMAIN,         \
00157                                        G_LOG_LEVEL_DEBUG,    \
00158                                        format)
00159 #else   /* no varargs macros */
00160 static void
00161 g_error (const gchar *format,
00162          ...)
00163 {
00164   va_list args;
00165   va_start (args, format);
00166   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
00167   va_end (args);
00168 }
00169 static void
00170 g_message (const gchar *format,
00171            ...)
00172 {
00173   va_list args;
00174   va_start (args, format);
00175   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
00176   va_end (args);
00177 }
00178 static void
00179 g_critical (const gchar *format,
00180             ...)
00181 {
00182   va_list args;
00183   va_start (args, format);
00184   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
00185   va_end (args);
00186 }
00187 static void
00188 g_warning (const gchar *format,
00189            ...)
00190 {
00191   va_list args;
00192   va_start (args, format);
00193   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
00194   va_end (args);
00195 }
00196 static void
00197 g_debug (const gchar *format,
00198          ...)
00199 {
00200   va_list args;
00201   va_start (args, format);
00202   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
00203   va_end (args);
00204 }
00205 #endif  /* !__GNUC__ */
00206 
00207 typedef void    (*GPrintFunc)           (const gchar    *string);
00208 IMPORT_C void            g_print                 (const gchar    *format,
00209                                          ...) G_GNUC_PRINTF (1, 2);
00210 IMPORT_C GPrintFunc      g_set_print_handler     (GPrintFunc      func);
00211 IMPORT_C void            g_printerr              (const gchar    *format,
00212                                          ...) G_GNUC_PRINTF (1, 2);
00213 IMPORT_C GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
00214 
00215 
00216 /* Provide macros for error handling. The "assert" macros will
00217  *  exit on failure. The "return" macros will exit the current
00218  *  function. Two different definitions are given for the macros
00219  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
00220  *  __PRETTY_FUNCTION__ capability.
00221  */
00222 
00223 #ifdef G_DISABLE_ASSERT
00224 
00225 #define g_assert(expr)          G_STMT_START{ (void)0; }G_STMT_END
00226 #define g_assert_not_reached()  G_STMT_START{ (void)0; }G_STMT_END
00227 
00228 #else /* !G_DISABLE_ASSERT */
00229 
00230 #ifdef __GNUC__
00231 
00232 #define g_assert(expr)                  G_STMT_START{           \
00233      if G_LIKELY(expr) { } else                                 \
00234         g_assert_warning (G_LOG_DOMAIN,                         \
00235                           __FILE__,                             \
00236                           __LINE__,                             \
00237                           __PRETTY_FUNCTION__,                  \
00238                           #expr);                 }G_STMT_END
00239 
00240 #define g_assert_not_reached()          G_STMT_START{           \
00241         g_assert_warning (G_LOG_DOMAIN,                         \
00242                           __FILE__,                             \
00243                           __LINE__,                             \
00244                           __PRETTY_FUNCTION__,                  \
00245                           NULL);                  }G_STMT_END
00246 
00247 #else /* !__GNUC__ */
00248 
00249 #define g_assert(expr)                  G_STMT_START{           \
00250      if (expr) { } else                                         \
00251        g_log (G_LOG_DOMAIN,                                     \
00252               G_LOG_LEVEL_ERROR,                                \
00253               "file %s: line %d: assertion failed: (%s)",       \
00254               __FILE__,                                         \
00255               __LINE__,                                         \
00256               #expr);                   }G_STMT_END
00257 
00258 #define g_assert_not_reached()          G_STMT_START{   \
00259      g_log (G_LOG_DOMAIN,                               \
00260             G_LOG_LEVEL_ERROR,                          \
00261             "file %s: line %d: should not be reached",  \
00262             __FILE__,                                   \
00263             __LINE__);          }G_STMT_END
00264 
00265 #endif /* __GNUC__ */
00266 
00267 #endif /* !G_DISABLE_ASSERT */
00268 
00269 
00270 #ifdef G_DISABLE_CHECKS
00271 
00272 #define g_return_if_fail(expr)                  G_STMT_START{ (void)0; }G_STMT_END
00273 #define g_return_val_if_fail(expr,val)          G_STMT_START{ (void)0; }G_STMT_END
00274 #define g_return_if_reached()                   G_STMT_START{ return; }G_STMT_END
00275 #define g_return_val_if_reached(val)            G_STMT_START{ return (val); }G_STMT_END
00276 
00277 #else /* !G_DISABLE_CHECKS */
00278 
00279 #ifdef __GNUC__
00280 
00281 #define g_return_if_fail(expr)          G_STMT_START{                   \
00282      if G_LIKELY(expr) { } else                                         \
00283        {                                                                \
00284          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
00285                                    __PRETTY_FUNCTION__,                 \
00286                                    #expr);                              \
00287          return;                                                        \
00288        };                               }G_STMT_END
00289 
00290 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
00291      if G_LIKELY(expr) { } else                                         \
00292        {                                                                \
00293          g_return_if_fail_warning (G_LOG_DOMAIN,                        \
00294                                    __PRETTY_FUNCTION__,                 \
00295                                    #expr);                              \
00296          return (val);                                                  \
00297        };                               }G_STMT_END
00298 
00299 #define g_return_if_reached()           G_STMT_START{                   \
00300      g_log (G_LOG_DOMAIN,                                               \
00301             G_LOG_LEVEL_CRITICAL,                                       \
00302             "file %s: line %d (%s): should not be reached",             \
00303             __FILE__,                                                   \
00304             __LINE__,                                                   \
00305             __PRETTY_FUNCTION__);                                       \
00306      return;                            }G_STMT_END
00307 
00308 #define g_return_val_if_reached(val)    G_STMT_START{                   \
00309      g_log (G_LOG_DOMAIN,                                               \
00310             G_LOG_LEVEL_CRITICAL,                                       \
00311             "file %s: line %d (%s): should not be reached",             \
00312             __FILE__,                                                   \
00313             __LINE__,                                                   \
00314             __PRETTY_FUNCTION__);                                       \
00315      return (val);                      }G_STMT_END
00316 
00317 #else /* !__GNUC__ */
00318 
00319 #define g_return_if_fail(expr)          G_STMT_START{           \
00320      if (expr) { } else                                         \
00321        {                                                        \
00322          g_log (G_LOG_DOMAIN,                                   \
00323                 G_LOG_LEVEL_CRITICAL,                           \
00324                 "file %s: line %d: assertion `%s' failed",      \
00325                 __FILE__,                                       \
00326                 __LINE__,                                       \
00327                 #expr);                                         \
00328          return;                                                \
00329        };                               }G_STMT_END
00330 
00331 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
00332      if (expr) { } else                                         \
00333        {                                                        \
00334          g_log (G_LOG_DOMAIN,                                   \
00335                 G_LOG_LEVEL_CRITICAL,                           \
00336                 "file %s: line %d: assertion `%s' failed",      \
00337                 __FILE__,                                       \
00338                 __LINE__,                                       \
00339                 #expr);                                         \
00340          return (val);                                          \
00341        };                               }G_STMT_END
00342 
00343 #define g_return_if_reached()           G_STMT_START{           \
00344      g_log (G_LOG_DOMAIN,                                       \
00345             G_LOG_LEVEL_CRITICAL,                               \
00346             "file %s: line %d: should not be reached",          \
00347             __FILE__,                                           \
00348             __LINE__);                                          \
00349      return;                            }G_STMT_END
00350 
00351 #define g_return_val_if_reached(val)    G_STMT_START{           \
00352      g_log (G_LOG_DOMAIN,                                       \
00353             G_LOG_LEVEL_CRITICAL,                               \
00354             "file %s: line %d: should not be reached",          \
00355             __FILE__,                                           \
00356             __LINE__);                                          \
00357      return (val);                      }G_STMT_END
00358 
00359 #endif /* !__GNUC__ */
00360 
00361 #endif /* !G_DISABLE_CHECKS */
00362 
00363 G_END_DECLS
00364 
00365 #endif /* __G_MESSAGES_H__ */
00366 

Copyright © Nokia Corporation 2001-2008
Back to top