platformAssert.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/engineAPI.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. bool PlatformAssert::process(Type assertType,
  61. const char *filename,
  62. U32 lineNumber,
  63. const char *message)
  64. {
  65. // If we're somehow recursing, just die.
  66. if(processing)
  67. Platform::debugBreak();
  68. processing = true;
  69. bool ret = false;
  70. // always dump to the Assert to the Console
  71. if (Con::isActive())
  72. {
  73. if (assertType == Warning)
  74. Con::warnf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], message);
  75. else
  76. Con::errorf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], message);
  77. }
  78. // if not a WARNING pop-up a dialog box
  79. if (assertType != Warning)
  80. {
  81. // used for processing navGraphs (an assert won't botch the whole build)
  82. if(Con::getBoolVariable("$FP::DisableAsserts", false) == true)
  83. Platform::forceShutdown(1);
  84. char buffer[2048];
  85. dSprintf(buffer, 2048, "%s: (%s @ %ld)", typeName[assertType], filename, lineNumber);
  86. if( !ignoreAll )
  87. {
  88. // Display message box with Debug, Ignore, Ignore All, and Exit options
  89. switch( Platform::AlertAssert(buffer, message) )
  90. {
  91. case Platform::ALERT_ASSERT_DEBUG:
  92. ret = true;
  93. break;
  94. case Platform::ALERT_ASSERT_IGNORE:
  95. ret = false;
  96. break;
  97. case Platform::ALERT_ASSERT_IGNORE_ALL:
  98. ignoreAll = true;
  99. ret = false;
  100. break;
  101. default:
  102. case Platform::ALERT_ASSERT_EXIT:
  103. Platform::forceShutdown(1);
  104. break;
  105. }
  106. }
  107. }
  108. processing = false;
  109. return ret;
  110. }
  111. bool PlatformAssert::processingAssert()
  112. {
  113. return platformAssert ? platformAssert->processing : false;
  114. }
  115. //--------------------------------------
  116. bool PlatformAssert::processAssert(Type assertType,
  117. const char *filename,
  118. U32 lineNumber,
  119. const char *message)
  120. {
  121. if (platformAssert)
  122. return platformAssert->process(assertType, filename, lineNumber, message);
  123. else // when platAssert NULL (during _start/_exit) try direct output...
  124. dPrintf("\n%s: (%s @ %ld) %s\n", typeName[assertType], filename, lineNumber, message);
  125. // this could also be platform-specific: OutputDebugString on PC, DebugStr on Mac.
  126. // Will raw printfs do the job? In the worst case, it's a break-pointable line of code.
  127. // would have preferred Con but due to race conditions, it might not be around...
  128. // Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
  129. return true;
  130. }
  131. //--------------------------------------
  132. const char* avar(const char *message, ...)
  133. {
  134. static char buffer[4096];
  135. va_list args;
  136. va_start(args, message);
  137. dVsprintf(buffer, sizeof(buffer), message, args);
  138. va_end(args);
  139. return( buffer );
  140. }
  141. //-----------------------------------------------------------------------------
  142. DefineEngineFunction(Assert, void, (bool condition, const char* message),, "Fatal Script Assertion")
  143. {
  144. // Process Assertion.
  145. AssertISV(condition, message);
  146. }