00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_DEBUG_H
00027 #define NL_DEBUG_H
00028 #include <stdio.h>
00029
00030 #include "nel/misc/common.h"
00031 #include "nel/misc/log.h"
00032 #include "nel/misc/mutex.h"
00033 #include "nel/misc/mem_displayer.h"
00034 #include "nel/misc/displayer.h"
00035
00036 namespace NLMISC
00037 {
00038
00039
00040
00041
00042
00043 extern CLog *ErrorLog;
00044 extern CLog *WarningLog;
00045 extern CLog *InfoLog;
00046 extern CLog *DebugLog;
00047 extern CLog *AssertLog;
00048
00049 extern CMemDisplayer *DefaultMemDisplayer;
00050 extern CMsgBoxDisplayer *DefaultMsgBoxDisplayer;
00051
00052
00053
00054
00055
00057 void nlFatalError (const char *format, ...);
00058
00060 void nlError (const char *format, ...);
00061
00062
00063 void createDebug (const char *logPath = NULL, bool logInFile = true);
00064
00065
00066 void enterBreakpoint (const char *message);
00067
00068
00069 extern bool DebugNeedAssert;
00070
00071
00072
00073
00087 #define nldebug NLMISC::createDebug (), NLMISC::DebugLog->setPosition( __LINE__, __FILE__ ), NLMISC::DebugLog->displayNL
00088
00089
00094 #define nlinfo NLMISC::createDebug (), NLMISC::InfoLog->setPosition( __LINE__, __FILE__ ), NLMISC::InfoLog->displayNL
00095
00096
00114 #define nlwarning NLMISC::createDebug (), NLMISC::WarningLog->setPosition( __LINE__, __FILE__ ), NLMISC::WarningLog->displayNL
00115
00116
00133 #define nlerror NLMISC::createDebug (), NLMISC::ErrorLog->setPosition( __LINE__, __FILE__ ), NLMISC::nlFatalError
00134
00135
00141 #define nlerrornoex NLMISC::createDebug (), NLMISC::ErrorLog->setPosition( __LINE__, __FILE__ ), NLMISC::nlError
00142
00143
00245
00246 #if defined (NL_OS_WINDOWS)
00247 #define NLMISC_BREAKPOINT _asm { int 3 }
00248 #else
00249 #define NLMISC_BREAKPOINT abort()
00250 #endif
00251
00252
00253
00254 #define nlassert(exp) \
00255 { \
00256 static bool ignoreNextTime = false; \
00257 if (!ignoreNextTime && !(exp)) { \
00258 NLMISC::DebugNeedAssert = false; \
00259 NLMISC::createDebug (); \
00260 if (NLMISC::DefaultMsgBoxDisplayer) \
00261 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00262 else \
00263 NLMISC::DebugNeedAssert = true; \
00264 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00265 NLMISC::AssertLog->displayNL ("\"%s\" ", #exp); \
00266 if (NLMISC::DefaultMsgBoxDisplayer) \
00267 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00268 if (NLMISC::DebugNeedAssert) \
00269 NLMISC_BREAKPOINT; \
00270 } \
00271 }
00272
00273 #define nlassertonce(exp) \
00274 { \
00275 static bool ignoreNextTime = false; \
00276 if (!ignoreNextTime && !(exp)) { \
00277 ignoreNextTime = true; \
00278 NLMISC::DebugNeedAssert = false; \
00279 NLMISC::createDebug (); \
00280 if (NLMISC::DefaultMsgBoxDisplayer) \
00281 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00282 else \
00283 NLMISC::DebugNeedAssert = true; \
00284 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00285 NLMISC::AssertLog->displayNL ("\"%s\" ", #exp); \
00286 if (NLMISC::DefaultMsgBoxDisplayer) \
00287 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00288 if (NLMISC::DebugNeedAssert) \
00289 NLMISC_BREAKPOINT; \
00290 } \
00291 }
00292
00293 #define nlassertex(exp, str) \
00294 { \
00295 static bool ignoreNextTime = false; \
00296 if (!ignoreNextTime && !(exp)) { \
00297 NLMISC::DebugNeedAssert = false; \
00298 NLMISC::createDebug (); \
00299 if (NLMISC::DefaultMsgBoxDisplayer) \
00300 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00301 else \
00302 NLMISC::DebugNeedAssert = true; \
00303 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00304 NLMISC::AssertLog->display ("\"%s\" ", #exp); \
00305 NLMISC::AssertLog->displayRawNL str; \
00306 if (NLMISC::DefaultMsgBoxDisplayer) \
00307 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00308 if (NLMISC::DebugNeedAssert) \
00309 NLMISC_BREAKPOINT; \
00310 } \
00311 }
00312
00313 #define nlverify(exp) \
00314 { \
00315 static bool ignoreNextTime = false; \
00316 if (!ignoreNextTime && !(exp)) { \
00317 NLMISC::DebugNeedAssert = false; \
00318 NLMISC::createDebug (); \
00319 if (NLMISC::DefaultMsgBoxDisplayer) \
00320 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00321 else \
00322 NLMISC::DebugNeedAssert = true; \
00323 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00324 NLMISC::AssertLog->displayNL ("\"%s\" ", #exp); \
00325 if (NLMISC::DefaultMsgBoxDisplayer) \
00326 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00327 if (NLMISC::DebugNeedAssert) \
00328 NLMISC_BREAKPOINT; \
00329 } \
00330 }
00331
00332 #define nlverifyonce(exp) \
00333 { \
00334 static bool ignoreNextTime = false; \
00335 if (!ignoreNextTime && !(exp)) { \
00336 ignoreNextTime = true; \
00337 NLMISC::DebugNeedAssert = false; \
00338 NLMISC::createDebug (); \
00339 if (NLMISC::DefaultMsgBoxDisplayer) \
00340 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00341 else \
00342 NLMISC::DebugNeedAssert = true; \
00343 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00344 NLMISC::AssertLog->displayNL ("\"%s\" ", #exp); \
00345 if (NLMISC::DefaultMsgBoxDisplayer) \
00346 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00347 if (NLMISC::DebugNeedAssert) \
00348 NLMISC_BREAKPOINT; \
00349 } \
00350 }
00351
00352 #define nlverifyex(exp, str) \
00353 { \
00354 static bool ignoreNextTime = false; \
00355 if (!ignoreNextTime && !(exp)) { \
00356 NLMISC::DebugNeedAssert = false; \
00357 NLMISC::createDebug (); \
00358 if (NLMISC::DefaultMsgBoxDisplayer) \
00359 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00360 else \
00361 NLMISC::DebugNeedAssert = true; \
00362 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00363 NLMISC::AssertLog->display ("\"%s\" ", #exp); \
00364 NLMISC::AssertLog->displayRawNL str; \
00365 if (NLMISC::DefaultMsgBoxDisplayer) \
00366 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00367 if (NLMISC::DebugNeedAssert) \
00368 NLMISC_BREAKPOINT; \
00369 } \
00370 }
00371
00372
00373
00374 #define nlstop \
00375 { \
00376 static bool ignoreNextTime = false; \
00377 if (!ignoreNextTime) { \
00378 NLMISC::DebugNeedAssert = false; \
00379 NLMISC::createDebug (); \
00380 if (NLMISC::DefaultMsgBoxDisplayer) \
00381 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00382 else \
00383 NLMISC::DebugNeedAssert = true; \
00384 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00385 NLMISC::AssertLog->displayNL ("STOP"); \
00386 if (NLMISC::DefaultMsgBoxDisplayer) \
00387 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00388 if (NLMISC::DebugNeedAssert) \
00389 NLMISC_BREAKPOINT; \
00390 } \
00391 }
00392
00393 #define nlstoponce \
00394 { \
00395 static bool ignoreNextTime = false; \
00396 if (!ignoreNextTime) { \
00397 ignoreNextTime = true; \
00398 NLMISC::DebugNeedAssert = false; \
00399 NLMISC::createDebug (); \
00400 if (NLMISC::DefaultMsgBoxDisplayer) \
00401 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00402 else \
00403 NLMISC::DebugNeedAssert = true; \
00404 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00405 NLMISC::AssertLog->displayNL ("STOP"); \
00406 if (NLMISC::DefaultMsgBoxDisplayer) \
00407 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00408 if (NLMISC::DebugNeedAssert) \
00409 NLMISC_BREAKPOINT; \
00410 } \
00411 }
00412
00413
00414 #define nlstopex(str) \
00415 { \
00416 static bool ignoreNextTime = false; \
00417 if (!ignoreNextTime) { \
00418 NLMISC::DebugNeedAssert = false; \
00419 NLMISC::createDebug (); \
00420 if (NLMISC::DefaultMsgBoxDisplayer) \
00421 NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime = ignoreNextTime; \
00422 else \
00423 NLMISC::DebugNeedAssert = true; \
00424 NLMISC::AssertLog->setPosition (__LINE__, __FILE__); \
00425 NLMISC::AssertLog->display ("STOP "); \
00426 NLMISC::AssertLog->displayRawNL str; \
00427 if (NLMISC::DefaultMsgBoxDisplayer) \
00428 ignoreNextTime = NLMISC::DefaultMsgBoxDisplayer->IgnoreNextTime; \
00429 if (NLMISC::DebugNeedAssert) \
00430 NLMISC_BREAKPOINT; \
00431 } \
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 struct EFatalError : public Exception
00461 {
00462 EFatalError() : Exception( "nlerror() called" ) {}
00463 };
00464
00465 class ETrapDebug : public Exception
00466 {
00467 };
00468
00469
00470 #ifdef assert
00471 #undef assert
00472 #endif
00473 #define assert(a) you_must_not_use_assert___use_nl_assert___read_debug_h_file
00474
00475
00477 void getCallStackAndLog (std::string &result, sint skipNFirst = 0);
00478
00483 template<class T, class U> inline T safe_cast(U o)
00484 {
00485
00486 #ifdef NL_DEBUG
00487 nlassert(dynamic_cast<T>(o));
00488 #endif
00489 return static_cast<T>(o);
00490 }
00491
00494 #define nlctassert(cond) sizeof(uint[(cond) ? 1 : 0]);
00495
00496 }
00497
00498
00499 #endif // NL_DEBUG_H
00500
00501