BsCrashHandler.h 4.4 KB

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