Add some basic tracing

This commit is contained in:
Ivan Stošić 2022-09-20 13:43:21 +02:00
parent 24fca9df02
commit ff6bcca3ab
7 changed files with 94 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include "Constants.h"
#include "Settings.h"
#include "dllmain.h"
#include "Trace.h"
// Implementations of inherited IUnknown methods
@ -130,6 +131,7 @@ IFACEMETHODIMP ExplorerCommand::QueryContextMenu(HMENU hmenu, UINT indexMenu, UI
if (!InsertMenuItem(hmenu, indexMenu, TRUE, &mii))
{
hr = HRESULT_FROM_WIN32(GetLastError());
Trace::QueryContextMenuError(hr);
}
else
{
@ -142,15 +144,18 @@ IFACEMETHODIMP ExplorerCommand::QueryContextMenu(HMENU hmenu, UINT indexMenu, UI
IFACEMETHODIMP ExplorerCommand::InvokeCommand(CMINVOKECOMMANDINFO* pici)
{
Trace::Invoked();
ipc::Writer writer;
if (HRESULT result = writer.start(); FAILED(result))
{
Trace::InvokedRet(result);
return result;
}
if (HRESULT result = LaunchUI(pici, &writer); FAILED(result))
{
Trace::InvokedRet(result);
return result;
}
@ -182,6 +187,7 @@ IFACEMETHODIMP ExplorerCommand::InvokeCommand(CMINVOKECOMMANDINFO* pici)
shell_item_array->Release();
}
Trace::InvokedRet(S_OK);
return S_OK;
}

View File

@ -167,6 +167,7 @@
<ClInclude Include="pch.h" />
<ClInclude Include="Registry.h" />
<ClInclude Include="Settings.h" />
<ClInclude Include="Trace.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ClassFactory.cpp" />
@ -181,6 +182,7 @@
<ClCompile Include="PowerToysModule.cpp" />
<ClCompile Include="Registry.cpp" />
<ClCompile Include="Settings.cpp" />
<ClCompile Include="Trace.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="dll.def" />

View File

@ -36,6 +36,9 @@
<ClInclude Include="Settings.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Trace.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@ -59,6 +62,9 @@
<ClCompile Include="Settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Trace.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="dll.def">

View File

@ -9,6 +9,7 @@
#include "Constants.h"
#include "dllmain.h"
#include "Settings.h"
#include "Trace.h"
class FileLocksmithModule : public PowertoyModuleIface
{
@ -70,6 +71,7 @@ public:
Logger::info(L"File Locksmith enabled");
m_enabled = true;
save_settings();
Trace::EnableFileLocksmith(m_enabled);
}
virtual void disable() override
@ -77,6 +79,7 @@ public:
Logger::info(L"File Locksmith disabled");
m_enabled = false;
save_settings();
Trace::EnableFileLocksmith(m_enabled);
}
virtual bool is_enabled() override

View File

@ -0,0 +1,60 @@
#include "pch.h"
#include "Trace.h"
#include "../common/Telemetry/ProjectTelemetry.h"
TRACELOGGING_DEFINE_PROVIDER(
g_hProvider,
"Microsoft.PowerToys",
// {38e8889b-9731-53f5-e901-e8a7c1753074}
(0x38e8889b, 0x9731, 0x53f5, 0xe9, 0x01, 0xe8, 0xa7, 0xc1, 0x75, 0x30, 0x74),
TraceLoggingOptionProjectTelemetry());
void Trace::RegisterProvider() noexcept
{
TraceLoggingRegister(g_hProvider);
}
void Trace::UnregisterProvider() noexcept
{
TraceLoggingUnregister(g_hProvider);
}
void Trace::EnableFileLocksmith(_In_ bool enabled) noexcept
{
TraceLoggingWrite(
g_hProvider,
"FileLocksmith_EnableFileLocksmith",
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE),
TraceLoggingBoolean(enabled, "Enabled"));
}
void Trace::Invoked() noexcept
{
TraceLoggingWrite(
g_hProvider,
"FileLocksmith_Invoked",
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}
void Trace::InvokedRet(_In_ HRESULT hr) noexcept
{
TraceLoggingWrite(
g_hProvider,
"FileLocksmith_InvokedRet",
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingHResult(hr),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}
void Trace::QueryContextMenuError(_In_ HRESULT hr) noexcept
{
TraceLoggingWrite(
g_hProvider,
"FileLocksmith_QueryContextMenuError",
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingHResult(hr),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "pch.h"
class Trace
{
public:
static void RegisterProvider() noexcept;
static void UnregisterProvider() noexcept;
static void EnableFileLocksmith(_In_ bool enabled) noexcept;
static void Invoked() noexcept;
static void InvokedRet(_In_ HRESULT hr) noexcept;
static void QueryContextMenuError(_In_ HRESULT hr) noexcept;
};

View File

@ -6,6 +6,7 @@
#include "Registry.h"
#include "ClassFactory.h"
#include "Trace.h"
namespace globals
{
@ -23,16 +24,15 @@ BOOL APIENTRY DllMain( HMODULE hModule,
{
case DLL_PROCESS_ATTACH:
globals::instance = hModule;
Trace::RegisterProvider();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
Trace::UnregisterProvider();
break;
}
return TRUE;
}
STDAPI DllRegisterServer()
{
if (!add_registry_keys())
@ -68,14 +68,3 @@ STDAPI DllCanUnloadNow(void)
{
return globals::ref_count == 0 ? S_OK : S_FALSE;
}
// Things to implement:
// 1. (DONE) A class which implements IExplorerCommand
// 2. (DONE) A class which implements IClassFactory
// 3. (DONE) DLL register/unregister functions which will create registry entries
// 4. (DONE) Other DLL exported functions
// 5. (DONE) Implement IShellExtInit in ExplorerCommand
// 6. (DONE) Implement IContextMenu in ExplorerCommand
// 7. (DONE) Extract useful functions from HandlesExperiment to a static library
// 8. (DONE) Implement IPC in Lib - to be used between UI and DLL
// 9. Implement UI