BsCrashHandler.h 2.4 KB

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