From 50c6a1de8a7996b1a70bf6504b65c7b57175797a Mon Sep 17 00:00:00 2001 From: Mykhailo Pylyp Date: Fri, 21 May 2021 17:24:33 +0300 Subject: [PATCH] [PowerToys Run] Logging (#11378) --- src/common/utils/elevation.h | 29 ++++++++++++++----- .../launcher/Microsoft.Launcher/dllmain.cpp | 2 +- .../launcher/PowerLauncher/App.xaml.cs | 8 +++++ .../powerpreview/powerpreview.vcxproj | 1 + 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/common/utils/elevation.h b/src/common/utils/elevation.h index 119355d273..17f891ac3f 100644 --- a/src/common/utils/elevation.h +++ b/src/common/utils/elevation.h @@ -6,6 +6,8 @@ #include #include +#include +#include // Returns true if the current process is running with elevated privileges inline bool is_process_elevated(const bool use_cached_value = true) @@ -91,6 +93,7 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param HWND hwnd = GetShellWindow(); if (!hwnd) { + Logger::error(L"GetShellWindow() failed. {}", get_last_error_or_default(GetLastError())); return false; } DWORD pid; @@ -99,6 +102,7 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) }; if (!process) { + Logger::error(L"OpenProcess() failed. {}", get_last_error_or_default(GetLastError())); return false; } @@ -107,21 +111,28 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param InitializeProcThreadAttributeList(nullptr, 1, 0, &size); auto pproc_buffer = std::make_unique(size); auto pptal = reinterpret_cast(pproc_buffer.get()); + if (!pptal) + { + Logger::error(L"pptal failed to initialize. {}", get_last_error_or_default(GetLastError())); + return false; + } if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size)) { + Logger::error(L"InitializeProcThreadAttributeList() failed. {}", get_last_error_or_default(GetLastError())); return false; } HANDLE process_handle = process.get(); - if (!pptal || !UpdateProcThreadAttribute(pptal, - 0, - PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, - &process_handle, - sizeof(process_handle), - nullptr, - nullptr)) + if (!UpdateProcThreadAttribute(pptal, + 0, + PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, + &process_handle, + sizeof(process_handle), + nullptr, + nullptr)) { + Logger::error(L"UpdateProcThreadAttribute() failed. {}", get_last_error_or_default(GetLastError())); return false; } @@ -156,6 +167,10 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param CloseHandle(pi.hThread); } } + else + { + Logger::error(L"CreateProcessW() failed. {}", get_last_error_or_default(GetLastError())); + } return succeeded; } diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index 430dbecb22..8017d2a58c 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -243,7 +243,7 @@ public: if (run_non_elevated(action_runner_path, params, pidBuffer)) { - Logger::trace("Started PowerToys Run Process. PID {}", *pidBuffer); + Logger::trace("Started PowerToys Run Process"); m_enabled = true; const int maxRetries = 80; for (int retry = 0; retry < maxRetries; ++retry) diff --git a/src/modules/launcher/PowerLauncher/App.xaml.cs b/src/modules/launcher/PowerLauncher/App.xaml.cs index ffe735505d..8ba00506cc 100644 --- a/src/modules/launcher/PowerLauncher/App.xaml.cs +++ b/src/modules/launcher/PowerLauncher/App.xaml.cs @@ -42,6 +42,7 @@ namespace PowerLauncher [STAThread] public static void Main() { + Log.Info($"Starting PowerToys Run with PID={Process.GetCurrentProcess().Id}", typeof(App)); if (SingleInstance.InitializeAsFirstInstance(Unique)) { using (var application = new App()) @@ -50,10 +51,15 @@ namespace PowerLauncher application.Run(); } } + else + { + Log.Info("There is already running PowerToys Run instance", typeof(App)); + } } private void OnStartup(object sender, StartupEventArgs e) { + Log.Info("On Startup.", GetType()); for (int i = 0; i + 1 < e.Args.Length; i++) { if (e.Args[i] == "-powerToysPid") @@ -61,8 +67,10 @@ namespace PowerLauncher int powerToysPid; if (int.TryParse(e.Args[i + 1], out powerToysPid)) { + Log.Info($"Runner pid={powerToysPid}", GetType()); RunnerHelper.WaitForPowerToysRunner(powerToysPid, () => { + Log.Info($"Runner with pid={powerToysPid} exited. Exiting PowerToys Run", GetType()); try { Dispose(); diff --git a/src/modules/previewpane/powerpreview/powerpreview.vcxproj b/src/modules/previewpane/powerpreview/powerpreview.vcxproj index 53bed6a061..4b7898d9b8 100644 --- a/src/modules/previewpane/powerpreview/powerpreview.vcxproj +++ b/src/modules/previewpane/powerpreview/powerpreview.vcxproj @@ -91,6 +91,7 @@ +