From b06cd9f89612e1bbd2937b360689efc7af7790ac Mon Sep 17 00:00:00 2001 From: Jaylyn Barbee <51131738+Jaylyn-Barbee@users.noreply.github.com> Date: Thu, 9 Oct 2025 14:11:31 -0400 Subject: [PATCH] Adding logger to Light Switch Service (#42264) Adding proper logs to the Light Switch Service --- .../LightSwitchService/LightSwitchService.cpp | 54 ++++-- .../LightSwitchService/LightSwitchService.rc | Bin 5352 -> 5350 bytes .../LightSwitchService.vcxproj | 157 +++--------------- .../LightSwitchService.vcxproj.filters | 17 +- 4 files changed, 69 insertions(+), 159 deletions(-) diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp index 168ee092e7..15c268fc84 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include SERVICE_STATUS g_ServiceStatus = {}; SERVICE_STATUS_HANDLE g_StatusHandle = nullptr; @@ -35,6 +38,8 @@ int _tmain(int argc, TCHAR* argv[]) wchar_t serviceName[] = L"LightSwitchService"; SERVICE_TABLE_ENTRYW table[] = { { serviceName, ServiceMain }, { nullptr, nullptr } }; + LoggerHelpers::init_logger(L"LightSwitch", L"Service", LogSettings::lightSwitchLoggerName); + if (!StartServiceCtrlDispatcherW(table)) { DWORD err = GetLastError(); @@ -106,6 +111,7 @@ VOID WINAPI ServiceCtrlHandler(DWORD dwCtrl) SetServiceStatus(g_StatusHandle, &g_ServiceStatus); // Signal the service to stop + Logger::info(L"[LightSwitchService] Stop requested, signaling worker thread to exit."); SetEvent(g_ServiceStopEvent); break; @@ -126,13 +132,21 @@ static void update_sun_times(auto& settings) int newLightTime = newTimes.sunriseHour * 60 + newTimes.sunriseMinute; int newDarkTime = newTimes.sunsetHour * 60 + newTimes.sunsetMinute; + try + { + auto values = PowerToysSettings::PowerToyValues::load_from_settings_file(L"LightSwitch"); + values.add_property(L"lightTime", newLightTime); + values.add_property(L"darkTime", newDarkTime); + values.save_to_settings_file(); - auto values = PowerToysSettings::PowerToyValues::load_from_settings_file(L"LightSwitch"); - values.add_property(L"lightTime", newLightTime); - values.add_property(L"darkTime", newDarkTime); - values.save_to_settings_file(); - - OutputDebugString(L"[LightSwitchService] Updated sun times and saved to config.\n"); + Logger::info(L"[LightSwitchService] Updated sun times and saved to config."); + } + catch (const std::exception& e) + { + std::wstring wmsg(e.what(), e.what() + strlen(e.what())); + Logger::error(L"[LightSwitchService] Exception during sun time update: {}", wmsg); + } + } DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) @@ -142,7 +156,8 @@ DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) if (parentPid) hParent = OpenProcess(SYNCHRONIZE, FALSE, parentPid); - OutputDebugString(L"[LightSwitchService] Worker thread starting...\n"); + Logger::info(L"[LightSwitchService] Worker thread starting..."); + Logger::info(L"[LightSwitchService] Parent PID: {}", parentPid); // Initialize settings system LightSwitchSettings::instance().InitFileWatcher(); @@ -214,19 +229,19 @@ DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) update_sun_times(settings); g_lastUpdatedDay = st.wDay; - OutputDebugString(L"[LightSwitchService] Recalculated sun times at new day boundary.\n"); + Logger::info(L"[LightSwitchService] Recalculated sun times at new day boundary."); } wchar_t msg[160]; swprintf_s(msg, - L"[LightSwitchService] now=%02d:%02d | light=%02d:%02d | dark=%02d:%02d\n", + L"[LightSwitchService] now=%02d:%02d | light=%02d:%02d | dark=%02d:%02d", st.wHour, st.wMinute, settings.lightTime / 60, settings.lightTime % 60, settings.darkTime / 60, settings.darkTime % 60); - OutputDebugString(msg); + Logger::info(msg); // --- Manual override check --- bool manualOverrideActive = false; @@ -242,11 +257,11 @@ DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) nowMinutes == (settings.darkTime + settings.sunset_offset) % 1440) { ResetEvent(hManualOverride); - OutputDebugString(L"[LightSwitchService] Manual override cleared at boundary\n"); + Logger::info(L"[LightSwitchService] Manual override cleared at boundary\n"); } else { - OutputDebugString(L"[LightSwitchService] Skipping schedule due to manual override\n"); + Logger::info(L"[LightSwitchService] Skipping schedule due to manual override\n"); goto sleep_until_next_minute; } } @@ -261,10 +276,17 @@ DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) msToNextMinute = 50; DWORD wait = WaitForMultipleObjects(count, waits, FALSE, msToNextMinute); - if (wait == WAIT_OBJECT_0) // stop event + if (wait == WAIT_OBJECT_0) + { + Logger::info(L"[LightSwitchService] Stop event triggered — exiting worker loop."); break; - if (hParent && wait == WAIT_OBJECT_0 + 1) // parent exited + } + if (hParent && wait == WAIT_OBJECT_0 + 1) // parent process exited + { + Logger::info(L"[LightSwitchService] Parent process exited — stopping service."); break; + } + } if (hManualOverride) @@ -282,8 +304,8 @@ int APIENTRY wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) wchar_t msg[160]; swprintf_s( msg, - L"Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.\n"); - OutputDebugString(msg); + L"Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); + Logger::info(msg); return 0; } diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.rc b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.rc index 82dbcb263a088b71ada6996c65eed17786fc0354..c1914c5a5ed6d79ed7a6d0fa4e1381649bc8e409 100644 GIT binary patch delta 16 XcmaE%`Al;|4BuoP0kzF7d?{=IImQLQ delta 20 bcmaE+`9gC;3?HMyWPMI?MupAAe8p@4ODzTr diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj index 2151d0b5b6..b082250f61 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj @@ -28,19 +28,6 @@ LightSwitchService - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - Application true @@ -54,84 +41,25 @@ true Unicode - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - - - - ..\..\..\..\$(Platform)\$(Configuration)\$(MSBuildProjectName)\ PowerToys.LightSwitchService - - - Level3 - true - _DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - - - - - Level3 - true - true - true - NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - - Level3 true - %(PreprocessorDefinitions) true NotUsing + %(PreprocessorDefinitions) ./../; - ..\..\..\common\Telemetry; ..\..\..\common; + ..\..\..\common\logger; + ..\..\..\common\utils; + ..\..\..\common\SettingsAPI; + ..\..\..\common\Telemetry; ..\..\..\; ..\..\..\..\deps\spdlog\include; ./; @@ -145,8 +73,27 @@ - - {d9b8fc84-322a-4f9f-bbb9-20915c47ddfd} + + + + + + + + + + + + + + + + + + + + + {4aed67b6-55fd-486f-b917-e543dee2cb3c} {6955446d-23f7-4023-9bb3-8657f904af99} @@ -158,62 +105,10 @@ {8f021b46-362b-485c-bfba-ccf83e820cbd} - - - Level3 - true - true - true - NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - - - - - NotUsing - - - - - NotUsing - - - NotUsing - - - - - - - - - - - - - - - - false - - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - - \ No newline at end of file diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj.filters b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj.filters index a244dfc075..f5aa05afc3 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj.filters +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj.filters @@ -24,15 +24,6 @@ Source Files - - Source Files - - - Source Files - - - Source Files - Source Files @@ -43,9 +34,6 @@ Source Files - - - Header Files @@ -69,4 +57,9 @@ + + + Resource Files + + \ No newline at end of file