BsCrashHandler.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #define BS_MAX_STACKTRACE_DEPTH 200
  3. #define BS_MAX_STACKTRACE_NAME_BYTES 1024
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Saves crash data and notifies the user when a crash occurs.
  8. */
  9. // TODO - Crashes are reported in the same process as the main application. This can be a problem if the crash was caused
  10. // by heap. Any further use of the heap by the reporting methods will cause a silent crash, failing to log it. A more appropriate
  11. // way of doing it should be to resume another process to actually handle the crash.
  12. class BS_UTILITY_EXPORT CrashHandler
  13. {
  14. public:
  15. CrashHandler();
  16. ~CrashHandler();
  17. /**
  18. * @brief Constructs and starts the module.
  19. */
  20. static void startUp() { _instance() = bs_new<CrashHandler>(); }
  21. /**
  22. * @brief Shuts down this module and frees any resources it is using.
  23. */
  24. static void shutDown() { bs_delete(_instance()); }
  25. /**
  26. * @brief Returns a reference to the module instance.
  27. */
  28. static CrashHandler& instance() { return *_instance(); }
  29. /**
  30. * @brief Records a crash with a custom error message.
  31. *
  32. * @param type Type of the crash that occurred. e.g. "InvalidParameter".
  33. * @param description More detailed description of the issue that caused the crash.
  34. * @param function Optional name of the function where the error occurred.
  35. * @param file Optional name of the source code file in which the code that crashed the program exists.
  36. * @param line Optional source code line at which the crash was triggered at.
  37. */
  38. void reportCrash(const String& type, const String& description, const String& function = StringUtil::BLANK,
  39. const String& file = StringUtil::BLANK, UINT32 line = 0);
  40. #if BS_PLATFORM == BS_PLATFORM_WIN32
  41. /**
  42. * @brief Records a crash resulting from a Windows-specific SEH exception.
  43. *
  44. * @param exceptionData Exception data returned from GetExceptionInformation()
  45. *
  46. * @returns Code that signals the __except exception handler on how to proceed.
  47. */
  48. int reportCrash(void* exceptionData);
  49. #endif
  50. /**
  51. * @brief Returns a string containing a current stack trace. If function can be found in the symbol
  52. * table its readable name will be present in the stack trace, otherwise just its address.
  53. *
  54. * @returns String containing the call stack with each function on its own line.
  55. */
  56. String getStackTrace();
  57. private:
  58. /**
  59. * @brief Returns path to the folder into which to store the crash reports.
  60. */
  61. Path getCrashFolder();
  62. /**
  63. * @brief Returns a singleton instance of this module.
  64. */
  65. static CrashHandler*& _instance() { static CrashHandler* inst = nullptr; return inst; }
  66. static const wchar_t* CrashReportFolder;
  67. static const wchar_t* CrashLogName;
  68. struct Data;
  69. Data* m;
  70. };
  71. /**
  72. * @brief Returns an instance of the CrashHandler.
  73. */
  74. BS_UTILITY_EXPORT CrashHandler& gCrashHandler();
  75. }