[PowerToys Run] Logging (#11378)

This commit is contained in:
Mykhailo Pylyp
2021-05-21 17:24:33 +03:00
committed by GitHub
parent 4a65aa3d0e
commit 50c6a1de8a
4 changed files with 32 additions and 8 deletions

View File

@@ -6,6 +6,8 @@
#include <sddl.h> #include <sddl.h>
#include <string> #include <string>
#include <common/logger/logger.h>
#include <common/utils/winapi_error.h>
// Returns true if the current process is running with elevated privileges // Returns true if the current process is running with elevated privileges
inline bool is_process_elevated(const bool use_cached_value = true) 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(); HWND hwnd = GetShellWindow();
if (!hwnd) if (!hwnd)
{ {
Logger::error(L"GetShellWindow() failed. {}", get_last_error_or_default(GetLastError()));
return false; return false;
} }
DWORD pid; 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) }; winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) };
if (!process) if (!process)
{ {
Logger::error(L"OpenProcess() failed. {}", get_last_error_or_default(GetLastError()));
return false; return false;
} }
@@ -107,21 +111,28 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param
InitializeProcThreadAttributeList(nullptr, 1, 0, &size); InitializeProcThreadAttributeList(nullptr, 1, 0, &size);
auto pproc_buffer = std::make_unique<char[]>(size); auto pproc_buffer = std::make_unique<char[]>(size);
auto pptal = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(pproc_buffer.get()); auto pptal = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(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)) if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size))
{ {
Logger::error(L"InitializeProcThreadAttributeList() failed. {}", get_last_error_or_default(GetLastError()));
return false; return false;
} }
HANDLE process_handle = process.get(); HANDLE process_handle = process.get();
if (!pptal || !UpdateProcThreadAttribute(pptal, if (!UpdateProcThreadAttribute(pptal,
0, 0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
&process_handle, &process_handle,
sizeof(process_handle), sizeof(process_handle),
nullptr, nullptr,
nullptr)) nullptr))
{ {
Logger::error(L"UpdateProcThreadAttribute() failed. {}", get_last_error_or_default(GetLastError()));
return false; return false;
} }
@@ -156,6 +167,10 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
} }
} }
else
{
Logger::error(L"CreateProcessW() failed. {}", get_last_error_or_default(GetLastError()));
}
return succeeded; return succeeded;
} }

View File

@@ -243,7 +243,7 @@ public:
if (run_non_elevated(action_runner_path, params, pidBuffer)) 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; m_enabled = true;
const int maxRetries = 80; const int maxRetries = 80;
for (int retry = 0; retry < maxRetries; ++retry) for (int retry = 0; retry < maxRetries; ++retry)

View File

@@ -42,6 +42,7 @@ namespace PowerLauncher
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
Log.Info($"Starting PowerToys Run with PID={Process.GetCurrentProcess().Id}", typeof(App));
if (SingleInstance<App>.InitializeAsFirstInstance(Unique)) if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{ {
using (var application = new App()) using (var application = new App())
@@ -50,10 +51,15 @@ namespace PowerLauncher
application.Run(); application.Run();
} }
} }
else
{
Log.Info("There is already running PowerToys Run instance", typeof(App));
}
} }
private void OnStartup(object sender, StartupEventArgs e) private void OnStartup(object sender, StartupEventArgs e)
{ {
Log.Info("On Startup.", GetType());
for (int i = 0; i + 1 < e.Args.Length; i++) for (int i = 0; i + 1 < e.Args.Length; i++)
{ {
if (e.Args[i] == "-powerToysPid") if (e.Args[i] == "-powerToysPid")
@@ -61,8 +67,10 @@ namespace PowerLauncher
int powerToysPid; int powerToysPid;
if (int.TryParse(e.Args[i + 1], out powerToysPid)) if (int.TryParse(e.Args[i + 1], out powerToysPid))
{ {
Log.Info($"Runner pid={powerToysPid}", GetType());
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () => RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
{ {
Log.Info($"Runner with pid={powerToysPid} exited. Exiting PowerToys Run", GetType());
try try
{ {
Dispose(); Dispose();

View File

@@ -91,6 +91,7 @@
<ItemGroup> <ItemGroup>
<None Include="Resources.resx" /> <None Include="Resources.resx" />
</ItemGroup> </ItemGroup>
<Import Project="..\..\..\..\deps\spdlog.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
<Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" /> <Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />