BsCrashHandler.h 3.0 KB

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