Added simple logging

This commit is contained in:
2021-12-10 02:20:42 +03:00
parent a4af63ed34
commit b8d3db9289
6 changed files with 152 additions and 6 deletions

57
wCenterWindow/Logger.cpp Normal file
View File

@@ -0,0 +1,57 @@
// Logger.cpp
//
#include <fstream>
#include <filesystem>
#include <string>
#include <strsafe.h>
#include <Windows.h>
#include "Logger.h"
#define TS_LEN 30
#define PATH_LEN 1024
std::wofstream logfile;
namespace fs = std::filesystem;
std::wstring GetTimeStamp() {
SYSTEMTIME lt;
GetLocalTime(&lt);
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();
}
}

57
wCenterWindow/Logger.h Normal file
View File

@@ -0,0 +1,57 @@
#pragma once
#include <iostream>
#include <fstream>
extern std::wofstream logfile;
std::wstring GetTimeStamp();
template <typename T1>
void diag_log(T1 arg1) {
//std::wcout << GetTimeStamp() << arg1 << std::endl;
logfile << GetTimeStamp() << arg1 << std::endl;
}
template <typename T1, typename T2>
void diag_log(T1 arg1, T2 arg2) {
//std::wcout << GetTimeStamp() << arg1 << ' ' << arg2 << std::endl;
logfile << GetTimeStamp() << arg1 << ' ' << arg2 << std::endl;
}
template <typename T1, typename T2, typename T3>
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 <typename T1, typename T2, typename T3, typename T4>
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 <typename T1, typename T2, typename T3, typename T4, typename T5>
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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
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();

View File

@@ -1,8 +1,9 @@
// wCenterWindow, v2.2 // wCenterWindow, v2.3
// //
#include "framework.h" #include "framework.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
#include "Logger.h"
#define KEY_I 0x49 #define KEY_I 0x49
#define KEY_C 0x43 #define KEY_C 0x43
@@ -59,11 +60,15 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize) VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
{ {
diag_log(L"Entering MoveWindowToMonitorCenter(): hwnd =", hwnd);
RECT fgwrc; RECT fgwrc;
GetWindowRect(hwnd, &fgwrc); GetWindowRect(hwnd, &fgwrc);
LONG nWidth = fgwrc.right - fgwrc.left; LONG nWidth = fgwrc.right - fgwrc.left;
LONG nHeight = fgwrc.bottom - fgwrc.top; 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; MONITORINFO mi;
mi.cbSize = sizeof(MONITORINFO); mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi); GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi);
@@ -95,6 +100,8 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
SendMessage(hwnd, WM_ENTERSIZEMOVE, NULL, NULL); SendMessage(hwnd, WM_ENTERSIZEMOVE, NULL, NULL);
MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE); MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE);
SendMessage(hwnd, WM_EXITSIZEMOVE, NULL, NULL); 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(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);
OpenLogFile();
diag_log(L"Entering WinMain()...");
hInst = hInstance; hInst = hInstance;
LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle)); LoadString(hInstance, IDS_APP_TITLE, szTitle, _countof(szTitle));
@@ -125,6 +135,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
int nArgs = 0; int nArgs = 0;
LPWSTR* szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); 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; (nArgs >= 2 && 0 == lstrcmpiW(szArglist[1], TEXT("/hide"))) ? bShowIcon = FALSE : bShowIcon = TRUE;
LocalFree(szArglist); LocalFree(szArglist);
HandlingTrayIcon(); HandlingTrayIcon();
@@ -155,6 +168,9 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
if (hMenu) DestroyMenu(hMenu); if (hMenu) DestroyMenu(hMenu);
Shell_NotifyIcon(NIM_DELETE, &nid); Shell_NotifyIcon(NIM_DELETE, &nid);
diag_log(L"Quiting WinMain(). msg.wParam =", (int)msg.wParam);
CloseLogFile();
return (int)msg.wParam; return (int)msg.wParam;
} }
@@ -384,10 +400,12 @@ BOOL CheckWindow(HWND hFW)
VOID HandlingTrayIcon() VOID HandlingTrayIcon()
{ {
diag_log(L"Entering HandlingTrayIcon()...", L" bShowIcon=", bShowIcon);
if (bShowIcon) if (bShowIcon)
{ {
if (!Shell_NotifyIcon(NIM_ADD, &nid)) if (!Shell_NotifyIcon(NIM_ADD, &nid))
{ {
diag_log(L"GetLastError():", GetLastError());
ShowError(IDS_ERR_ICON); ShowError(IDS_ERR_ICON);
bShowIcon = FALSE; bShowIcon = FALSE;
} }
@@ -396,6 +414,7 @@ VOID HandlingTrayIcon()
{ {
Shell_NotifyIcon(NIM_DELETE, &nid); Shell_NotifyIcon(NIM_DELETE, &nid);
} }
diag_log(L"Quiting HandlingTrayIcon()...");
} }
VOID ShowError(UINT uID) VOID ShowError(UINT uID)

Binary file not shown.

View File

@@ -24,31 +24,32 @@
<ProjectGuid>{f1a1603a-f5d0-47b8-8e4b-cf17747bcfba}</ProjectGuid> <ProjectGuid>{f1a1603a-f5d0-47b8-8e4b-cf17747bcfba}</ProjectGuid>
<RootNamespace>wCenterWindow</RootNamespace> <RootNamespace>wCenterWindow</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<XPDeprecationWarning>false</XPDeprecationWarning>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v141_xp</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v141_xp</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v141_xp</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
@@ -88,7 +89,8 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -108,6 +110,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -126,6 +129,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -140,6 +144,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -150,11 +155,13 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="framework.h" /> <ClInclude Include="framework.h" />
<ClInclude Include="Logger.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />
<ClInclude Include="wCenterWindow.h" /> <ClInclude Include="wCenterWindow.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Logger.cpp" />
<ClCompile Include="wCenterWindow.cpp" /> <ClCompile Include="wCenterWindow.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -27,11 +27,17 @@
<ClInclude Include="wCenterWindow.h"> <ClInclude Include="wCenterWindow.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Logger.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="wCenterWindow.cpp"> <ClCompile Include="wCenterWindow.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="wCenterWindow.rc"> <ResourceCompile Include="wCenterWindow.rc">