diff --git a/src/modules/fancyzones/FancyZonesLib/CallTracer.cpp b/src/common/logger/call_tracer.cpp similarity index 96% rename from src/modules/fancyzones/FancyZonesLib/CallTracer.cpp rename to src/common/logger/call_tracer.cpp index a7dd2ad908..ae4b9dfa03 100644 --- a/src/modules/fancyzones/FancyZonesLib/CallTracer.cpp +++ b/src/common/logger/call_tracer.cpp @@ -1,5 +1,7 @@ #include "pch.h" -#include "CallTracer.h" +#include "call_tracer.h" + +#include #include namespace diff --git a/src/modules/fancyzones/FancyZonesLib/CallTracer.h b/src/common/logger/call_tracer.h similarity index 82% rename from src/modules/fancyzones/FancyZonesLib/CallTracer.h rename to src/common/logger/call_tracer.h index c2c0e67e82..a271633c25 100644 --- a/src/modules/fancyzones/FancyZonesLib/CallTracer.h +++ b/src/common/logger/call_tracer.h @@ -1,6 +1,8 @@ #pragma once -#include "common/logger/logger.h" +#include + +#include "logger.h" #define _TRACER_ CallTracer callTracer(__FUNCTION__) diff --git a/src/common/logger/logger.vcxproj b/src/common/logger/logger.vcxproj index e8417dd42e..7dde0c7c82 100644 --- a/src/common/logger/logger.vcxproj +++ b/src/common/logger/logger.vcxproj @@ -31,12 +31,14 @@ + + diff --git a/src/common/logger/logger.vcxproj.filters b/src/common/logger/logger.vcxproj.filters index cc30326a76..27abca281f 100644 --- a/src/common/logger/logger.vcxproj.filters +++ b/src/common/logger/logger.vcxproj.filters @@ -27,6 +27,9 @@ Header Files + + Header Files + @@ -38,6 +41,9 @@ Source Files + + Source Files + diff --git a/src/common/logger/logger_settings.h b/src/common/logger/logger_settings.h index 35b398792d..cada3f537f 100644 --- a/src/common/logger/logger_settings.h +++ b/src/common/logger/logger_settings.h @@ -24,6 +24,7 @@ struct LogSettings inline const static std::string keyboardManagerLoggerName = "keyboard-manager"; inline const static std::wstring keyboardManagerLogPath = L"Logs\\keyboard-manager-log.txt"; inline const static std::string findMyMouseLoggerName = "find-my-mouse"; + inline const static std::string powerRenameLoggerName = "powerrename"; inline const static int retention = 30; std::wstring logLevel; LogSettings(); diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp index 284aa890bd..246fb1f601 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp +++ b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,6 @@ #include "VirtualDesktop.h" #include "MonitorWorkAreaHandler.h" #include "util.h" -#include "CallTracer.h" #include #include diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp b/src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp index 72d195be97..277f4ae101 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp @@ -4,10 +4,10 @@ #include "JsonHelpers.h" #include "ZoneSet.h" #include "Settings.h" -#include "CallTracer.h" #include "GuidUtils.h" #include +#include #include #include #include diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj index 0387a20044..2c770bbea3 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj @@ -37,7 +37,6 @@ - @@ -65,7 +64,6 @@ - diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters index 2a1b8750e5..e82f0f67a1 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters @@ -78,9 +78,6 @@ Header Files - - Header Files - Header Files @@ -149,9 +146,6 @@ Source Files - - Source Files - Source Files diff --git a/src/modules/fancyzones/FancyZonesLib/OnThreadExecutor.cpp b/src/modules/fancyzones/FancyZonesLib/OnThreadExecutor.cpp index 3e574a3703..4f85365143 100644 --- a/src/modules/fancyzones/FancyZonesLib/OnThreadExecutor.cpp +++ b/src/modules/fancyzones/FancyZonesLib/OnThreadExecutor.cpp @@ -1,7 +1,8 @@ #include "pch.h" +#include + #include "on_thread_executor.h" -#include "CallTracer.h" OnThreadExecutor::OnThreadExecutor() : _shutdown_request{ false }, _worker_thread{ [this] { worker_thread(); } } diff --git a/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp b/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp index 995dcaeb06..29fefa9895 100644 --- a/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp +++ b/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "WorkArea.h" +#include #include #include "FancyZonesData.h" @@ -10,7 +11,6 @@ #include "util.h" #include "on_thread_executor.h" #include "Settings.h" -#include "CallTracer.h" #include #include diff --git a/src/modules/fancyzones/FancyZonesLib/ZoneWindowDrawing.cpp b/src/modules/fancyzones/FancyZonesLib/ZoneWindowDrawing.cpp index f23193843c..3c3040b19d 100644 --- a/src/modules/fancyzones/FancyZonesLib/ZoneWindowDrawing.cpp +++ b/src/modules/fancyzones/FancyZonesLib/ZoneWindowDrawing.cpp @@ -1,12 +1,12 @@ #include "pch.h" #include "ZoneWindowDrawing.h" -#include "CallTracer.h" #include #include #include #include +#include #include namespace diff --git a/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.cpp b/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.cpp index 214e7445aa..9f1b94317e 100644 --- a/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.cpp +++ b/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.cpp @@ -5,13 +5,22 @@ #include "PowerRenameUIHost.h" #include #include + +#include #include #include #include + +#include +#include +#include #include #define MAX_LOADSTRING 100 +// Non-localizable +const std::wstring moduleName = L"PowerRename"; +const std::wstring internalPath = L""; const wchar_t c_WindowClass[] = L"PowerRename"; HINSTANCE g_hostHInst; @@ -19,6 +28,8 @@ int AppWindow::Show(HINSTANCE hInstance, std::vector files) { auto window = AppWindow(hInstance, files); window.CreateAndShowWindow(); + Logger::debug(L"PowerRename UI created. Starting the message loop."); + return window.MessageLoop(window.m_accelerators.get()); } @@ -40,38 +51,54 @@ LRESULT AppWindow::MessageHandler(UINT message, WPARAM wParam, LPARAM lParam) no AppWindow::AppWindow(HINSTANCE hInstance, std::vector files) noexcept : m_instance{ hInstance }, m_managerEvents{ this } { - HRESULT hr = CPowerRenameManager::s_CreateInstance(&m_prManager); - // Create the factory for our items - CComPtr prItemFactory; - hr = CPowerRenameItem::s_CreateInstance(nullptr, IID_PPV_ARGS(&prItemFactory)); - hr = m_prManager->PutRenameItemFactory(prItemFactory); - hr = m_prManager->Advise(&m_managerEvents, &m_cookie); - - if (SUCCEEDED(hr)) + if (SUCCEEDED(CPowerRenameManager::s_CreateInstance(&m_prManager))) { - CComPtr shellItemArray; - // To test PowerRenameUIHost uncomment this line and update the path to - // your local (absolute or relative) path which you want to see in PowerRename - // files.push_back(L"path"); - - if (!files.empty()) + // Create the factory for our items + CComPtr prItemFactory; + if (SUCCEEDED(CPowerRenameItem::s_CreateInstance(nullptr, IID_PPV_ARGS(&prItemFactory)))) { - hr = CreateShellItemArrayFromPaths(files, &shellItemArray); - if (SUCCEEDED(hr)) + if(SUCCEEDED(m_prManager->PutRenameItemFactory(prItemFactory))) { - CComPtr enumShellItems; - hr = shellItemArray->EnumItems(&enumShellItems); - if (SUCCEEDED(hr)) + if (SUCCEEDED(m_prManager->Advise(&m_managerEvents, &m_cookie))) { - EnumerateShellItems(enumShellItems); + CComPtr shellItemArray; + // To test PowerRenameUIHost uncomment this line and update the path to + // your local (absolute or relative) path which you want to see in PowerRename + //files.push_back(L""); + + if (!files.empty()) + { + if (SUCCEEDED(CreateShellItemArrayFromPaths(files, &shellItemArray))) + { + CComPtr enumShellItems; + if (SUCCEEDED(shellItemArray->EnumItems(&enumShellItems))) + { + EnumerateShellItems(enumShellItems); + } + } + } + else + { + Logger::warn(L"No items selected to be renamed."); + } } } } + else + { + Logger::error(L"Error creating PowerRenameItemFactory"); + } + } + else + { + Logger::error(L"Error creating PowerRenameManager"); } } void AppWindow::CreateAndShowWindow() { + _TRACER_; + m_accelerators.reset(LoadAcceleratorsW(m_instance, MAKEINTRESOURCE(IDC_POWERRENAMEUIHOST))); WNDCLASSEXW wcex = { sizeof(wcex) }; @@ -99,12 +126,21 @@ void AppWindow::CreateAndShowWindow() bool AppWindow::OnCreate(HWND, LPCREATESTRUCT) noexcept { + _TRACER_; + m_mainUserControl = winrt::PowerRenameUILib::MainWindow(); m_xamlIsland = CreateDesktopWindowsXamlSource(WS_TABSTOP, m_mainUserControl); - PopulateExplorerItems(); - SetHandlers(); - ReadSettings(); + try + { + PopulateExplorerItems(); + SetHandlers(); + ReadSettings(); + } + catch (std::exception e) + { + Logger::error("Exception thrown during explorer items population: {}", std::string{ e.what() }); + } m_mainUserControl.UIUpdatesItem().ButtonRenameEnabled(false); InitAutoComplete(); @@ -143,11 +179,15 @@ void AppWindow::OnCommand(HWND, int id, HWND hwndControl, UINT codeNotify) noexc void AppWindow::OnDestroy(HWND hwnd) noexcept { + _TRACER_; + base_type::OnDestroy(hwnd); } void AppWindow::OnResize(HWND, UINT state, int cx, int cy) noexcept { + _TRACER_; + SetWindowPos(m_xamlIsland, NULL, 0, 0, cx, cy, SWP_SHOWWINDOW); } @@ -155,6 +195,8 @@ HRESULT AppWindow::CreateShellItemArrayFromPaths( std::vector files, IShellItemArray** shellItemArray) { + _TRACER_; + *shellItemArray = nullptr; PIDLIST_ABSOLUTE* itemList = nullptr; itemList = new (std::nothrow) PIDLIST_ABSOLUTE[files.size()]; @@ -174,14 +216,21 @@ HRESULT AppWindow::CreateShellItemArrayFromPaths( if (SUCCEEDED(hr) && itemsCnt > 0) { hr = SHCreateShellItemArrayFromIDLists(itemsCnt, const_cast(itemList), shellItemArray); - - for (UINT i = 0; i < itemsCnt; i++) + if (SUCCEEDED(hr)) { - CoTaskMemFree(itemList[i]); + for (UINT i = 0; i < itemsCnt; i++) + { + CoTaskMemFree(itemList[i]); + } + } + else + { + Logger::error(L"Creating ShellItemArray from path list failed."); } } else { + Logger::error(L"Parsing path list display names failed."); hr = E_FAIL; } @@ -191,8 +240,11 @@ HRESULT AppWindow::CreateShellItemArrayFromPaths( void AppWindow::PopulateExplorerItems() { + _TRACER_; + UINT count = 0; m_prManager->GetVisibleItemCount(&count); + Logger::debug(L"Number of visible items: {}", count); UINT currDepth = 0; std::stack parents{}; @@ -245,6 +297,8 @@ void AppWindow::PopulateExplorerItems() HRESULT AppWindow::InitAutoComplete() { + _TRACER_; + HRESULT hr = S_OK; if (CSettingsInstance().GetMRUEnabled()) { @@ -281,6 +335,8 @@ HRESULT AppWindow::InitAutoComplete() HRESULT AppWindow::EnumerateShellItems(_In_ IEnumShellItems* enumShellItems) { + _TRACER_; + HRESULT hr = S_OK; // Enumerate the data object and populate the manager if (m_prManager) @@ -303,6 +359,9 @@ HRESULT AppWindow::EnumerateShellItems(_In_ IEnumShellItems* enumShellItems) void AppWindow::SearchReplaceChanged(bool forceRenaming) { + _TRACER_; + + Logger::debug(L"Forced renaming - {}", forceRenaming); // Pass updated search and replace terms to the IPowerRenameRegEx handler CComPtr prRegEx; if (m_prManager && SUCCEEDED(m_prManager->GetRenameRegEx(&prRegEx))) @@ -359,6 +418,8 @@ void AppWindow::ValidateFlags(PowerRenameFlags flag) void AppWindow::UpdateFlag(PowerRenameFlags flag, UpdateFlagCommand command) { + _TRACER_; + DWORD flags{}; m_prManager->GetFlags(&flags); @@ -371,6 +432,8 @@ void AppWindow::UpdateFlag(PowerRenameFlags flag, UpdateFlagCommand command) flags &= ~flag; } + Logger::debug(L"Flag {} " + std::wstring{ command == UpdateFlagCommand::Set ? L"set" : L"reset" }, flag); + // Ensure we update flags if (m_prManager) { @@ -380,6 +443,8 @@ void AppWindow::UpdateFlag(PowerRenameFlags flag, UpdateFlagCommand command) void AppWindow::SetHandlers() { + _TRACER_; + m_mainUserControl.UIUpdatesItem().PropertyChanged([&](winrt::Windows::Foundation::IInspectable const& sender, Data::PropertyChangedEventArgs const& e) { std::wstring property{ e.PropertyName() }; if (property == L"ShowAll") @@ -538,14 +603,21 @@ void AppWindow::SetHandlers() void AppWindow::ToggleItem(int32_t id, bool checked) { + _TRACER_; + Logger::debug(L"Toggling item with id = {}", id); CComPtr spItem; - m_prManager->GetItemById(id, &spItem); - spItem->PutSelected(checked); + + if (SUCCEEDED(m_prManager->GetItemById(id, &spItem))) + { + spItem->PutSelected(checked); + } UpdateCounts(); } void AppWindow::ToggleAll() { + _TRACER_; + UINT itemCount = 0; m_prManager->GetItemCount(&itemCount); bool selected = m_mainUserControl.CheckBoxSelectAll().IsChecked().GetBoolean(); @@ -562,12 +634,16 @@ void AppWindow::ToggleAll() void AppWindow::SwitchView() { + _TRACER_; + m_prManager->SwitchFilter(0); PopulateExplorerItems(); } void AppWindow::Rename(bool closeWindow) { + _TRACER_; + if (m_prManager) { m_prManager->Rename(m_window, closeWindow); @@ -581,10 +657,15 @@ void AppWindow::Rename(bool closeWindow) HRESULT AppWindow::ReadSettings() { + _TRACER_; + + bool persistState{ CSettingsInstance().GetPersistState() }; + Logger::debug(L"ReadSettings with persistState = {}", persistState); + // Check if we should read flags from settings // or the defaults from the manager. DWORD flags = 0; - if (CSettingsInstance().GetPersistState()) + if (persistState) { flags = CSettingsInstance().GetFlags(); @@ -604,6 +685,8 @@ HRESULT AppWindow::ReadSettings() HRESULT AppWindow::WriteSettings() { + _TRACER_; + // Check if we should store our settings if (CSettingsInstance().GetPersistState()) { @@ -835,6 +918,8 @@ HRESULT AppWindow::OnRegExCanceled(_In_ DWORD threadId) HRESULT AppWindow::OnRegExCompleted(_In_ DWORD threadId) { + _TRACER_; + if (m_flagValidationInProgress) { m_flagValidationInProgress = false; @@ -860,6 +945,9 @@ HRESULT AppWindow::OnRenameStarted() HRESULT AppWindow::OnRenameCompleted(bool closeUIWindowAfterRenaming) { + _TRACER_; + + Logger::debug(L"Renaming completed. Close UI window - {}", closeUIWindowAfterRenaming); if (closeUIWindowAfterRenaming) { // Close the window @@ -878,11 +966,17 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { + LoggerHelpers::init_logger(moduleName, internalPath, LogSettings::powerRenameLoggerName); + #define BUFSIZE 4096 * 4 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); if (hStdin == INVALID_HANDLE_VALUE) + { + Logger::error(L"Invalid input handle."); ExitProcess(1); + } + BOOL bSuccess; WCHAR chBuf[BUFSIZE]; DWORD dwRead; @@ -909,10 +1003,19 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, break; } - g_hostHInst = hInstance; - winrt::init_apartment(winrt::apartment_type::single_threaded); + Logger::debug(L"Starting PowerRename with {} files selected", files.size()); - winrt::PowerRenameUILib::App app; - const auto result = AppWindow::Show(hInstance, files); - app.Close(); + g_hostHInst = hInstance; + try + { + winrt::init_apartment(winrt::apartment_type::single_threaded); + + winrt::PowerRenameUILib::App app; + const auto result = AppWindow::Show(hInstance, files); + app.Close(); + } + catch (std::exception e) + { + Logger::error("Exception thrown during PowerRename UI initialization: {}", std::string{ e.what() }); + } } diff --git a/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.vcxproj b/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.vcxproj index 483078fe18..4d911a29c2 100644 --- a/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.vcxproj +++ b/src/modules/powerrename/PowerRenameUIHost/PowerRenameUIHost.vcxproj @@ -38,8 +38,10 @@ false - - + + + + @@ -126,6 +128,7 @@ + @@ -158,6 +161,9 @@ + + {d9b8fc84-322a-4f9f-bbb9-20915c47ddfd} + {51920f1f-c28c-4adf-8660-4238766796c2} diff --git a/src/modules/powerrename/dll/PowerRenameExt.vcxproj b/src/modules/powerrename/dll/PowerRenameExt.vcxproj index 9005dbeee9..ba221b69cc 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.vcxproj +++ b/src/modules/powerrename/dll/PowerRenameExt.vcxproj @@ -54,6 +54,9 @@ {caba8dfb-823b-4bf2-93ac-3f31984150d9} + + {d9b8fc84-322a-4f9f-bbb9-20915c47ddfd} + {98537082-0fdb-40de-abd8-0dc5a4269bab} @@ -65,6 +68,7 @@ + diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 985aa23993..d615703924 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include "Generated Files/resource.h" #include @@ -174,6 +176,7 @@ public: // Enable the powertoy virtual void enable() { + Logger::info(L"PowerRename enabled"); m_enabled = true; save_settings(); } @@ -181,6 +184,7 @@ public: // Disable the powertoy virtual void disable() { + Logger::info(L"PowerRename disabled"); m_enabled = false; save_settings(); } @@ -261,9 +265,9 @@ public: Trace::SettingsChanged(); } - catch (std::exception) + catch (std::exception e) { - // Improper JSON. + Logger::error("Configuration parsing failed: {}", std::string{ e.what() }); } } @@ -297,6 +301,7 @@ public: init_settings(); app_name = GET_RESOURCE_STRING(IDS_POWERRENAME_APP_NAME); app_key = PowerRenameConstants::ModuleKey; + LoggerHelpers::init_logger(app_key, L"ModuleInterface", LogSettings::powerRenameLoggerName); } ~PowerRenameModule(){};