diff --git a/wCenterWindow/Logger.cpp b/wCenterWindow/Logger.cpp new file mode 100644 index 0000000..f777fe1 --- /dev/null +++ b/wCenterWindow/Logger.cpp @@ -0,0 +1,57 @@ +// Logger.cpp +// +#include +#include +#include +#include +#include +#include "Logger.h" + +#define TS_LEN 30 +#define PATH_LEN 1024 + +std::wofstream logfile; +namespace fs = std::filesystem; + +std::wstring GetTimeStamp() { + SYSTEMTIME lt; + GetLocalTime(<); + wchar_t ts[TS_LEN]; + StringCchPrintf(ts, TS_LEN, L"%d-%02d-%02d %02d:%02d:%02d.%03d - ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); + return ts; +} + +void OpenLogFile() { + wchar_t lpszPath[PATH_LEN]{}; + DWORD dwPathLength = GetModuleFileName(NULL, lpszPath, PATH_LEN); + DWORD dwError = GetLastError(); + if (ERROR_INSUFFICIENT_BUFFER == dwError) { + MessageBoxW(NULL, L"Path to logfile is too long! Working without logging.", L"WARNING", MB_OK | MB_ICONWARNING); + return; + } + if (NULL == dwPathLength) { + MessageBoxW(NULL, L"Can't get module filename! Working without logging.", L"WARNING", MB_OK | MB_ICONWARNING); + return; + } + + fs::path log_path = lpszPath; + log_path.replace_extension(L".log"); + std::wstring logname = log_path.stem() += L".log"; + + logfile.open(logname); + if (logfile.is_open()) { + diag_log(L"Starting logging."); + diag_log(L"logfile:", logname); + diag_log(logname, L"successfully opened."); + } else { + MessageBoxW(NULL, L"Can't open logfile! Working without logging.", L"WARNING", MB_OK | MB_ICONWARNING); + } + return; +} + +void CloseLogFile() { + if (logfile) { + diag_log(L"Ending logging."); + logfile.close(); + } +} diff --git a/wCenterWindow/Logger.h b/wCenterWindow/Logger.h new file mode 100644 index 0000000..0dfb6d8 --- /dev/null +++ b/wCenterWindow/Logger.h @@ -0,0 +1,57 @@ +#pragma once +#include +#include + +extern std::wofstream logfile; +std::wstring GetTimeStamp(); + +template +void diag_log(T1 arg1) { + //std::wcout << GetTimeStamp() << arg1 << std::endl; + logfile << GetTimeStamp() << arg1 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << ' ' << arg7 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << ' ' << arg7 << std::endl; +} + +template +void diag_log(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { + //std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << ' ' << arg7 << ' ' << arg8 << std::endl; + logfile << GetTimeStamp() << arg1 << ' ' << arg2 << ' ' << arg3 << ' ' << arg4 << ' ' << arg5 << ' ' << arg6 << ' ' << arg7 << ' ' << arg8 << std::endl; +} + +void OpenLogFile(); +void CloseLogFile(); diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index 4b49f87..ea9ca9d 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -1,8 +1,9 @@ -// wCenterWindow, v2.2 +// wCenterWindow, v2.3 // #include "framework.h" #include "wCenterWindow.h" +#include "Logger.h" #define KEY_I 0x49 #define KEY_C 0x43 @@ -59,11 +60,15 @@ ATOM MyRegisterClass(HINSTANCE hInstance) VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) { + diag_log(L"Entering MoveWindowToMonitorCenter(): hwnd =", hwnd); + RECT fgwrc; GetWindowRect(hwnd, &fgwrc); LONG nWidth = fgwrc.right - fgwrc.left; LONG nHeight = fgwrc.bottom - fgwrc.top; + diag_log(L"Moving window from x =", fgwrc.left, L" y =", fgwrc.top, L" r =", fgwrc.right, L" b =", fgwrc.bottom); + MONITORINFO mi; mi.cbSize = sizeof(MONITORINFO); GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi); @@ -95,6 +100,8 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) SendMessage(hwnd, WM_ENTERSIZEMOVE, NULL, NULL); MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); SendMessage(hwnd, WM_EXITSIZEMOVE, NULL, NULL); + + diag_log(L"Moving window to x =", x, L" y =", y, L" r =", aw, L" b =", ah); } @@ -104,6 +111,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); + OpenLogFile(); + diag_log(L"Entering WinMain()..."); + hInst = hInstance; LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); @@ -125,6 +135,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm int nArgs = 0; LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); + + diag_log(L"nArgs =", nArgs, L" Args: ", *szArglist); + (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], TEXT("/hide"))) ? bShowIcon = FALSE : bShowIcon = TRUE; LocalFree(szArglist); HandlingTrayIcon(); @@ -155,6 +168,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm if (hMenu) DestroyMenu(hMenu); Shell_NotifyIcon(NIM_DELETE, &nid); + diag_log(L"Quiting WinMain(). msg.wParam =", (int)msg.wParam); + CloseLogFile(); + return (int)msg.wParam; } @@ -384,10 +400,12 @@ BOOL CheckWindow(HWND hFW) VOID HandlingTrayIcon() { + diag_log(L"Entering HandlingTrayIcon()...", L" bShowIcon=", bShowIcon); if (bShowIcon) { if (!Shell_NotifyIcon(NIM_ADD, &nid)) { + diag_log(L"GetLastError():", GetLastError()); ShowError(IDS_ERR_ICON); bShowIcon = FALSE; } @@ -396,6 +414,7 @@ VOID HandlingTrayIcon() { Shell_NotifyIcon(NIM_DELETE, &nid); } + diag_log(L"Quiting HandlingTrayIcon()..."); } VOID ShowError(UINT uID) diff --git a/wCenterWindow/wCenterWindow.rc b/wCenterWindow/wCenterWindow.rc index 5079055..f9a8fdd 100644 Binary files a/wCenterWindow/wCenterWindow.rc and b/wCenterWindow/wCenterWindow.rc differ diff --git a/wCenterWindow/wCenterWindow.vcxproj b/wCenterWindow/wCenterWindow.vcxproj index a35f84c..d356576 100644 --- a/wCenterWindow/wCenterWindow.vcxproj +++ b/wCenterWindow/wCenterWindow.vcxproj @@ -24,31 +24,32 @@ {f1a1603a-f5d0-47b8-8e4b-cf17747bcfba} wCenterWindow 10.0 + false Application true - v142 + v143 Unicode Application false - v142 + v141_xp true Unicode Application true - v142 + v141_xp Unicode Application false - v142 + v141_xp true Unicode @@ -88,7 +89,8 @@ Level3 true WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) - true + false + stdcpp17 Windows @@ -108,6 +110,7 @@ WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true MultiThreaded + stdcpp17 Windows @@ -126,6 +129,7 @@ true _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true + stdcpp17 Windows @@ -140,6 +144,7 @@ true NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true + stdcpp17 Windows @@ -150,11 +155,13 @@ + + diff --git a/wCenterWindow/wCenterWindow.vcxproj.filters b/wCenterWindow/wCenterWindow.vcxproj.filters index 3d0c9ac..fae9a27 100644 --- a/wCenterWindow/wCenterWindow.vcxproj.filters +++ b/wCenterWindow/wCenterWindow.vcxproj.filters @@ -27,11 +27,17 @@ Header Files + + Header Files + Source Files + + Source Files +