| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 4 | #include "Debug/BsDebug.h" |
| 5 | #include "FileSystem/BsFileSystem.h" |
| 6 | #include "FileSystem/BsPath.h" |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | const String CrashHandler::sCrashReportFolder = "CrashReports"; |
| 11 | const String CrashHandler::sCrashLogName = u8"log.html"; |
| 12 | const String CrashHandler::sFatalErrorMsg = |
| 13 | "A fatal error occurred and the program has to terminate!"; |
| 14 | |
| 15 | CrashHandler& gCrashHandler() |
| 16 | { |
| 17 | return CrashHandler::instance(); |
| 18 | } |
| 19 | |
| 20 | const Path& CrashHandler::getCrashFolder() |
| 21 | { |
| 22 | static const Path path = FileSystem::getWorkingDirectoryPath() + sCrashReportFolder + |
| 23 | getCrashTimestamp(); |
| 24 | |
| 25 | static bool first = true; |
| 26 | if (first) |
| 27 | { |
| 28 | FileSystem::createDir(path); |
| 29 | first = false; |
| 30 | } |
| 31 | |
| 32 | return path; |
| 33 | } |
| 34 | |
| 35 | void CrashHandler::logErrorAndStackTrace(const String& errorMsg, const String& stackTrace) const |
| 36 | { |
| 37 | StringStream errorMessage; |
| 38 | errorMessage << sFatalErrorMsg << std::endl; |
| 39 | errorMessage << errorMsg; |
| 40 | errorMessage << "\n\nStack trace: \n"; |
| 41 | errorMessage << stackTrace; |
| 42 | |
| 43 | gDebug().logError(errorMessage.str()); |
| 44 | } |
| 45 | |
| 46 | void CrashHandler::logErrorAndStackTrace(const String& type, const String& description, const String& function, |
| 47 | const String& file, UINT32 line) const |
| 48 | { |
| 49 | StringStream errorMessage; |
| 50 | errorMessage << " - Error: "<< type << std::endl; |
| 51 | errorMessage << " - Description: "<< description << std::endl; |
| 52 | errorMessage << " - In function: "<< function << std::endl; |
| 53 | errorMessage << " - In file: "<< file << ":"<< line; |
| 54 | logErrorAndStackTrace(errorMessage.str(), getStackTrace()); |
| 55 | } |
| 56 | |
| 57 | void CrashHandler::saveCrashLog() const |
| 58 | { |
| 59 | gDebug().saveLog(getCrashFolder() + sCrashLogName); |
| 60 | } |
| 61 | } |
| 62 |