platformAssert.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platformAssert.h"
  23. #include "console/console.h"
  24. #include <stdarg.h>
  25. #include "platformAssert_ScriptBinding.h"
  26. //-------------------------------------- STATIC Declaration
  27. PlatformAssert *PlatformAssert::platformAssert = NULL;
  28. //--------------------------------------
  29. PlatformAssert::PlatformAssert()
  30. {
  31. processing = false;
  32. }
  33. //--------------------------------------
  34. PlatformAssert::~PlatformAssert()
  35. {
  36. }
  37. //--------------------------------------
  38. void PlatformAssert::create( PlatformAssert* newAssertClass )
  39. {
  40. if (!platformAssert)
  41. platformAssert = newAssertClass ? newAssertClass : new PlatformAssert;
  42. }
  43. //--------------------------------------
  44. void PlatformAssert::destroy()
  45. {
  46. if (platformAssert)
  47. delete platformAssert;
  48. platformAssert = NULL;
  49. }
  50. //--------------------------------------
  51. bool PlatformAssert::displayMessageBox(const char *title, const char *message, bool retry)
  52. {
  53. if (retry)
  54. return Platform::AlertRetry(title, message);
  55. Platform::AlertOK(title, message);
  56. return false;
  57. }
  58. const static char *typeName[] = { "Unknown", "Fatal-ISV", "Fatal", "Warning" };
  59. //------------------------------------------------------------------------------
  60. static bool askToEnterDebugger(const char* message )
  61. {
  62. static bool haveAsked = false;
  63. static bool useDebugger = true;
  64. if(!haveAsked )
  65. {
  66. static char tempBuff[1024];
  67. dSprintf( tempBuff, 1024, "Torque has encountered an assertion with message\n\n"
  68. "%s\n\n"
  69. "Would you like to use the debugger? If you cancel, you won't be asked"
  70. " again until you restart Torque.", message);
  71. useDebugger = Platform::AlertOKCancel("Use debugger?", tempBuff );
  72. haveAsked = true;
  73. }
  74. return useDebugger;
  75. }
  76. //--------------------------------------
  77. bool PlatformAssert::process(Type assertType,
  78. const char *filename,
  79. U32 lineNumber,
  80. const char *message)
  81. {
  82. // If we're somehow recursing, just die.
  83. if(processing)
  84. Platform::debugBreak();
  85. processing = true;
  86. bool ret = true;
  87. // always dump to the Assert to the Console
  88. if (Con::isActive())
  89. {
  90. if (assertType == Warning)
  91. Con::warnf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  92. else
  93. Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  94. }
  95. // if not a WARNING pop-up a dialog box
  96. if (assertType != Warning)
  97. {
  98. // used for processing navGraphs (an assert won't botch the whole build)
  99. if(Con::getBoolVariable("$FP::DisableAsserts", false) == true)
  100. Platform::forceShutdown(1);
  101. char buffer[2048];
  102. dSprintf(buffer, 2048, "%s: (%s @ %ld)", typeName[assertType], filename, lineNumber);
  103. #ifdef TORQUE_DEBUG
  104. // In debug versions, allow a retry even for ISVs...
  105. bool retry = displayMessageBox(buffer, message, true);
  106. #else
  107. bool retry = displayMessageBox(buffer, message, ((assertType == Fatal) ? true : false) );
  108. #endif
  109. if(!retry)
  110. Platform::forceShutdown(1);
  111. ret = askToEnterDebugger(message);
  112. }
  113. processing = false;
  114. return ret;
  115. }
  116. bool PlatformAssert::processingAssert()
  117. {
  118. return platformAssert ? platformAssert->processing : false;
  119. }
  120. //--------------------------------------
  121. bool PlatformAssert::processAssert(Type assertType,
  122. const char *filename,
  123. U32 lineNumber,
  124. const char *message)
  125. {
  126. if (platformAssert)
  127. return platformAssert->process(assertType, filename, lineNumber, message);
  128. else // when platAssert NULL (during _start/_exit) try direct output...
  129. dPrintf("\n%s: (%s @ %ld) %s\n", typeName[assertType], filename, lineNumber, message);
  130. // this could also be platform-specific: OutputDebugString on PC, DebugStr on Mac.
  131. // Will raw printfs do the job? In the worst case, it's a break-pointable line of code.
  132. // would have preferred Con but due to race conditions, it might not be around...
  133. // Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  134. return true;
  135. }
  136. //--------------------------------------
  137. const char* avar(const char *message, ...)
  138. {
  139. static char buffer[4096];
  140. va_list args;
  141. va_start(args, message);
  142. dVsprintf(buffer, sizeof(buffer), message, args);
  143. return( buffer );
  144. }