platformAssert.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdarg.h>
  23. #include "core/strings/stringFunctions.h"
  24. #include "console/console.h"
  25. //-------------------------------------- STATIC Declaration
  26. PlatformAssert *PlatformAssert::platformAssert = NULL;
  27. //--------------------------------------
  28. PlatformAssert::PlatformAssert()
  29. {
  30. processing = false;
  31. }
  32. //--------------------------------------
  33. PlatformAssert::~PlatformAssert()
  34. {
  35. }
  36. //--------------------------------------
  37. void PlatformAssert::create( PlatformAssert* newAssertClass )
  38. {
  39. if (!platformAssert)
  40. platformAssert = newAssertClass ? newAssertClass : new PlatformAssert;
  41. }
  42. //--------------------------------------
  43. void PlatformAssert::destroy()
  44. {
  45. if (platformAssert)
  46. delete platformAssert;
  47. platformAssert = NULL;
  48. }
  49. //--------------------------------------
  50. bool PlatformAssert::displayMessageBox(const char *title, const char *message, bool retry)
  51. {
  52. if (retry)
  53. return Platform::AlertRetry(title, message);
  54. Platform::AlertOK(title, message);
  55. return false;
  56. }
  57. static const char *typeName[] = { "Unknown", "Fatal-ISV", "Fatal", "Warning" };
  58. //------------------------------------------------------------------------------
  59. static bool askToEnterDebugger(const char* message )
  60. {
  61. static bool haveAsked = false;
  62. static bool useDebugger = true;
  63. if(!haveAsked )
  64. {
  65. static char tempBuff[1024];
  66. dSprintf( tempBuff, 1024, "Torque has encountered an assertion with message\n\n"
  67. "%s\n\n"
  68. "Would you like to use the debugger? If you cancel, you won't be asked"
  69. " again until you restart Torque.", message);
  70. useDebugger = Platform::AlertOKCancel("Use debugger?", tempBuff );
  71. haveAsked = true;
  72. }
  73. return useDebugger;
  74. }
  75. //--------------------------------------
  76. bool PlatformAssert::process(Type assertType,
  77. const char *filename,
  78. U32 lineNumber,
  79. const char *message)
  80. {
  81. // If we're somehow recursing, just die.
  82. if(processing)
  83. Platform::debugBreak();
  84. processing = true;
  85. bool ret = true;
  86. // always dump to the Assert to the Console
  87. if (Con::isActive())
  88. {
  89. if (assertType == Warning)
  90. Con::warnf(ConsoleLogEntry::Assert, "%s(%ld) : %s - %s", filename, lineNumber, typeName[assertType], message);
  91. else
  92. Con::errorf(ConsoleLogEntry::Assert, "%s(%ld) : %s - %s", filename, lineNumber, typeName[assertType], message);
  93. }
  94. // if not a WARNING pop-up a dialog box
  95. if (assertType != Warning)
  96. {
  97. // used for processing navGraphs (an assert won't botch the whole build)
  98. if(Con::getBoolVariable("$FP::DisableAsserts", false) == true)
  99. Platform::forceShutdown(1);
  100. char buffer[2048];
  101. dSprintf(buffer, 2048, "%s(%ld) : %s", filename, lineNumber, typeName[assertType] );
  102. #ifdef TORQUE_DEBUG
  103. // In debug versions, allow a retry even for ISVs...
  104. bool retry = displayMessageBox(buffer, message, true);
  105. #else
  106. bool retry = displayMessageBox(buffer, message, ((assertType == Fatal) ? true : false) );
  107. #endif
  108. if(!retry)
  109. Platform::forceShutdown(1);
  110. ret = askToEnterDebugger(message);
  111. }
  112. processing = false;
  113. return ret;
  114. }
  115. bool PlatformAssert::processingAssert()
  116. {
  117. return platformAssert ? platformAssert->processing : false;
  118. }
  119. //--------------------------------------
  120. bool PlatformAssert::processAssert(Type assertType,
  121. const char *filename,
  122. U32 lineNumber,
  123. const char *message)
  124. {
  125. if (platformAssert)
  126. return platformAssert->process(assertType, filename, lineNumber, message);
  127. else // when platAssert NULL (during _start/_exit) try direct output...
  128. dPrintf("\n%s: (%s @ %ld) %s\n", typeName[assertType], filename, lineNumber, message);
  129. // this could also be platform-specific: OutputDebugString on PC, DebugStr on Mac.
  130. // Will raw printfs do the job? In the worst case, it's a break-pointable line of code.
  131. // would have preferred Con but due to race conditions, it might not be around...
  132. // Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  133. return true;
  134. }
  135. //--------------------------------------
  136. const char* avar(const char *message, ...)
  137. {
  138. static char buffer[4096];
  139. va_list args;
  140. va_start(args, message);
  141. dVsprintf(buffer, sizeof(buffer), message, args);
  142. return( buffer );
  143. }