BsCrashHandler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #define BS_MAX_STACKTRACE_DEPTH 200
  5. #define BS_MAX_STACKTRACE_NAME_BYTES 1024
  6. namespace bs
  7. {
  8. /** @addtogroup Internal-Utility
  9. * @{
  10. */
  11. /** @addtogroup Error-Internal
  12. * @{
  13. */
  14. /** Saves crash data and notifies the user when a crash occurs. */
  15. // TODO - Crashes are reported in the same process as the main application. This can be a problem if the crash was caused
  16. // by heap. Any further use of the heap by the reporting methods will cause a silent crash, failing to log it. A more
  17. // appropriate way of doing it should be to resume another process to actually handle the crash.
  18. // - Perhaps an even better option would be to use a private heap for all engine allocations. So when corruptions does
  19. // happen the crash handler can use the default heap with no issues.
  20. class BS_UTILITY_EXPORT CrashHandler
  21. {
  22. public:
  23. CrashHandler();
  24. ~CrashHandler();
  25. /** Constructs and starts the module. */
  26. static void startUp()
  27. {
  28. if(_instance() == nullptr)
  29. _instance() = bs_new<CrashHandler>();
  30. }
  31. /** Shuts down this module and frees any resources it is using. */
  32. static void shutDown()
  33. {
  34. if(_instance() != nullptr)
  35. {
  36. bs_delete(_instance());
  37. _instance() = nullptr;
  38. }
  39. }
  40. /** Returns a reference to the module instance. */
  41. static CrashHandler& instance() { return *_instance(); }
  42. /**
  43. * Records a crash with a custom error message.
  44. *
  45. * @param[in] type Type of the crash that occurred. For example "InvalidParameter".
  46. * @param[in] description More detailed description of the issue that caused the crash.
  47. * @param[in] function Optional name of the function where the error occurred.
  48. * @param[in] file Optional name of the source code file in which the code that crashed the program exists.
  49. * @param[in] line Optional source code line at which the crash was triggered at.
  50. */
  51. void reportCrash(const String& type, const String& description, const String& function = StringUtil::BLANK,
  52. const String& file = StringUtil::BLANK, UINT32 line = 0) const;
  53. #if BS_PLATFORM == BS_PLATFORM_WIN32
  54. /**
  55. * Records a crash resulting from a Windows-specific SEH exception.
  56. *
  57. * @param[in] exceptionData Exception data returned from GetExceptionInformation()
  58. * @return Code that signals the __except exception handler on how to proceed.
  59. *
  60. * @note Available in Windows builds only.
  61. */
  62. int reportCrash(void* exceptionData) const;
  63. #endif
  64. /**
  65. * Returns a string containing a current stack trace. If function can be found in the symbol table its readable
  66. * name will be present in the stack trace, otherwise just its address.
  67. *
  68. * @return String containing the call stack with each function on its own line.
  69. */
  70. static String getStackTrace();
  71. private:
  72. /** Does what it says. Internal utility function used by reportCrash(). */
  73. void logErrorAndStackTrace(const String& message, const String& stackTrace) const;
  74. /** Does what it says. Internal utility function used by reportCrash(). */
  75. void logErrorAndStackTrace(const String& type,
  76. const String& description,
  77. const String& function,
  78. const String& file,
  79. UINT32 line) const;
  80. /** Does what it says. Internal utility function used by reportCrash(). */
  81. void saveCrashLog() const;
  82. /** Creates the crash report directory and returns its path. */
  83. static const Path& getCrashFolder();
  84. /** Returns the current time as a string timestamp. This is used
  85. * to name the crash report directory.. */
  86. static String getCrashTimestamp();
  87. /** Returns a singleton instance of this module. */
  88. static CrashHandler*& _instance() { static CrashHandler* inst = nullptr; return inst; }
  89. /** The name of the crash reports directory. */
  90. static const String sCrashReportFolder;
  91. /** The name of the HTML crash log file. */
  92. static const String sCrashLogName;
  93. /** Error message to display on program failure. */
  94. static const String sFatalErrorMsg;
  95. #if BS_PLATFORM == BS_PLATFORM_WIN32
  96. struct Data;
  97. Data* m;
  98. #endif
  99. };
  100. /** Easier way of accessing the CrashHandler. */
  101. BS_UTILITY_EXPORT CrashHandler& gCrashHandler();
  102. /** @} */
  103. /** @} */
  104. }