Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

macros.h

Go to the documentation of this file.
00001 /* -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
00002 /*********************************************************************
00003  *
00004  * Filename : macros.h (debug)
00005  * C header.
00006  *
00007  * Debugging macros:
00008  * - debugging statement with DODEBUG(something)
00009  * - pretty colors
00010  * - print position (in a function/method)
00011  *
00012  * This file is a part of the UPPAAL toolkit.
00013  * Copyright (c) 1995 - 2003, Uppsala University and Aalborg University.
00014  * All right reserved.
00015  *
00016  * v 1.2 reviewed.
00017  * $Id: macros.h,v 1.13 2005/06/22 13:30:32 adavid Exp $
00018  *
00019  **********************************************************************/
00020 
00021 #ifndef INCLUDE_DEBUG_MACROS_H
00022 #define INCLUDE_DEBUG_MACROS_H
00023 
00024 #include "debug/utils.h"
00025 
00026 /**********************************************************************
00027  * Macros for debugging
00028  *  NPRETTY_COLORS -> deactivate colorized printouts
00029  *  NSHORTFILENAME -> deactivate short filename printouts
00030  *  NDEBUG: controls debugging, standard
00031  **********************************************************************/
00032 
00033 /* Incompatibility: deactive pretty features */
00034 #ifdef _WIN32
00035 #define __PRETTY_FUNCTION__ __FUNCTION__
00036 #ifndef NPRETTY_COLORS
00037 #define NPRETTY_COLORS
00038 #endif
00039 #endif
00040 
00041 /* C++ example: std::cout << RED(BOLD) "Warning!" NORMAL << std::endl
00042  * C example: printf(RED(BOLD)"Warning!"NORMAL"\n")
00043  */
00044 #ifndef NPRETTY_COLORS
00045 #define THIN       "0"
00046 #define BOLD       "1"
00047 #define RED(S)     "\033["S";31m"
00048 #define GREEN(S)   "\033["S";32m"
00049 #define YELLOW(S)  "\033["S";33m"
00050 #define BLUE(S)    "\033["S";34m"
00051 #define MAGENTA(S) "\033["S";35m"
00052 #define CYAN(S)    "\033["S";36m"
00053 #define WHITE(S)   "\033["S";37m"
00054 #define NORMAL     "\033[0;0m"
00055 #else
00056 #define THIN       ""
00057 #define BOLD       ""
00058 #define RED(S)     ""
00059 #define GREEN(S)   ""
00060 #define YELLOW(S)  ""
00061 #define BLUE(S)    ""
00062 #define MAGENTA(S) ""
00063 #define CYAN(S)    ""
00064 #define WHITE(S)   ""
00065 #define NORMAL     ""
00066 #endif
00067 
00068 /* C/C++ default output streams.
00069  * No need to be in conditional def.
00070  */
00071 #ifdef __cplusplus
00072 #define DEFAULT_OUT std::cout
00073 #define DEFAULT_ERR std::cerr
00074 #else
00075 #define DEFAULT_OUT stdout
00076 #define DEFAULT_ERR stderr
00077 #endif
00078 
00079 #ifndef NDEBUG
00080 
00081 /* C/C++ print style
00082  */
00083 #ifdef __cplusplus
00084 #define PRINT(OUT,S) (OUT) << (S)
00085 #define PRINT_INT(OUT,N) (OUT) << (N)
00086 #else
00087 #define PRINT(OUT,S) fprintf(OUT,S)
00088 #define PRINT_INT(OUT,N) fprintf(OUT,"%d",(N))
00089 #endif
00090 
00091 /* Long or short printouts?
00092  */
00093 #ifndef NSHORTFILENAME
00094 #define PRINT_FILE(OUT) PRINT(OUT, debug_shortSource(__FILE__))
00095 #else
00096 #define PRINT_FILE(OUT) PRINT(OUT, __FILE__)
00097 #endif
00098 
00099 /* Statement to execute only for debugging
00100  * WARNING: there should not be side effect
00101  * on the normal code.
00102  */
00103 #define DODEBUG(STATEMENT) STATEMENT
00104 
00105 
00106 /* Print some info from a function.
00107  */
00108 #define PRINT_INFO(INFO)                   \
00109 do {                                       \
00110   PRINT(DEFAULT_OUT, __PRETTY_FUNCTION__); \
00111   PRINT(DEFAULT_OUT, ": " INFO "\n");      \
00112 } while(0)
00113 
00114 
00115 /* Pretty print info in debugging mode:
00116  * with and without color.
00117  * Do not concatenate __PRETTY_FUNCTION__
00118  * because it is deprecated.
00119  */
00120 #ifndef NPRETTY_COLORS
00121 #define PRINT_CINFO(COLOR,INFO)             \
00122 do {                                        \
00123   PRINT(DEFAULT_OUT, COLOR);                \
00124   PRINT(DEFAULT_OUT, __PRETTY_FUNCTION__);  \
00125   PRINT(DEFAULT_OUT, ": " INFO NORMAL "\n");\
00126 } while(0)
00127 #else
00128 #define PRINT_CINFO(COLOR,INFO) PRINT_INFO(INFO)
00129 #endif
00130 
00131 
00132 /* This is essentially to avoid warning
00133  * on printf("")
00134  */
00135 #ifndef NPRETTY_COLORS
00136 #define PRINT_COLOR(OUT, COLOR) PRINT(OUT, COLOR)
00137 #else
00138 #define PRINT_COLOR(OUT, COLOR)
00139 #endif
00140 
00141 
00142 /* Like an ordinary assert but allow
00143  * to print debug information if the
00144  * assertion is violated.
00145  */
00146 #define ASSERT(COND, PRINTME)                      \
00147 do {                                               \
00148     if (!(COND))                                   \
00149     {                                              \
00150         PRINT_COLOR(DEFAULT_ERR, RED(BOLD));       \
00151         PRINT_FILE(DEFAULT_ERR);                   \
00152         PRINT(DEFAULT_ERR, "(");                   \
00153         PRINT_INT(DEFAULT_ERR, __LINE__);          \
00154         PRINT(DEFAULT_ERR, ") ");                  \
00155         PRINT(DEFAULT_ERR, __PRETTY_FUNCTION__);   \
00156         PRINT(DEFAULT_ERR, ": Assertion `"         \
00157               MAGENTA(BOLD) #COND RED(BOLD)        \
00158               "' failed." NORMAL "\n");            \
00159         PRINTME;                                   \
00160         abort();                                   \
00161     }                                              \
00162 } while(0) /* standard trick to make it a statement */
00163 
00164 /* Like assert but controlled by another flag
00165  * to tune testing with or without expensive asserts.
00166  */
00167 #ifndef DISABLE_ASSERTX
00168 #define assertx(STATEMENT) assert(STATEMENT)
00169 #define DODEBUGX(STATEMENT) STATEMENT
00170 #else
00171 #define assertx(STATEMENT)
00172 #define DODEBUGX(STATEMENT)
00173 #endif
00174 
00175 #else /* NDEBUG */
00176 
00177 #define PRINT(OUT,S)
00178 #define PRINT_INT(OUT,N)
00179 #define PRINT_FILE(OUT)
00180 #define DODEBUG(STATEMENT)
00181 #define PRINT_INFO(INFO)
00182 #define PRINT_CINFO(COLOR,INFO)
00183 #define ASSERT(COND,PRINTME)
00184 #define PRINT_COLOR(OUT, COLOR)
00185 #define assertx(STATEMENT)
00186 #define DODEBUGX(STATEMENT)
00187 
00188 #endif /* NDEBUG */
00189 
00190 
00191 /* On Sun/Solaris, rand() is bugged,
00192  * thank you Sun! rand() returns a
00193  * random number only on 16 bits,
00194  * which is quite laughable.
00195  * It is fine to use rand() if you
00196  * are satisfied with a 16 bits number
00197  * though. This #define may be wrong
00198  * on Intel running Solaris, but it
00199  * isn't sane to do so anyway.
00200  */
00201 #if INTEL_ARCH
00202 
00203 #define RAND() rand()
00204 
00205 /* include inttypes.h for this */
00206 #define RAND64() \
00207 ((((uint64_t)rand()) << 32) | \
00208  (((uint64_t)rand())))
00209 
00210 #else
00211 
00212 #define RAND() (rand() | (rand() << 16))
00213 
00214 /* include inttypes.h for this */
00215 #define RAND64() \
00216 ((((uint64_t)rand()) << 48) | \
00217  (((uint64_t)rand()) << 32) | \
00218  (((uint64_t)rand()) << 16) | \
00219  (((uint64_t)rand())))
00220 
00221 #endif /* INTEL_ARCH */
00222 
00223 
00224 #endif /* INCLUDE_DEBUG_MACROS_H */

Generated on Fri Jun 30 00:02:51 2006 for Module debug by  doxygen 1.4.2