@@ -14,17 +14,7 @@ namespace AppInstaller::Debugging
1414
1515 struct SelfInitiatedMinidumpHelper
1616 {
17- SelfInitiatedMinidumpHelper () : m_keepFile(false )
18- {
19- m_filePath = Runtime::GetPathTo (Runtime::PathName::DefaultLogLocation);
20- m_filePath /= c_minidumpPrefix.data () + (' -' + Utility::GetCurrentTimeForFilename () + c_minidumpExtension.data ());
21-
22- m_file.reset (CreateFile (m_filePath.wstring ().c_str (), GENERIC_READ | GENERIC_WRITE,
23- FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr , CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr ));
24- THROW_LAST_ERROR_IF (!m_file);
25-
26- SetUnhandledExceptionFilter (UnhandledExceptionCallback);
27- }
17+ SelfInitiatedMinidumpHelper () = default ;
2818
2919 ~SelfInitiatedMinidumpHelper ()
3020 {
@@ -57,6 +47,30 @@ namespace AppInstaller::Debugging
5747 return EXCEPTION_CONTINUE_SEARCH;
5848 }
5949
50+ SelfInitiatedMinidumpHelper& Enable (const std::filesystem::path& filePath = {})
51+ {
52+ std::call_once (m_enableFlag, [&]()
53+ {
54+ if (filePath.empty ())
55+ {
56+ m_filePath = Runtime::GetPathTo (Runtime::PathName::DefaultLogLocation);
57+ m_filePath /= c_minidumpPrefix.data () + (' -' + Utility::GetCurrentTimeForFilename () + c_minidumpExtension.data ());
58+ }
59+ else
60+ {
61+ m_filePath = filePath;
62+ }
63+
64+ m_file.reset (CreateFile (m_filePath.wstring ().c_str (), GENERIC_READ | GENERIC_WRITE,
65+ FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr , CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr ));
66+ THROW_LAST_ERROR_IF (!m_file);
67+
68+ SetUnhandledExceptionFilter (UnhandledExceptionCallback);
69+ });
70+
71+ return *this ;
72+ }
73+
6074 void WriteMinidump ()
6175 {
6276 std::thread ([&]() {
@@ -66,20 +80,25 @@ namespace AppInstaller::Debugging
6680 }
6781
6882 private:
83+ std::once_flag m_enableFlag;
6984 std::filesystem::path m_filePath;
7085 wil::unique_handle m_file;
71- std::atomic_bool m_keepFile;
86+ std::atomic_bool m_keepFile{ false } ;
7287 };
7388 }
7489
7590 void EnableSelfInitiatedMinidump ()
7691 {
77- // Force object creation and thus enabling of the crash detection.
78- SelfInitiatedMinidumpHelper::Instance ();
92+ SelfInitiatedMinidumpHelper::Instance ().Enable ();
93+ }
94+
95+ void EnableSelfInitiatedMinidump (const std::filesystem::path& filePath)
96+ {
97+ SelfInitiatedMinidumpHelper::Instance ().Enable (filePath);
7998 }
8099
81100 void WriteMinidump ()
82101 {
83- SelfInitiatedMinidumpHelper::Instance ().WriteMinidump ();
102+ SelfInitiatedMinidumpHelper::Instance ().Enable (). WriteMinidump ();
84103 }
85104}
0 commit comments