2
0

BsCrashHandler.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Prerequisites/BsPrerequisitesUtil.h"
  4. #include "Debug/BsDebug.h"
  5. #include "FileSystem/BsFileSystem.h"
  6. #include "FileSystem/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 = FileSystem::getWorkingDirectoryPath() + sCrashReportFolder +
  20. getCrashTimestamp();
  21. static bool first = true;
  22. if (first)
  23. {
  24. FileSystem::createDir(path);
  25. first = false;
  26. }
  27. return path;
  28. }
  29. void CrashHandler::logErrorAndStackTrace(const String& errorMsg, const String& stackTrace) const
  30. {
  31. StringStream errorMessage;
  32. errorMessage << sFatalErrorMsg << std::endl;
  33. errorMessage << errorMsg;
  34. errorMessage << "\n\nStack trace: \n";
  35. errorMessage << stackTrace;
  36. gDebug().logError(errorMessage.str());
  37. }
  38. void CrashHandler::logErrorAndStackTrace(const String& type, const String& description, const String& function,
  39. const String& file, UINT32 line) const
  40. {
  41. StringStream errorMessage;
  42. errorMessage << " - Error: " << type << std::endl;
  43. errorMessage << " - Description: " << description << std::endl;
  44. errorMessage << " - In function: " << function << std::endl;
  45. errorMessage << " - In file: " << file << ":" << line;
  46. logErrorAndStackTrace(errorMessage.str(), getStackTrace());
  47. }
  48. void CrashHandler::saveCrashLog() const
  49. {
  50. gDebug().saveLog(getCrashFolder() + toWString(sCrashLogName));
  51. }
  52. }