BsCrashHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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() { _instance() = bs_new<CrashHandler>(); }
  27. /** Shuts down this module and frees any resources it is using. */
  28. static void shutDown() { bs_delete(_instance()); }
  29. /** Returns a reference to the module instance. */
  30. static CrashHandler& instance() { return *_instance(); }
  31. /**
  32. * Records a crash with a custom error message.
  33. *
  34. * @param[in] type Type of the crash that occurred. For example "InvalidParameter".
  35. * @param[in] description More detailed description of the issue that caused the crash.
  36. * @param[in] function Optional name of the function where the error occurred.
  37. * @param[in] file Optional name of the source code file in which the code that crashed the program exists.
  38. * @param[in] line Optional source code line at which the crash was triggered at.
  39. */
  40. void reportCrash(const String& type, const String& description, const String& function = StringUtil::BLANK,
  41. const String& file = StringUtil::BLANK, UINT32 line = 0) const;
  42. #if BS_PLATFORM == BS_PLATFORM_WIN32
  43. /**
  44. * Records a crash resulting from a Windows-specific SEH exception.
  45. *
  46. * @param[in] exceptionData Exception data returned from GetExceptionInformation()
  47. * @return Code that signals the __except exception handler on how to proceed.
  48. *
  49. * @note Available in Windows builds only.
  50. */
  51. int reportCrash(void* exceptionData) const;
  52. #endif
  53. /**
  54. * Returns a string containing a current stack trace. If function can be found in the symbol table its readable
  55. * name will be present in the stack trace, otherwise just its address.
  56. *
  57. * @return String containing the call stack with each function on its own line.
  58. */
  59. static String getStackTrace();
  60. private:
  61. /** Does what it says. Internal utility function used by reportCrash(). */
  62. void logErrorAndStackTrace(const String& message, const String& stackTrace) const;
  63. /** Does what it says. Internal utility function used by reportCrash(). */
  64. void logErrorAndStackTrace(const String& type,
  65. const String& description,
  66. const String& function,
  67. const String& file,
  68. UINT32 line) const;
  69. /** Does what it says. Internal utility function used by reportCrash(). */
  70. void saveCrashLog() const;
  71. /** Creates the crash report directory and returns its path. */
  72. static const Path& getCrashFolder();
  73. /** Returns the current time as a string timestamp. This is used
  74. * to name the crash report directory.. */
  75. static String getCrashTimestamp();
  76. /** Returns a singleton instance of this module. */
  77. static CrashHandler*& _instance() { static CrashHandler* inst = nullptr; return inst; }
  78. /** The name of the crash reports directory. */
  79. static const String sCrashReportFolder;
  80. /** The name of the HTML crash log file. */
  81. static const String sCrashLogName;
  82. /** Error message to display on program failure. */
  83. static const String sFatalErrorMsg;
  84. #if BS_PLATFORM == BS_PLATFORM_WIN32
  85. struct Data;
  86. Data* m;
  87. #endif
  88. };
  89. /** Easier way of accessing the CrashHandler. */
  90. BS_UTILITY_EXPORT CrashHandler& gCrashHandler();
  91. /** @} */
  92. /** @} */
  93. }