3 Commits

Author SHA1 Message Date
75b31b5489 Fixed logging codepage.
So it should work on Windows 7.
2025-05-09 14:23:09 +03:00
386f2df1da Changed path to logfile in debug mode. 2025-05-03 17:09:31 +03:00
822f0dc7b7 Dropped toolset to v142. 2025-05-03 17:07:44 +03:00
4 changed files with 35 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
// wCenterWindow // wCenterWindow
// CLogger.cpp // CLogger.cpp
#include "CLogger.h" #include "CLogger.h"
@@ -6,6 +6,15 @@
#include <filesystem> #include <filesystem>
#include <strsafe.h> #include <strsafe.h>
// Convert a wide Unicode string to an UTF8 string
std::string CLogger::ConvU16U8(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
std::string str_out(size, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &str_out[0], size, NULL, NULL);
return str_out;
}
inline wchar_t* CLogger::GetTimeStamp() { inline wchar_t* CLogger::GetTimeStamp() {
GetLocalTime(&lt); GetLocalTime(&lt);
StringCchPrintfW(logTimeBuffer, _countof(logTimeBuffer), L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); StringCchPrintfW(logTimeBuffer, _countof(logTimeBuffer), L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
@@ -19,7 +28,7 @@ void CLogger::Out(const wchar_t* fmt, ...) {
EnterCriticalSection(&cs); EnterCriticalSection(&cs);
StringCchVPrintfW(logBuffer, _countof(logBuffer), fmt, args); StringCchVPrintfW(logBuffer, _countof(logBuffer), fmt, args);
va_end(args); va_end(args);
fsLogFile << GetTimeStamp() << logBuffer << std::endl; fsLogFile << ConvU16U8(GetTimeStamp()) << ConvU16U8(logBuffer) << std::endl;
LeaveCriticalSection(&cs); LeaveCriticalSection(&cs);
} }
} }
@@ -44,15 +53,15 @@ void CLogger::Init() {
if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path);
#ifdef _DEBUG #ifdef _DEBUG
log_path = L"D:\\test.log"; log_path = L"test.log";
#endif #endif
fsLogFile.open(log_path, std::ios::trunc); fsLogFile.open(log_path, std::ios::trunc);
if (fsLogFile.is_open()) { if (fsLogFile.is_open()) {
InitializeCriticalSection(&cs); InitializeCriticalSection(&cs);
fsLogFile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM fsLogFile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM
fsLogFile.imbue(std::locale("en-US.utf8")); fsLogFile.imbue(std::locale(".UTF-8"));
fsLogFile << GetTimeStamp() << "[ " << szAppTitleVer.c_str() << " ] Start log." << std::endl; fsLogFile << ConvU16U8(GetTimeStamp()) << "[ " << ConvU16U8(szAppTitleVer).c_str() << " ] Start log." << std::endl;
fsLogFile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl; fsLogFile << ConvU16U8(GetTimeStamp()) << "Logfile: \"" << ConvU16U8(log_path.native()) << "\"" << std::endl;
} }
else { else {
MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
@@ -67,7 +76,7 @@ CLogger::CLogger(const wchar_t* _appTitle) {
CLogger::~CLogger() { CLogger::~CLogger() {
if (fsLogFile) { if (fsLogFile) {
fsLogFile << GetTimeStamp() << "Stop log." << std::endl; fsLogFile << ConvU16U8(GetTimeStamp()) << "Stop log." << std::endl;
fsLogFile.close(); fsLogFile.close();
DeleteCriticalSection(&cs); DeleteCriticalSection(&cs);
} }

View File

@@ -1,4 +1,4 @@
// wCenterWindow // wCenterWindow
// CLogger.h // CLogger.h
#pragma once #pragma once
@@ -18,12 +18,13 @@ private:
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
wchar_t logTimeBuffer[28] { 0 }; wchar_t logTimeBuffer[28] { 0 };
wchar_t logBuffer[MAX_LOGBUFFER_LENGTH] { 0 }; wchar_t logBuffer[MAX_LOGBUFFER_LENGTH] { 0 };
std::wofstream fsLogFile; std::ofstream fsLogFile;
std::wstring szAppTitle { 0 }; std::wstring szAppTitle { 0 };
std::wstring szAppVersion { 0 }; std::wstring szAppVersion { 0 };
std::wstring szAppPlatform { 0 }; std::wstring szAppPlatform { 0 };
std::wstring szAppTitleVer { 0 }; std::wstring szAppTitleVer { 0 };
std::string ConvU16U8(const std::wstring&);
inline wchar_t* GetTimeStamp(); inline wchar_t* GetTimeStamp();
void Init(); void Init();
}; };

View File

@@ -5,6 +5,8 @@
// TODO: Make the automatic updater (download, unzip and replace executable). // TODO: Make the automatic updater (download, unzip and replace executable).
// TODO: Change keyboard low-level hook to RegisterHotKey function. (Is it really needed?) // TODO: Change keyboard low-level hook to RegisterHotKey function. (Is it really needed?)
#pragma warning( disable : 28251 )
#include "framework.h" #include "framework.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
#include "updater.h" #include "updater.h"

View File

@@ -30,26 +30,26 @@
<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>v143</PlatformToolset> <PlatformToolset>v142</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>v143</PlatformToolset> <PlatformToolset>v142</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>v143</PlatformToolset> <PlatformToolset>v142</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>v143</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
@@ -100,6 +100,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode> <ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -123,7 +124,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode> <ConformanceMode>false</ConformanceMode>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
@@ -147,6 +148,7 @@
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -170,7 +172,7 @@
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>