CrashSupport.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // LY Crash Handler shared support
  9. #pragma once
  10. #include <AzCore/PlatformDef.h>
  11. #include <AzCore/std/string/conversions.h>
  12. #include <AzCore/Utils/Utils.h>
  13. #include <algorithm>
  14. #include <string>
  15. #define CRASH_HANDLER_MAX_PATH_LEN 1024
  16. #define _MAKE_DEFINE_STRING(x) #x
  17. #define MAKE_DEFINE_STRING(x) _MAKE_DEFINE_STRING(x)
  18. namespace CrashHandler
  19. {
  20. std::string GetTimeString();
  21. void GetTimeInfo(tm& curTime);
  22. inline void GetExecutablePath(std::string& returnPath)
  23. {
  24. char currentFileName[CRASH_HANDLER_MAX_PATH_LEN] = { 0 };
  25. AZ::Utils::GetExecutablePath(currentFileName, CRASH_HANDLER_MAX_PATH_LEN);
  26. returnPath = currentFileName;
  27. std::replace(returnPath.begin(), returnPath.end(), '\\', '/');
  28. }
  29. inline void GetExecutablePath(std::wstring& returnPathW)
  30. {
  31. std::string returnPath;
  32. GetExecutablePath(returnPath);
  33. wchar_t currentFileNameW[CRASH_HANDLER_MAX_PATH_LEN] = { 0 };
  34. AZStd::to_wstring(currentFileNameW, CRASH_HANDLER_MAX_PATH_LEN, { returnPath.c_str(), returnPath.size() });
  35. returnPathW = currentFileNameW;
  36. }
  37. template<typename T>
  38. inline void GetExecutableFolder(T& returnPath)
  39. {
  40. GetExecutablePath(returnPath);
  41. auto lastPos = returnPath.find_last_of('/');
  42. if (lastPos != T::npos)
  43. {
  44. returnPath = returnPath.substr(0, lastPos + 1);
  45. }
  46. }
  47. template<typename T>
  48. inline void GetExecutableBaseName(T& returnPath)
  49. {
  50. GetExecutablePath(returnPath);
  51. auto lastPos = returnPath.find_last_of('/');
  52. if (lastPos != T::npos)
  53. {
  54. returnPath = returnPath.substr(lastPos + 1, returnPath.length());
  55. }
  56. auto extPos = returnPath.find_last_of('.');
  57. if (extPos != T::npos)
  58. {
  59. returnPath = returnPath.substr(0, extPos);
  60. }
  61. }
  62. }