edelib  2.1.0
Debug.h
1 /*
2  * $Id: Debug.h 3444 2012-11-05 14:29:38Z karijes $
3  *
4  * Debug functions
5  * Copyright (c) 2005-2011 edelib authors
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef __EDELIB_DEBUG_H__
22 #define __EDELIB_DEBUG_H__
23 
24 #include "edelib-global.h"
25 #include <stdarg.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
35 #ifndef SKIP_DOCS
36 EDELIB_API void edelib_logv(const char *domain, int type, const char *fmt, va_list args);
37 EDELIB_API void edelib_log(const char *domain, int type, const char *fmt, ...);
38 #endif
39 
44 typedef enum {
45  EDELIB_ERROR_MESSAGE_DEBUG,
46  EDELIB_ERROR_MESSAGE_WARNING,
47  EDELIB_ERROR_MESSAGE_FATAL
48 } EdelibErrorMessageType;
49 
53 EDELIB_API void edelib_error_message_handler_install(void (*)(int t, const char* domain, const char* msg));
54 
62 #ifndef E_LOG_DOMAIN
63  #define E_LOG_DOMAIN ((char*)0)
64 #endif
65 
66 #ifdef __GNUC__
67  #define _E_FUNCTION_NAME __PRETTY_FUNCTION__
68 #else
69  #define _E_FUNCTION_NAME "<unknown>"
70 #endif
71 
93 #ifdef EDELIB_HAVE_ISO_VARARGS
94  #define E_DEBUG(...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_DEBUG, __VA_ARGS__)
95  #define E_WARNING(...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_WARNING, __VA_ARGS__)
96  #define E_FATAL(...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, __VA_ARGS__)
97 #elif defined(EDELIB_HAVE_GNUC_VARARGS)
98  #define E_DEBUG(format...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_DEBUG, format)
99  #define E_WARNING(format...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_WARNING, format)
100  #define E_FATAL(format...) edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, format)
101 #else
102  void E_DEBUG(const char *fmt, ...);
103  void E_WARNING(const char *fmt, ...);
104  void E_FATAL(const char *fmt, ...);
105 #endif
106 
114 #ifdef NDEBUG
115  #define E_ASSERT(expr)
116 #else
117  #define E_ASSERT(expr) \
118  do { \
119  if(!(expr)) \
120  edelib_log(E_LOG_DOMAIN, EDELIB_ERROR_MESSAGE_FATAL, "Assertion failed: \"%s\" in %s (%d), function: \"%s\"\n", \
121  #expr, __FILE__, __LINE__, _E_FUNCTION_NAME); \
122  } while(0);
123 #endif
124 
125 #define _E_STRLOC_STRINGIFY(arg) _E_STRLOC_STRINGIFY_ARG(arg)
126 #define _E_STRLOC_STRINGIFY_ARG(content) #content
127 
134 #define E_STRLOC __FILE__ ":" _E_STRLOC_STRINGIFY(__LINE__)
135 
152 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
153  #define _E_BOOLEAN_EXPR(expr) \
154  __extension__ ({ \
155  int _edelib_boolean_var_; \
156  if(expr) \
157  _edelib_boolean_var_ = 1; \
158  else \
159  _edelib_boolean_var_ = 0; \
160  _edelib_boolean_var_; \
161  })
162 
163  #define E_LIKELY(expr) (__builtin_expect(_E_BOOLEAN_EXPR(expr), 1))
164  #define E_UNLIKELY(expr) (__builtin_expect(_E_BOOLEAN_EXPR(expr), 0))
165 #else
166  #define E_LIKELY(expr) (expr)
167  #define E_UNLIKELY(expr) (expr)
168 #endif
169 
178 #define E_RETURN_IF_FAIL(expr) \
179  do { \
180  if E_LIKELY(expr) { } \
181  else { \
182  E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
183  return; \
184  } \
185  } while(0)
186 
194 #define E_RETURN_VAL_IF_FAIL(expr, val) \
195  do { \
196  if E_LIKELY(expr) { } \
197  else { \
198  E_WARNING(E_STRLOC ": Condition '%s' failed\n", #expr); \
199  return (val); \
200  } \
201  } while(0)
202 
203 
204 /* compatibility with the old code */
205 #define EDEBUG E_DEBUG
206 #define EWARNING E_WARNING
207 #define EFATAL E_FATAL
208 #define EASSERT E_ASSERT
209 #define ESTRLOC E_STRLOC
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif
#define E_FATAL(...)
Definition: Debug.h:96
#define E_DEBUG(...)
Definition: Debug.h:94
#define E_WARNING(...)
Definition: Debug.h:95