00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00049 #ifndef BASE_DEBUG_H
00050 #define BASE_DEBUG_H
00051
00052 #include "fastlib/base/common.h"
00053
00069 #ifdef DEBUG
00070 #define DEBUG_ONLY if (0) NOP; else
00071 #else
00072 #define DEBUG_ONLY if (1) NOP; else
00073 #endif
00074
00080 #ifdef VERBOSE
00081 #define VERBOSE_ONLY if (0) NOP; else
00082 #else
00083 #define VERBOSE_ONLY if (1) NOP; else
00084 #endif
00085
00091 #ifdef PROFILE
00092 #define PROFILE_ONLY if (0) NOP; else
00093 #else
00094 #define PROFILE_ONLY if (1) NOP; else
00095 #endif
00096
00098 extern double verbosity_level;
00100 extern int print_got_heres;
00102 extern int print_warnings;
00103
00126 #define VERBOSE_MSG(min_verbosity, msg_params...) \
00127 VERBOSE_ONLY( \
00128 unlikely(verbosity_level >= (min_verbosity)) \
00129 ? NOTIFY(msg_params) : NOP)
00130
00141 #define VERBOSE_GOT_HERE(min_verbosity) \
00142 VERBOSE_ONLY( \
00143 unlikely(print_got_heres && verbosity_level >= (min_verbosity)) \
00144 ? NOTIFY("Got to line %d of %s", __LINE__, __FUNCTION__) : NOP)
00145
00161 #define DEBUG_WARNING_MSG_IF(cond, msg_params...) \
00162 DEBUG_ONLY(unlikely((cond) && print_warnings) \
00163 ? NONFATAL(msg_params) : NOP)
00164 #define DEBUG_WARN_MSG_IF DEBUG_WARNING_MSG_IF
00165
00180 #define DEBUG_WARNING_IF(cond) \
00181 DEBUG_WARNING_MSG_IF(cond, "warning: " #cond)
00182 #define DEBUG_WARN_IF DEBUG_WARNING_IF
00183
00195 #define DEBUG_ERROR_MSG_IF(cond, msg_params...) \
00196 DEBUG_ONLY(unlikely(cond) \
00197 ? FATAL(msg_params) : NOP)
00198 #define DEBUG_ERR_MSG_IF DEBUG_ERROR_MSG_IF
00199
00210 #define DEBUG_ERROR_IF(cond) \
00211 DEBUG_ERROR_MSG_IF(cond, "error: " #cond)
00212 #define DEBUG_ERR_IF DEBUG_ERROR_IF
00213
00225 #define DEBUG_ASSERT_MSG(cond, msg_params...) \
00226 DEBUG_ONLY(likely(cond) \
00227 ? NOP : FATAL(msg_params))
00228
00239 #define DEBUG_ASSERT(cond) \
00240 DEBUG_ASSERT_MSG(cond, "assertion failure: %s", #cond)
00241
00249 #define BIG_BAD_NUMBER 2146666666
00250
00261 #define BIG_BAD_POINTER(type) (REINTERPRET_CAST(type *, 0xDEADBEEF))
00262
00264 #ifdef __cplusplus
00265 template<typename T>
00266 const T *poison_ptr(T *&x) {
00267 return x = BIG_BAD_POINTER(T);
00268 }
00269 #else
00270 #define poison_ptr(x) ((x) = BIG_BAD_POINTER(void))
00271 #endif
00272
00283 #define DEBUG_POISON_PTR(x) DEBUG_ONLY(poison_ptr(x))
00284
00302 #define DEBUG_BOUNDS(x, bound) \
00303 DEBUG_ASSERT_MSG(STATIC_CAST(uint64, x) < STATIC_CAST(uint64, bound), \
00304 "DEBUG_BOUNDS failed: %s = %"L64"d not in [0, %s = %"L64"d)\n", \
00305 #x, STATIC_CAST(int64, x), #bound, STATIC_CAST(int64, bound))
00306
00319 #define DEBUG_BOUNDS_INCLUSIVE(x, bound) \
00320 DEBUG_ASSERT_MSG(STATIC_CAST(uint64, x) <= STATIC_CAST(uint64, bound), \
00321 "DEBUG_BOUNDS failed: %s = %"L64"d not in [0, %s = %"L64"d]\n", \
00322 #x, STATIC_CAST(int64, x), #bound, STATIC_CAST(int64, bound))
00323
00336 #define DEBUG_SAME_INT(x, y) \
00337 DEBUG_ASSERT_MSG((x) == (y), \
00338 "DEBUG_SAME_INT failed: %s = %"L64"d not equal to %s = %"L64"d\n", \
00339 #x, STATIC_CAST(int64, x), #y, STATIC_CAST(int64, y))
00340
00356 #define DEBUG_SAME_SIZE(x, y) \
00357 DEBUG_ASSERT_MSG((x) == (y), \
00358 "DEBUG_SAME_SIZE failed: %s = %"L64"d not equal to %s = %"L64"d\n", \
00359 #x, STATIC_CAST(int64, x), #y, STATIC_CAST(int64, y))
00360
00373 #define DEBUG_SAME_DOUBLE(x, y) \
00374 DEBUG_ASSERT_MSG((x) == (y), \
00375 "DEBUG_SAME_DOUBLE failed: %s = %g not equal to %s = %g\n", \
00376 #x, STATIC_CAST(double, x), #y, STATIC_CAST(double, y))
00377
00391 #define DEBUG_APPROX_DOUBLE(x, y, eps) \
00392 DEBUG_ASSERT_MSG(fabs(STATIC_CAST(double, (x) - (y))) < eps, \
00393 "DEBUG_APPROX_DOUBLE failed: %s = %g not within %g of %s = %g\n", \
00394 #x, STATIC_CAST(double, x), STATIC_CAST(double, eps), \
00395 #y, STATIC_CAST(double, y))
00396
00397 #endif