edelib/Debug.h

00001 /*
00002  * $Id: Debug.h 2614 2009-03-30 15:10:53Z karijes $
00003  *
00004  * Debug functions
00005  * Copyright (c) 2005-2007 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 
00037 #ifndef E_LOG_DOMAIN
00038         #define E_LOG_DOMAIN ((char*)0)
00039 #endif
00040 
00041 /* these functions are not in the namespace so we don't get * strange output with the preprocessor */
00042 EDELIB_API void _edelib_debug(const char* fmt, ...);
00043 EDELIB_API void _edelib_warning(const char* fmt, ...);
00044 EDELIB_API void _edelib_assert(int cond, const char* cond_text, const char* file, int line, const char* func);
00045 EDELIB_API void _edelib_fatal(const char* fmt, ...);
00046 
00047 #ifdef __GNUC__
00048         #define _E_FUNCTION_NAME __PRETTY_FUNCTION__
00049 #else
00050         #define _E_FUNCTION_NAME "<unknown>"
00051 #endif
00052 
00060 #ifdef NDEBUG
00061         #define E_ASSERT(expr)
00062 #else
00063         #define E_ASSERT(expr) _edelib_assert((expr) != 0, #expr, __FILE__, __LINE__, _E_FUNCTION_NAME)
00064 #endif
00065 
00072 #define E_DEBUG   _edelib_debug
00073 
00080 #define E_WARNING _edelib_warning
00081 
00088 #define E_FATAL   _edelib_fatal
00089 
00090 #define _E_STRLOC_STRINGIFY(arg)          _E_STRLOC_STRINGIFY_ARG(arg)
00091 #define _E_STRLOC_STRINGIFY_ARG(content)  #content
00092 
00099 #define E_STRLOC __FILE__ ":" _E_STRLOC_STRINGIFY(__LINE__)
00100 
00117 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00118         #define _E_BOOLEAN_EXPR(expr)         \
00119         __extension__ ({                      \
00120                         int _edelib_boolean_var_;     \
00121                         if(expr)                      \
00122                                 _edelib_boolean_var_ = 1; \
00123                         else                          \
00124                                 _edelib_boolean_var_ = 0; \
00125                         _edelib_boolean_var_;         \
00126         })
00127 
00128         #define E_LIKELY(expr)   (__builtin_expect(_E_BOOLEAN_EXPR(expr), 1))
00129         #define E_UNLIKELY(expr) (__builtin_expect(_E_BOOLEAN_EXPR(expr), 0))
00130 #else
00131         #define E_LIKELY(expr)   (expr)
00132         #define E_UNLIKELY(expr) (expr)
00133 #endif
00134 
00143 #define E_RETURN_IF_FAIL(expr)                                      \
00144         do {                                                            \
00145                 if E_LIKELY(expr) { }                                       \
00146                 else {                                                      \
00147                         E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
00148                         return;                                                 \
00149                 }                                                           \
00150         } while(0)                 
00151 
00159 #define E_RETURN_VAL_IF_FAIL(expr, val)                             \
00160         do {                                                            \
00161                 if E_LIKELY(expr) { }                                       \
00162                 else {                                                      \
00163                         E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
00164                         return (val);                                           \
00165                 }                                                           \
00166         } while(0)
00167 
00168 
00169 /* compatibility with the old code */
00170 #define EDEBUG   E_DEBUG
00171 #define EWARNING E_WARNING
00172 #define EFATAL   E_FATAL
00173 #define EASSERT  E_ASSERT
00174 #define ESTRLOC  E_STRLOC
00175 
00176 EDELIB_NS_BEGIN
00177 
00182 enum ErrorMessageType {
00183         ERROR_MESSAGE_DEBUG,      
00184         ERROR_MESSAGE_WARNING,    
00185         ERROR_MESSAGE_FATAL       
00186 };
00187 
00191 EDELIB_API void error_mesage_handler_install(void (*)(ErrorMessageType t, const char* msg));
00192 
00193 EDELIB_NS_END
00194 #endif

Generated on Wed Dec 16 14:31:52 2009 for edelib by  doxygen 1.5.2