platformAssert.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ignoreAll = 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. static const 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 = false;
  87. // always dump to the Assert to the Console
  88. if (Con::isActive())
  89. {
  90. if (assertType == Warning)
  91. Con::warnf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], message);
  92. else
  93. Con::errorf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], 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. if( !ignoreAll )
  104. {
  105. // Display message box with Debug, Ignore, Ignore All, and Exit options
  106. switch( Platform::AlertAssert(buffer, message) )
  107. {
  108. case Platform::ALERT_ASSERT_DEBUG:
  109. ret = true;
  110. break;
  111. case Platform::ALERT_ASSERT_IGNORE:
  112. ret = false;
  113. break;
  114. case Platform::ALERT_ASSERT_IGNORE_ALL:
  115. ignoreAll = true;
  116. ret = false;
  117. break;
  118. default:
  119. case Platform::ALERT_ASSERT_EXIT:
  120. Platform::forceShutdown(1);
  121. break;
  122. }
  123. }
  124. }
  125. processing = false;
  126. return ret;
  127. }
  128. bool PlatformAssert::processingAssert()
  129. {
  130. return platformAssert ? platformAssert->processing : false;
  131. }
  132. //--------------------------------------
  133. bool PlatformAssert::processAssert(Type assertType,
  134. const char *filename,
  135. U32 lineNumber,
  136. const char *message)
  137. {
  138. if (platformAssert)
  139. return platformAssert->process(assertType, filename, lineNumber, message);
  140. else // when platAssert NULL (during _start/_exit) try direct output...
  141. dPrintf("\n%s: (%s @ %ld) %s\n", typeName[assertType], filename, lineNumber, message);
  142. // this could also be platform-specific: OutputDebugString on PC, DebugStr on Mac.
  143. // Will raw printfs do the job? In the worst case, it's a break-pointable line of code.
  144. // would have preferred Con but due to race conditions, it might not be around...
  145. // Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  146. return true;
  147. }
  148. //--------------------------------------
  149. const char* avar(const char *message, ...)
  150. {
  151. static char buffer[4096];
  152. va_list args;
  153. va_start(args, message);
  154. dVsprintf(buffer, sizeof(buffer), message, args);
  155. va_end(args);
  156. return( buffer );
  157. }
  158. //-----------------------------------------------------------------------------
  159. ConsoleFunction( Assert, void, 3, 3, "(condition, message) - Fatal Script Assertion" )
  160. {
  161. // Process Assertion.
  162. AssertISV( dAtob(argv[1]), argv[2] );
  163. }