diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index 8143e72b72..0b551c241b 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -2501,6 +2501,7 @@ website webview wekyb wexfs +wdupenv Whichdoes whitespaces WIC diff --git a/src/common/logger/logger.cpp b/src/common/logger/logger.cpp index 06cfd9c9cf..f5b069847f 100644 --- a/src/common/logger/logger.cpp +++ b/src/common/logger/logger.cpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace std; using namespace spdlog; @@ -33,31 +34,45 @@ level::level_enum getLogLevel(std::wstring_view logSettingsPath) return result; } -Logger::Logger() +std::shared_ptr Logger::logger; + +bool Logger::wasLogFailedShown() { + wchar_t* pValue; + size_t len; + _wdupenv_s(&pValue, &len, logFailedShown.c_str()); + delete[] pValue; + return len; } -Logger::Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath) +void Logger::init(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath) { auto logLevel = getLogLevel(logSettingsPath); try { auto sink = make_shared(logFilePath, 0, 0, false, LogSettings::retention); - this->logger = make_shared(loggerName, sink); + logger = make_shared(loggerName, sink); } catch (...) { - cerr << "Can not create file logger. Create stdout logger instead" << endl; - this->logger = spdlog::stdout_color_mt("some_unique_name"); + logger = spdlog::null_logger_mt(loggerName); + if (!wasLogFailedShown()) + { + // todo: that message should be shown from init caller and strings should be localized + MessageBoxW(NULL, + L"Logger can not be initialized", + L"PowerToys", + MB_OK | MB_ICONERROR); + + SetEnvironmentVariable(logFailedShown.c_str(), L"yes"); + } + + return; } - this->logger->set_level(logLevel); - this->logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [p-%P] [t-%t] [%l] %v"); - spdlog::register_logger(this->logger); + logger->set_level(logLevel); + logger->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [p-%P] [t-%t] [%l] %v"); + spdlog::register_logger(logger); spdlog::flush_every(std::chrono::seconds(3)); -} - -Logger::~Logger() -{ - this->logger.reset(); + logger->info("{} logger is initialized", loggerName); } diff --git a/src/common/logger/logger.h b/src/common/logger/logger.h index e95700e6ca..c015e66d4e 100644 --- a/src/common/logger/logger.h +++ b/src/common/logger/logger.h @@ -5,53 +5,54 @@ class Logger { private: - std::shared_ptr logger; + inline const static std::wstring logFailedShown = L"logFailedShown"; + static std::shared_ptr logger; + static bool wasLogFailedShown(); public: - Logger(); - Logger(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath); + Logger() = delete; + + static void init(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath); // log message should not be localized template - void trace(const FormatString& fmt, const Args&... args) + static void trace(const FormatString& fmt, const Args&... args) { - this->logger->trace(fmt, args...); + logger->trace(fmt, args...); } // log message should not be localized template - void debug(const FormatString& fmt, const Args&... args) + static void debug(const FormatString& fmt, const Args&... args) { - this->logger->debug(fmt, args...); + logger->debug(fmt, args...); } // log message should not be localized template - void info(const FormatString& fmt, const Args&... args) + static void info(const FormatString& fmt, const Args&... args) { - this->logger->info(fmt, args...); + logger->info(fmt, args...); } // log message should not be localized template - void warn(const FormatString& fmt, const Args&... args) + static void warn(const FormatString& fmt, const Args&... args) { - this->logger->warn(fmt, args...); + logger->warn(fmt, args...); } // log message should not be localized template - void error(const FormatString& fmt, const Args&... args) + static void error(const FormatString& fmt, const Args&... args) { - this->logger->error(fmt, args...); + logger->error(fmt, args...); } // log message should not be localized template - void critical(const FormatString& fmt, const Args&... args) + static void critical(const FormatString& fmt, const Args&... args) { - this->logger->critical(fmt, args...); + logger->critical(fmt, args...); } - - ~Logger(); }; diff --git a/src/common/logger/logger_settings.cpp b/src/common/logger/logger_settings.cpp index 34dfa87d72..7484f42a5d 100644 --- a/src/common/logger/logger_settings.cpp +++ b/src/common/logger/logger_settings.cpp @@ -40,7 +40,6 @@ void to_file(std::wstring_view file_name, const JsonObject& obj) } catch (...) { - std::cerr << "Can not create log config file" << std::endl; } } @@ -61,7 +60,6 @@ LogSettings to_settings(JsonObject jobject) } catch (...) { - std::cerr << "Can not read log level from config file" << std::endl; result.logLevel = LogSettings::defaultLogLevel; } diff --git a/src/common/timeutil.h b/src/common/timeutil.h index 348eb7ab9e..e6d1fa1177 100644 --- a/src/common/timeutil.h +++ b/src/common/timeutil.h @@ -51,7 +51,7 @@ namespace timeutil inline int64_t in_days(const std::time_t to, const std::time_t from) { - return static_cast(std::difftime(to, from) / (3600 * 24)); + return static_cast(std::difftime(to, from) / (3600 * (int64_t)24)); } } } diff --git a/src/modules/colorPicker/UnitTest-ColorPickerUI/Helpers/ColorConverterTest.cs b/src/modules/colorPicker/UnitTest-ColorPickerUI/Helpers/ColorConverterTest.cs index f9d0e97ceb..80de408646 100644 --- a/src/modules/colorPicker/UnitTest-ColorPickerUI/Helpers/ColorConverterTest.cs +++ b/src/modules/colorPicker/UnitTest-ColorPickerUI/Helpers/ColorConverterTest.cs @@ -294,7 +294,7 @@ namespace UnitTest_ColorPickerUI.Helpers { var color = Color.FromArgb(red, green, blue); - Exception exception = null; + Exception? exception = null; try { diff --git a/src/modules/fancyzones/dll/dllmain.cpp b/src/modules/fancyzones/dll/dllmain.cpp index 5b82759348..a2253d6a43 100644 --- a/src/modules/fancyzones/dll/dllmain.cpp +++ b/src/modules/fancyzones/dll/dllmain.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include extern "C" IMAGE_DOS_HEADER __ImageBase; @@ -75,7 +75,7 @@ public: // Enable the powertoy virtual void enable() { - FancyZonesLogger::GetLogger()->info("FancyZones enabling"); + Logger::info("FancyZones enabling"); if (!m_app) { @@ -133,7 +133,7 @@ public: // Disable the powertoy virtual void disable() { - FancyZonesLogger::GetLogger()->info("FancyZones disabling"); + Logger::info("FancyZones disabling"); Disable(true); } @@ -155,7 +155,9 @@ public: { app_name = GET_RESOURCE_STRING(IDS_FANCYZONES); app_key = NonLocalizable::FancyZonesStr; - FancyZonesLogger::Init(PTSettingsHelper::get_module_save_folder_location(app_key)); + std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(app_key)); + logFilePath.append(LogSettings::fancyZonesLogPath); + Logger::init(LogSettings::fancyZonesLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); m_settings = MakeFancyZonesSettings(reinterpret_cast(&__ImageBase), FancyZonesModule::get_name(), FancyZonesModule::get_key()); FancyZonesDataInstance().LoadFancyZonesData(); s_instance = this; diff --git a/src/modules/fancyzones/lib/FancyZonesLib.vcxproj b/src/modules/fancyzones/lib/FancyZonesLib.vcxproj index 06c30eaf41..5fdaac34b1 100644 --- a/src/modules/fancyzones/lib/FancyZonesLib.vcxproj +++ b/src/modules/fancyzones/lib/FancyZonesLib.vcxproj @@ -102,7 +102,6 @@ - @@ -128,7 +127,6 @@ - diff --git a/src/modules/fancyzones/lib/FancyZonesLogger.cpp b/src/modules/fancyzones/lib/FancyZonesLogger.cpp deleted file mode 100644 index bf2e83b7ee..0000000000 --- a/src/modules/fancyzones/lib/FancyZonesLogger.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "pch.h" -#include -#include -#include "FancyZonesLogger.h" - -std::shared_ptr FancyZonesLogger::logger; - -void FancyZonesLogger::Init(std::wstring moduleSaveLocation) -{ - std::filesystem::path logFilePath(moduleSaveLocation); - logFilePath.append(LogSettings::fancyZonesLogPath); - logger = std::make_shared(LogSettings::fancyZonesLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); - logger->info("FancyZones logger initialized"); -} - -std::shared_ptr FancyZonesLogger::GetLogger() -{ - if (!logger) - { - throw "FancyZones logger is not initialized"; - } - - return logger; -} diff --git a/src/modules/fancyzones/lib/FancyZonesLogger.h b/src/modules/fancyzones/lib/FancyZonesLogger.h deleted file mode 100644 index 488844e37f..0000000000 --- a/src/modules/fancyzones/lib/FancyZonesLogger.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include - -class FancyZonesLogger -{ - static std::shared_ptr logger; -public: - static void Init(std::wstring moduleSaveLocation); - static std::shared_ptr GetLogger(); -}; \ No newline at end of file diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index 836206b32a..800fc79a9a 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -78,8 +78,6 @@ private: // Handle to event used to invoke the Runner HANDLE m_hEvent; - std::shared_ptr logger; - public: // Constructor Microsoft_Launcher() @@ -88,8 +86,8 @@ public: app_key = LauncherConstants::ModuleKey; std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(this->app_key)); logFilePath.append(LogSettings::launcherLogPath); - logger = std::make_shared(LogSettings::launcherLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); - logger->info("Launcher object is constructing"); + Logger::init(LogSettings::launcherLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); + Logger::info("Launcher object is constructing"); init_settings(); SECURITY_ATTRIBUTES sa; @@ -101,8 +99,7 @@ public: ~Microsoft_Launcher() { - logger->info("Launcher object is destroying"); - logger.reset(); + Logger::info("Launcher object is destroying"); if (m_enabled) { terminateProcess(); @@ -183,7 +180,7 @@ public: // Enable the powertoy virtual void enable() { - this->logger->info("Launcher is enabling"); + Logger::info("Launcher is enabling"); ResetEvent(m_hEvent); // Start PowerLauncher.exe only if the OS is 19H1 or higher if (UseNewSettings()) @@ -255,7 +252,7 @@ public: // Disable the powertoy virtual void disable() { - this->logger->info("Launcher is disabling"); + Logger::info("Launcher is disabling"); if (m_enabled) { ResetEvent(m_hEvent); @@ -327,7 +324,7 @@ public: if (TerminateProcess(m_hProcess, 1) == 0) { auto err = get_last_error_message(GetLastError()); - this->logger->error(L"Launcher process was not terminated. {}", err.has_value() ? err.value() : L""); + Logger::error(L"Launcher process was not terminated. {}", err.has_value() ? err.value() : L""); } // Temporarily disable sending a message to close diff --git a/src/modules/shortcut_guide/ShortcutGuideLogger.cpp b/src/modules/shortcut_guide/ShortcutGuideLogger.cpp deleted file mode 100644 index 72f7fb7e67..0000000000 --- a/src/modules/shortcut_guide/ShortcutGuideLogger.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "pch.h" -#include "ShortcutGuideLogger.h" -#include -#include - -std::shared_ptr ShortcutGuideLogger::logger; - -void ShortcutGuideLogger::Init(std::wstring moduleSaveLocation) -{ - std::filesystem::path logFilePath(moduleSaveLocation); - logFilePath.append(LogSettings::shortcutGuideLogPath); - logger = std::make_shared(LogSettings::shortcutGuideLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); - logger->info("Shortcut Guide logger initialized"); -} - -std::shared_ptr ShortcutGuideLogger::GetLogger() -{ - if (!logger) - { - throw "Shortcut Guide logger is not initialized"; - } - - return logger; -} diff --git a/src/modules/shortcut_guide/ShortcutGuideLogger.h b/src/modules/shortcut_guide/ShortcutGuideLogger.h deleted file mode 100644 index 9703ca0f66..0000000000 --- a/src/modules/shortcut_guide/ShortcutGuideLogger.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include - -class ShortcutGuideLogger -{ - static std::shared_ptr logger; - -public: - static void Init(std::wstring moduleSaveLocation); - static std::shared_ptr GetLogger(); -}; \ No newline at end of file diff --git a/src/modules/shortcut_guide/shortcut_guide.cpp b/src/modules/shortcut_guide/shortcut_guide.cpp index 03226c3e03..eae7cc7335 100644 --- a/src/modules/shortcut_guide/shortcut_guide.cpp +++ b/src/modules/shortcut_guide/shortcut_guide.cpp @@ -7,9 +7,11 @@ #include #include #include -#include -#include -#include +#include + +#include +#include + extern "C" IMAGE_DOS_HEADER __ImageBase; @@ -98,8 +100,10 @@ OverlayWindow::OverlayWindow() { app_name = GET_RESOURCE_STRING(IDS_SHORTCUT_GUIDE); app_key = ShortcutGuideConstants::ModuleKey; - ShortcutGuideLogger::Init(PTSettingsHelper::get_module_save_folder_location(app_key)); - ShortcutGuideLogger::GetLogger()->info("Overlay Window is creating"); + std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(app_key)); + logFilePath.append(LogSettings::shortcutGuideLogPath); + Logger::init(LogSettings::shortcutGuideLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); + Logger::info("Overlay Window is creating"); init_settings(); } @@ -200,7 +204,7 @@ constexpr UINT alternative_switch_vk_code = VK_OEM_2; void OverlayWindow::enable() { - ShortcutGuideLogger::GetLogger()->info("Shortcut Guide is enabling"); + Logger::info("Shortcut Guide is enabling"); auto switcher = [&](HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> LRESULT { if (msg == WM_KEYDOWN && wparam == VK_ESCAPE && instance->target_state->active()) @@ -253,7 +257,7 @@ void OverlayWindow::enable() void OverlayWindow::disable(bool trace_event) { - ShortcutGuideLogger::GetLogger()->info("Shortcut Guide is disabling"); + Logger::info("Shortcut Guide is disabling"); if (_enabled) { diff --git a/src/modules/shortcut_guide/shortcut_guide.vcxproj b/src/modules/shortcut_guide/shortcut_guide.vcxproj index 5e7c7173ae..826510f334 100644 --- a/src/modules/shortcut_guide/shortcut_guide.vcxproj +++ b/src/modules/shortcut_guide/shortcut_guide.vcxproj @@ -112,7 +112,6 @@ - @@ -125,7 +124,6 @@ - Create diff --git a/src/runner/main.cpp b/src/runner/main.cpp index a664c5246a..f9cf8cd52e 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -46,7 +46,6 @@ namespace const wchar_t POWER_TOYS_MODULE_LOAD_FAIL[] = L"Failed to load "; // Module name will be appended on this message and it is not localized. } -std::shared_ptr logger; void chdir_current_executable() { // Change current directory to the path of the executable. @@ -77,7 +76,11 @@ void open_menu_from_another_instance() int runner(bool isProcessElevated) { - logger->info("Runner is starting. Elevated={}", isProcessElevated); + std::filesystem::path logFilePath(PTSettingsHelper::get_root_save_folder_location()); + logFilePath.append(LogSettings::runnerLogPath); + Logger::init(LogSettings::runnerLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); + + Logger::info("Runner is starting. Elevated={}", isProcessElevated); DPIAware::EnableDPIAwarenessForThisProcess(); #if _DEBUG && _WIN64 @@ -296,10 +299,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return 0; } - std::filesystem::path logFilePath(PTSettingsHelper::get_root_save_folder_location()); - logFilePath = logFilePath.append(LogSettings::runnerLogPath); - logger = std::make_shared(LogSettings::runnerLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); - int n_cmd_args = 0; LPWSTR* cmd_arg_list = CommandLineToArgvW(GetCommandLineW(), &n_cmd_args); switch (should_run_in_special_mode(n_cmd_args, cmd_arg_list)) diff --git a/src/runner/settings_window.cpp b/src/runner/settings_window.cpp index 670949071d..9006069f32 100644 --- a/src/runner/settings_window.cpp +++ b/src/runner/settings_window.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #define BUFSIZE 1024 @@ -276,9 +277,18 @@ void run_settings_window() std::wstring powertoys_pipe_name(L"\\\\.\\pipe\\powertoys_runner_"); std::wstring settings_pipe_name(L"\\\\.\\pipe\\powertoys_settings_"); UUID temp_uuid; - UuidCreate(&temp_uuid); - wchar_t* uuid_chars; - UuidToString(&temp_uuid, (RPC_WSTR*)&uuid_chars); + wchar_t* uuid_chars = nullptr; + if (UuidCreate(&temp_uuid) == RPC_S_UUID_NO_ADDRESS) + { + auto val = get_last_error_message(GetLastError()); + Logger::warn(L"UuidCreate can not create guid. {}", val.has_value() ? val.value() : L""); + } + else if (UuidToString(&temp_uuid, (RPC_WSTR*)&uuid_chars) != RPC_S_OK) + { + auto val = get_last_error_message(GetLastError()); + Logger::warn(L"UuidToString can not convert to string. {}", val.has_value() ? val.value() : L""); + } + if (uuid_chars != nullptr) { powertoys_pipe_name += std::wstring(uuid_chars); @@ -386,10 +396,18 @@ void run_settings_window() current_settings_ipc->start(hToken); g_settings_process_id = process_info.dwProcessId; - WaitForSingleObject(process_info.hProcess, INFINITE); - if (WaitForSingleObject(process_info.hProcess, INFINITE) != WAIT_OBJECT_0) + if (process_info.hProcess) { - show_last_error_message(L"Couldn't wait on the Settings Window to close.", GetLastError(), L"PowerToys - runner"); + WaitForSingleObject(process_info.hProcess, INFINITE); + if (WaitForSingleObject(process_info.hProcess, INFINITE) != WAIT_OBJECT_0) + { + show_last_error_message(L"Couldn't wait on the Settings Window to close.", GetLastError(), L"PowerToys - runner"); + } + } + else + { + auto val = get_last_error_message(GetLastError()); + Logger::error(L"Process handle is empty. {}", val.has_value() ? val.value() : L""); } LExit: