diff --git a/wCenterWindow/CLogger.cpp b/wCenterWindow/CLogger.cpp index a5f5c13..7655ffc 100644 --- a/wCenterWindow/CLogger.cpp +++ b/wCenterWindow/CLogger.cpp @@ -1,4 +1,4 @@ -// wCenterWindow +// wCenterWindow // CLogger.cpp #include "CLogger.h" @@ -6,6 +6,15 @@ #include #include +// 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() { GetLocalTime(<); 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); StringCchVPrintfW(logBuffer, _countof(logBuffer), fmt, args); va_end(args); - fsLogFile << GetTimeStamp() << logBuffer << std::endl; + fsLogFile << ConvU16U8(GetTimeStamp()) << ConvU16U8(logBuffer) << std::endl; LeaveCriticalSection(&cs); } } @@ -50,9 +59,9 @@ void CLogger::Init() { if (fsLogFile.is_open()) { InitializeCriticalSection(&cs); fsLogFile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM - fsLogFile.imbue(std::locale("en-US.utf8")); - fsLogFile << GetTimeStamp() << "[ " << szAppTitleVer.c_str() << " ] Start log." << std::endl; - fsLogFile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl; + fsLogFile.imbue(std::locale(".UTF-8")); + fsLogFile << ConvU16U8(GetTimeStamp()) << "[ " << ConvU16U8(szAppTitleVer).c_str() << " ] Start log." << std::endl; + fsLogFile << ConvU16U8(GetTimeStamp()) << "Logfile: \"" << ConvU16U8(log_path.native()) << "\"" << std::endl; } else { 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() { if (fsLogFile) { - fsLogFile << GetTimeStamp() << "Stop log." << std::endl; + fsLogFile << ConvU16U8(GetTimeStamp()) << "Stop log." << std::endl; fsLogFile.close(); DeleteCriticalSection(&cs); } diff --git a/wCenterWindow/CLogger.h b/wCenterWindow/CLogger.h index ade8a24..a1255e3 100644 --- a/wCenterWindow/CLogger.h +++ b/wCenterWindow/CLogger.h @@ -1,4 +1,4 @@ -// wCenterWindow +// wCenterWindow // CLogger.h #pragma once @@ -16,14 +16,15 @@ public: private: SYSTEMTIME lt; CRITICAL_SECTION cs; - wchar_t logTimeBuffer[28]{ 0 }; - wchar_t logBuffer[MAX_LOGBUFFER_LENGTH]{ 0 }; - std::wofstream fsLogFile; - std::wstring szAppTitle{ 0 }; - std::wstring szAppVersion{ 0 }; - std::wstring szAppPlatform{ 0 }; - std::wstring szAppTitleVer{ 0 }; + wchar_t logTimeBuffer[28] { 0 }; + wchar_t logBuffer[MAX_LOGBUFFER_LENGTH] { 0 }; + std::ofstream fsLogFile; + std::wstring szAppTitle { 0 }; + std::wstring szAppVersion { 0 }; + std::wstring szAppPlatform { 0 }; + std::wstring szAppTitleVer { 0 }; + std::string ConvU16U8(const std::wstring&); inline wchar_t* GetTimeStamp(); void Init(); }; diff --git a/wCenterWindow/wCenterWindow.vcxproj b/wCenterWindow/wCenterWindow.vcxproj index fa56458..58862e4 100644 --- a/wCenterWindow/wCenterWindow.vcxproj +++ b/wCenterWindow/wCenterWindow.vcxproj @@ -100,6 +100,7 @@ WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) false stdcpp17 + MultiThreadedDebug Windows @@ -123,7 +124,7 @@ true WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) false - MultiThreadedDLL + MultiThreaded stdcpp17 @@ -147,6 +148,7 @@ _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp17 + MultiThreadedDebug Windows @@ -170,7 +172,7 @@ NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true stdcpp17 - MultiThreadedDLL + MultiThreaded Windows