00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _cvc3__debug_h
00023 #define _cvc3__debug_h
00024
00025 #include <string>
00026 #include <iostream>
00027 #include <sstream>
00028 #include <vector>
00029 #include "os.h"
00030 #include "exception.h"
00031
00032
00033
00034
00035
00036
00037 #define FatalAssert(cond, msg) if(!(cond)) \
00038 CVC3::fatalError(__FILE__, __LINE__, #cond, msg)
00039
00040 namespace CVC3 {
00041
00042
00043
00044 extern CVC_DLL void fatalError(const std::string& file, int line,
00045 const std::string& cond, const std::string& msg);
00046 }
00047
00048 #ifdef _CVC3_DEBUG_MODE
00049
00050 #include "compat_hash_map.h"
00051 #include "compat_hash_set.h"
00052
00053
00054 #define IF_DEBUG(code) code
00055
00056
00057
00058
00059 #define DBG_PRINT(cond, pre, v, post) if(cond) CVC3::debugger.getOS() \
00060 << (pre) << (v) << (post) << std::endl
00061
00062
00063 #define DBG_PRINT_MSG(cond, msg) \
00064 if(cond) CVC3::debugger.getOS() << (msg) << std::endl
00065
00066
00067
00068 #define TRACE(flag, pre, v, post) \
00069 DBG_PRINT(CVC3::debugger.trace(flag), pre, v, post)
00070
00071
00072 #define TRACE_MSG(flag, msg) \
00073 DBG_PRINT_MSG(CVC3::debugger.trace(flag), msg)
00074
00075
00076 #define DebugAssert(cond, str) if(!(cond)) \
00077 CVC3::debugError(__FILE__, __LINE__, #cond, str)
00078
00079
00080 namespace CVC3 {
00081
00082 class Expr;
00083
00084 class DebugException: public Exception {
00085 public:
00086
00087 DebugException(const std::string& msg): Exception(msg) { }
00088
00089 virtual std::string toString() const {
00090 return "Assertion violation " + d_msg;
00091 }
00092 };
00093
00094
00095
00096 extern CVC_DLL void debugError(const std::string& file, int line,
00097 const std::string& cond, const std::string& msg);
00098
00099
00100
00101
00102
00103 class DebugFlag {
00104 private:
00105 bool* d_flag;
00106 public:
00107
00108
00109 DebugFlag(bool& flag) : d_flag(&flag) { }
00110
00111 ~DebugFlag() { }
00112
00113 operator bool() { return *d_flag; }
00114
00115
00116
00117 bool operator--() { *d_flag = false; return false; }
00118 bool operator++() { *d_flag = true; return true; }
00119
00120 bool operator--(int) { bool x=*d_flag; *d_flag=false; return x; }
00121 bool operator++(int) { bool x=*d_flag; *d_flag=true; return x; }
00122
00123 DebugFlag& operator=(bool x) { *d_flag=(x!=false); return *this; }
00124
00125 friend bool operator==(const DebugFlag& f1, const DebugFlag& f2);
00126 friend bool operator!=(const DebugFlag& f1, const DebugFlag& f2);
00127
00128 friend std::ostream& operator<<(std::ostream& os, const DebugFlag& f);
00129 };
00130
00131
00132 inline bool operator==(const DebugFlag& f1, const DebugFlag& f2) {
00133 return (*f1.d_flag) == (*f2.d_flag);
00134 }
00135
00136 inline bool operator!=(const DebugFlag& f1, const DebugFlag& f2) {
00137 return (*f1.d_flag) != (*f2.d_flag);
00138 }
00139
00140 inline std::ostream& operator<<(std::ostream& os, const DebugFlag& f) {
00141 if(*f.d_flag) return(os << "true");
00142 else return(os << "false");
00143 }
00144
00145
00146
00147
00148
00149 class DebugCounter {
00150 private:
00151 int* d_counter;
00152 public:
00153
00154
00155
00156 DebugCounter(int& c) : d_counter(&c) { }
00157
00158 ~DebugCounter() { }
00159
00160
00161
00162 operator int() { return *d_counter; }
00163
00164
00165
00166
00167 int operator--() { return --(*d_counter); }
00168
00169 int operator++() { return ++(*d_counter); }
00170
00171 int operator--(int) { return (*d_counter)--; }
00172
00173 int operator++(int) { return (*d_counter)++; }
00174
00175 DebugCounter& operator=(int x) { *d_counter=x; return *this; }
00176 DebugCounter& operator+=(int x) { *d_counter+=x; return *this; }
00177 DebugCounter& operator-=(int x) { *d_counter-=x; return *this; }
00178
00179
00180 DebugCounter& operator=(const DebugCounter& x)
00181 { *d_counter=*x.d_counter; return *this; }
00182
00183 DebugCounter& operator-=(const DebugCounter& x)
00184 { *d_counter-=*x.d_counter; return *this; }
00185
00186 DebugCounter& operator+=(const DebugCounter& x)
00187 { *d_counter+=*x.d_counter; return *this; }
00188
00189 friend bool operator==(const DebugCounter& c1, const DebugCounter& c2);
00190 friend bool operator!=(const DebugCounter& c1, const DebugCounter& c2);
00191 friend bool operator==(int c1, const DebugCounter& c2);
00192 friend bool operator!=(int c1, const DebugCounter& c2);
00193 friend bool operator==(const DebugCounter& c1, int c2);
00194 friend bool operator!=(const DebugCounter& c1, int c2);
00195
00196 friend std::ostream& operator<<(std::ostream& os, const DebugCounter& f);
00197 };
00198
00199 inline bool operator==(const DebugCounter& c1, const DebugCounter& c2) {
00200 return (*c1.d_counter) == (*c2.d_counter);
00201 }
00202 inline bool operator!=(const DebugCounter& c1, const DebugCounter& c2) {
00203 return (*c1.d_counter) != (*c2.d_counter);
00204 }
00205 inline bool operator==(int c1, const DebugCounter& c2) {
00206 return c1 == (*c2.d_counter);
00207 }
00208 inline bool operator!=(int c1, const DebugCounter& c2) {
00209 return c1 != (*c2.d_counter);
00210 }
00211 inline bool operator==(const DebugCounter& c1, int c2) {
00212 return (*c1.d_counter) == c2;
00213 }
00214 inline bool operator!=(const DebugCounter& c1, int c2) {
00215 return (*c1.d_counter) != c2;
00216 }
00217 inline std::ostream& operator<<(std::ostream& os, const DebugCounter& c) {
00218 return (os << *c.d_counter);
00219 }
00220
00221
00222
00223
00224
00225 class DebugTime;
00226
00227
00228
00229
00230
00231 class CVC_DLL DebugTimer {
00232 private:
00233 DebugTime* d_time;
00234 bool d_clean_time;
00235 public:
00236
00237
00238
00239 DebugTimer(DebugTime* time, bool take_time = false)
00240 : d_time(time), d_clean_time(take_time) { }
00241
00242
00243
00244
00245
00246
00247
00248 DebugTimer(const DebugTimer& timer);
00249
00250 DebugTimer& operator=(const DebugTimer& timer);
00251
00252
00253 ~DebugTimer();
00254
00255
00256
00257 void reset();
00258 DebugTimer& operator+=(const DebugTimer& timer);
00259 DebugTimer& operator-=(const DebugTimer& timer);
00260
00261 DebugTimer operator+(const DebugTimer& timer);
00262
00263 DebugTimer operator-(const DebugTimer& timer);
00264
00265
00266 friend class Debug;
00267
00268 friend bool operator==(const DebugTimer& t1, const DebugTimer& t2);
00269 friend bool operator!=(const DebugTimer& t1, const DebugTimer& t2);
00270 friend bool operator<(const DebugTimer& t1, const DebugTimer& t2);
00271 friend bool operator>(const DebugTimer& t1, const DebugTimer& t2);
00272 friend bool operator<=(const DebugTimer& t1, const DebugTimer& t2);
00273 friend bool operator>=(const DebugTimer& t1, const DebugTimer& t2);
00274
00275
00276 friend std::ostream& operator<<(std::ostream& os, const DebugTimer& timer);
00277 };
00278
00279
00280
00281
00282
00283
00284
00285 class CVC_DLL Debug {
00286 private:
00287
00288 const std::vector<std::pair<std::string,bool> >* d_traceOptions;
00289
00290 const std::string* d_dumpName;
00291
00292 std::ostream* d_os;
00293
00294 std::ostream* d_osDumpTrace;
00295
00296 class stringHash {
00297 public:
00298 size_t operator()(const std::string& s) const {
00299 std::hash<char*> h;
00300 return h(s.c_str());
00301 }
00302 };
00303
00304 typedef std::hash_map<std::string, bool, stringHash> FlagMap;
00305 typedef std::hash_map<std::string, int, stringHash> CounterMap;
00306 typedef std::hash_map<std::string, DebugTime*, stringHash> TimerMap;
00307 FlagMap d_flags;
00308 FlagMap d_traceFlags;
00309 CounterMap d_counters;
00310
00311
00312
00313 TimerMap d_timers;
00314
00315 public:
00316
00317 Debug(): d_traceOptions(NULL), d_os(&std::cerr), d_osDumpTrace(NULL) { }
00318
00319 ~Debug();
00320
00321 void init(const std::vector<std::pair<std::string,bool> >* traceOptions,
00322 const std::string* dumpName);
00323
00324 void finalize();
00325
00326
00327
00328 DebugFlag flag(const std::string& name)
00329 { return DebugFlag(d_flags[name]); }
00330
00331
00332
00333 DebugFlag traceFlag(const std::string& name)
00334 { return DebugFlag(d_traceFlags[name]); }
00335
00336 DebugFlag traceFlag(const char* name);
00337
00338 void traceAll(bool enable = true);
00339
00340
00341 DebugCounter counter(const std::string& name)
00342 { return DebugCounter(d_counters[name]); }
00343
00344
00345 DebugTimer timer(const std::string& name);
00346
00347
00348
00349
00350 bool trace(const std::string& name);
00351
00352
00353
00354
00355
00356
00357
00358
00359 static DebugTimer newTimer();
00360
00361
00362 void setCurrentTime(DebugTimer& timer);
00363 void setCurrentTime(const std::string& name) {
00364 DebugTimer t(timer(name));
00365 setCurrentTime(t);
00366 }
00367
00368
00369
00370
00371
00372 void setElapsed(DebugTimer& timer);
00373
00374
00375 std::ostream& getOS() { return *d_os; }
00376
00377 std::ostream& getOSDumpTrace();
00378
00379
00380 void dumpTrace(const std::string& title,
00381 const std::vector<std::pair<std::string,std::string> >&
00382 fields);
00383
00384 void setOS(std::ostream& os) { d_os = &os; }
00385
00386
00387 void printAll(std::ostream& os);
00388
00389
00390 void printAll() { printAll(*d_os); }
00391
00392
00393
00394 int scopeLevel();
00395
00396 };
00397
00398 extern CVC_DLL Debug debugger;
00399
00400 }
00401
00402 #else // if _CVC3_DEBUG_MODE is not defined
00403
00404
00405
00406 #define IF_DEBUG(code)
00407
00408 #define DebugAssert(cond, str)
00409
00410 #define DBG_PRINT(cond, pre, v, post)
00411 #define TRACE(cond, pre, v, post)
00412
00413 #define DBG_PRINT_MSG(cond, msg)
00414 #define TRACE_MSG(flag, msg)
00415
00416
00417 namespace CVC3 {
00418 class DebugException: public Exception { };
00419 }
00420
00421 #endif // _CVC3_DEBUG_MODE
00422
00423 #include "cvc_util.h"
00424
00425 #endif // _cvc3__debug_h