BsCrashHandler.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrerequisitesUtil.h"
  4. #include "BsDebug.h"
  5. #include "BsFileSystem.h"
  6. #include "BsPath.h"
  7. namespace bs
  8. {
  9. const String CrashHandler::sCrashReportFolder = "CrashReports";
  10. const String CrashHandler::sCrashLogName = "log.html";
  11. const String CrashHandler::sFatalErrorMsg =
  12. "A fatal error occurred and the program has to terminate!";
  13. CrashHandler& gCrashHandler()
  14. {
  15. return CrashHandler::instance();
  16. }
  17. const Path& CrashHandler::getCrashFolder()
  18. {
  19. static const Path path =
  20. FileSystem::getWorkingDirectoryPath()
  21. + Path(sCrashReportFolder)
  22. + Path(getCrashTimestamp());
  23. static bool first = true;
  24. if (first) {
  25. FileSystem::createDir(path);
  26. first = false;
  27. }
  28. return path;
  29. }
  30. void CrashHandler::logErrorAndStackTrace(const String& errorMsg,
  31. const String& stackTrace) const
  32. {
  33. StringStream errorMessage;
  34. errorMessage << sFatalErrorMsg << std::endl;
  35. errorMessage << errorMsg;
  36. errorMessage << "\n\nStack trace: \n";
  37. errorMessage << stackTrace;
  38. gDebug().logError(errorMessage.str());
  39. }
  40. void CrashHandler::logErrorAndStackTrace(const String& type,
  41. const String& description,
  42. const String& function,
  43. const String& file,
  44. UINT32 line) const
  45. {
  46. StringStream errorMessage;
  47. errorMessage << " - Error: " << type << std::endl;
  48. errorMessage << " - Description: " << description << std::endl;
  49. errorMessage << " - In function: " << function << std::endl;
  50. errorMessage << " - In file: " << file << ":" << line;
  51. logErrorAndStackTrace(errorMessage.str(), getStackTrace());
  52. }
  53. void CrashHandler::saveCrashLog() const
  54. {
  55. gDebug().saveLog(getCrashFolder() + toWString(sCrashLogName));
  56. }
  57. }