BsCrashHandler.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 BansheeEngine
  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 appropriate
  17. // way of doing it should be to resume another process to actually handle the crash.
  18. class BS_UTILITY_EXPORT CrashHandler
  19. {
  20. public:
  21. CrashHandler();
  22. ~CrashHandler();
  23. /** Constructs and starts the module. */
  24. static void startUp() { _instance() = bs_new<CrashHandler>(); }
  25. /** Shuts down this module and frees any resources it is using. */
  26. static void shutDown() { bs_delete(_instance()); }
  27. /** Returns a reference to the module instance. */
  28. static CrashHandler& instance() { return *_instance(); }
  29. /**
  30. * Records a crash with a custom error message.
  31. *
  32. * @param[in] type Type of the crash that occurred. For example "InvalidParameter".
  33. * @param[in] description More detailed description of the issue that caused the crash.
  34. * @param[in] function Optional name of the function where the error occurred.
  35. * @param[in] file Optional name of the source code file in which the code that crashed the program exists.
  36. * @param[in] 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) const;
  40. #if BS_PLATFORM == BS_PLATFORM_WIN32
  41. /**
  42. * Records a crash resulting from a Windows-specific SEH exception.
  43. *
  44. * @param[in] exceptionData Exception data returned from GetExceptionInformation()
  45. * @return Code that signals the __except exception handler on how to proceed.
  46. *
  47. * @note Available in Windows builds only.
  48. */
  49. int reportCrash(void* exceptionData) const;
  50. #endif
  51. /**
  52. * Returns a string containing a current stack trace. If function can be found in the symbol table its readable
  53. * name will be present in the stack trace, otherwise just its address.
  54. *
  55. * @return String containing the call stack with each function on its own line.
  56. */
  57. static String getStackTrace();
  58. private:
  59. /** Returns path to the folder into which to store the crash reports. */
  60. Path getCrashFolder() const;
  61. /** Returns a singleton instance of this module. */
  62. static CrashHandler*& _instance() { static CrashHandler* inst = nullptr; return inst; }
  63. static const wchar_t* CrashReportFolder;
  64. static const wchar_t* CrashLogName;
  65. struct Data;
  66. Data* m;
  67. };
  68. /** Easier way of accessing the CrashHandler. */
  69. BS_UTILITY_EXPORT CrashHandler& gCrashHandler();
  70. /** @} */
  71. /** @} */
  72. }