edelib 2.0.0

edelib/Debug.h

00001 /*
00002  * $Id: Debug.h 3067 2011-10-06 22:18:14Z karijes $
00003  *
00004  * Debug functions
00005  * Copyright (c) 2005-2011 edelib authors
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #ifndef __EDELIB_DEBUG_H__
00022 #define __EDELIB_DEBUG_H__
00023 
00024 #include "edelib-global.h"
00025 #include <stdarg.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00035 #ifndef SKIP_DOCS
00036 EDELIB_API void edelib_logv(const char *domain, int type, const char *fmt, va_list args);
00037 EDELIB_API void edelib_log(const char *domain, int type, const char *fmt, ...);
00038 #endif
00039 
00044 typedef enum {
00045         EDELIB_ERROR_MESSAGE_DEBUG,      
00046         EDELIB_ERROR_MESSAGE_WARNING,    
00047         EDELIB_ERROR_MESSAGE_FATAL       
00048 } EdelibErrorMessageType;
00049 
00053 EDELIB_API void edelib_error_mesage_handler_install(void (*)(int t, const char* domain, const char* msg));
00054 
00062 #ifndef E_LOG_DOMAIN
00063  #define E_LOG_DOMAIN ((char*)0)
00064 #endif
00065 
00066 #ifdef __GNUC__
00067  #define _E_FUNCTION_NAME __PRETTY_FUNCTION__
00068 #else
00069  #define _E_FUNCTION_NAME "<unknown>"
00070 #endif
00071 
00093 #ifdef EDELIB_HAVE_ISO_VARARGS
00094  #define E_DEBUG(...)    edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_DEBUG, __VA_ARGS__)
00095  #define E_WARNING(...)  edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_WARNING, __VA_ARGS__)
00096  #define E_FATAL(...)    edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, __VA_ARGS__)
00097 #elif defined(EDELIB_HAVE_GNUC_VARARGS)
00098  #define E_DEBUG(format...)    edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_DEBUG, format)
00099  #define E_WARNING(format...)  edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_WARNING, format)
00100  #define E_FATAL(format...)    edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, format)
00101 #else
00102  void E_DEBUG(const char *fmt, ...);
00103  void E_WARNING(const char *fmt, ...);
00104  void E_FATAL(const char *fmt, ...);
00105 #endif
00106 
00114 #ifdef NDEBUG
00115  #define E_ASSERT(expr)
00116 #else
00117  #define E_ASSERT(expr) \
00118         do { \
00119                 if(!(expr)) \
00120                 edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, "Assertion failed: \"%s\" in %s (%d), function: \"%s\"\n", \
00121                         #expr, __FILE__, __LINE__, _E_FUNCTION_NAME); \
00122         } while(0);
00123 #endif
00124 
00125 #define _E_STRLOC_STRINGIFY(arg)          _E_STRLOC_STRINGIFY_ARG(arg)
00126 #define _E_STRLOC_STRINGIFY_ARG(content)  #content
00127 
00134 #define E_STRLOC __FILE__ ":" _E_STRLOC_STRINGIFY(__LINE__)
00135 
00152 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00153         #define _E_BOOLEAN_EXPR(expr)         \
00154         __extension__ ({                      \
00155                         int _edelib_boolean_var_;     \
00156                         if(expr)                      \
00157                                 _edelib_boolean_var_ = 1; \
00158                         else                          \
00159                                 _edelib_boolean_var_ = 0; \
00160                         _edelib_boolean_var_;         \
00161         })
00162 
00163         #define E_LIKELY(expr)   (__builtin_expect(_E_BOOLEAN_EXPR(expr), 1))
00164         #define E_UNLIKELY(expr) (__builtin_expect(_E_BOOLEAN_EXPR(expr), 0))
00165 #else
00166         #define E_LIKELY(expr)   (expr)
00167         #define E_UNLIKELY(expr) (expr)
00168 #endif
00169 
00178 #define E_RETURN_IF_FAIL(expr)                                      \
00179         do {                                                            \
00180                 if E_LIKELY(expr) { }                                       \
00181                 else {                                                      \
00182                         E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
00183                         return;                                                 \
00184                 }                                                           \
00185         } while(0)                 
00186 
00194 #define E_RETURN_VAL_IF_FAIL(expr, val)                             \
00195         do {                                                            \
00196                 if E_LIKELY(expr) { }                                       \
00197                 else {                                                      \
00198                         E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
00199                         return (val);                                           \
00200                 }                                                           \
00201         } while(0)
00202 
00203 
00204 /* compatibility with the old code */
00205 #define EDEBUG   E_DEBUG
00206 #define EWARNING E_WARNING
00207 #define EFATAL   E_FATAL
00208 #define EASSERT  E_ASSERT
00209 #define ESTRLOC  E_STRLOC
00210 
00211 #ifdef __cplusplus
00212 }
00213 #endif
00214 
00215 #endif