CrashHandler_Windows.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /******************************************************************************
  2. Copyright (C) 2025 by Patrick Heyer <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "CrashHandler.hpp"
  15. #include <OBSApp.hpp>
  16. #include <util/util.hpp>
  17. #include <vector>
  18. namespace OBS {
  19. using CrashFileEntry = std::pair<std::filesystem::path, std::filesystem::file_time_type>;
  20. PlatformType CrashHandler::getPlatformType() const
  21. {
  22. return PlatformType::Windows;
  23. }
  24. std::filesystem::path CrashHandler::findLastCrashLog() const
  25. {
  26. std::filesystem::path lastCrashLogFile;
  27. std::filesystem::path crashLogDirectory = getCrashLogDirectory();
  28. if (!std::filesystem::exists(crashLogDirectory)) {
  29. blog(LOG_ERROR, "Crash log directory '%s' does not exist", crashLogDirectory.u8string().c_str());
  30. return lastCrashLogFile;
  31. }
  32. std::vector<CrashFileEntry> crashLogFiles;
  33. for (const auto &entry : std::filesystem::directory_iterator(crashLogDirectory)) {
  34. if (entry.is_directory()) {
  35. continue;
  36. }
  37. std::string entryFileName = entry.path().filename().u8string();
  38. if (entryFileName.rfind("Crash ", 0) != 0) {
  39. continue;
  40. }
  41. CrashFileEntry crashLogFile =
  42. CrashFileEntry(entry.path(), std::filesystem::last_write_time(entry.path()));
  43. crashLogFiles.push_back(crashLogFile);
  44. }
  45. std::sort(crashLogFiles.begin(), crashLogFiles.end(),
  46. [](CrashFileEntry &lhs, CrashFileEntry &rhs) { return lhs.second > rhs.second; });
  47. if (crashLogFiles.size() > 0) {
  48. lastCrashLogFile = crashLogFiles.front().first;
  49. }
  50. return lastCrashLogFile;
  51. }
  52. std::filesystem::path CrashHandler::getCrashLogDirectory() const
  53. {
  54. BPtr crashLogDirectory = GetAppConfigPathPtr("obs-studio/crashes");
  55. std::string crashLogDirectoryString = crashLogDirectory.Get();
  56. std::filesystem::path crashLogDirectoryPath = std::filesystem::u8path(crashLogDirectoryString);
  57. return crashLogDirectoryPath;
  58. }
  59. } // namespace OBS