diff --git a/wCenterWindow/Logger.h b/wCenterWindow/Logger.h index 47cafa5..4c0a12d 100644 --- a/wCenterWindow/Logger.h +++ b/wCenterWindow/Logger.h @@ -1,74 +1,15 @@ // wCenterWindow // logger.h -// wLogger v3.1 (Edited version from RBTray project [https://github.com/benbuck/rbtray]) +// wLogger v3.2 (Edited version from RBTray project [https://github.com/benbuck/rbtray]) // // Usage: LOG_TO_FILE(L"%s(%d): Log message", TEXT(__FUNCTION__), __LINE__); // #pragma once -#include "globals.h" -#include "wCenterWindow.h" +//#include "globals.h" +//#include "wCenterWindow.h" #define LOG_TO_FILE(fmt, ...) StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); logfile << GetTimeStamp() << debugBuffer << std::endl; -SYSTEMTIME lt; -wchar_t debugTimeBuffer[TBUFLEN]; -wchar_t debugBuffer[DBUFLEN]; -std::wofstream logfile; - -extern wchar_t szTitle[]; - -wchar_t* GetTimeStamp() -{ - GetLocalTime(<); - StringCchPrintfW(debugTimeBuffer, TBUFLEN, L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); - return debugTimeBuffer; -} - -void OpenLogFile() -{ - WCHAR lpszPath[MAX_PATH + 1] = { 0 }; - DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH); - DWORD dwError = GetLastError(); - if (ERROR_INSUFFICIENT_BUFFER == dwError) - { - MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", szTitle, MB_OK | MB_ICONWARNING); - return; - } - if (NULL == dwPathLength) - { - MessageBoxW(NULL, L"Can't get module filename! Working without logging", szTitle, MB_OK | MB_ICONWARNING); - return; - } - - std::filesystem::path log_path = lpszPath; - log_path.replace_extension(L".log"); - std::filesystem::path bak_path = log_path; - bak_path.replace_extension(L".bak"); - - if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); -#ifdef _DEBUG - log_path = L"D:\\test.log"; -#endif - logfile.open(log_path, std::ios::trunc); - if (logfile.is_open()) - { - logfile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM - logfile.imbue(std::locale("en-US.utf8")); - logfile << GetTimeStamp() << "[ " << szTitle << " ] Start log." << std::endl; - logfile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl; - } - else - { - MessageBoxW(NULL, L"Can't open logfile! Working without logging", szTitle, MB_OK | MB_ICONWARNING); - } - return; -} - -void CloseLogFile() -{ - if (logfile) - { - logfile << GetTimeStamp() << "[ " << szTitle << " ] Stop log." << std::endl; - logfile.close(); - } -} +wchar_t* GetTimeStamp(); +void OpenLogFile(); +void CloseLogFile(); diff --git a/wCenterWindow/globals.h b/wCenterWindow/globals.h index 799d151..3f01534 100644 --- a/wCenterWindow/globals.h +++ b/wCenterWindow/globals.h @@ -9,5 +9,8 @@ #define DBUFLEN 256 extern WCHAR szTitle[MAX_LOADSTRING]; + +extern SYSTEMTIME lt; +extern wchar_t debugBuffer[DBUFLEN]; extern std::wofstream logfile; diff --git a/wCenterWindow/logger.cpp b/wCenterWindow/logger.cpp new file mode 100644 index 0000000..5d73d48 --- /dev/null +++ b/wCenterWindow/logger.cpp @@ -0,0 +1,68 @@ +// wCenterWindow +// logger.cpp +// +#include "wCenterWindow.h" +#include "globals.h" +#include "logger.h" + +SYSTEMTIME lt; +wchar_t debugTimeBuffer[TBUFLEN]; +wchar_t debugBuffer[DBUFLEN]; +std::wofstream logfile; + +wchar_t* GetTimeStamp() +{ + GetLocalTime(<); + StringCchPrintfW(debugTimeBuffer, TBUFLEN, L"%d-%02d-%02d %02d:%02d:%02d.%03d | ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); + return debugTimeBuffer; +} + +void OpenLogFile() +{ + WCHAR lpszPath[MAX_PATH + 1] = { 0 }; + DWORD dwPathLength = GetModuleFileNameW(NULL, lpszPath, MAX_PATH); + DWORD dwError = GetLastError(); + if (ERROR_INSUFFICIENT_BUFFER == dwError) + { + MessageBoxW(NULL, L"Path to logfile is too long! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + return; + } + if (NULL == dwPathLength) + { + MessageBoxW(NULL, L"Can't get module filename! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + return; + } + + std::filesystem::path log_path = lpszPath; + log_path.replace_extension(L".log"); + std::filesystem::path bak_path = log_path; + bak_path.replace_extension(L".bak"); + + if (std::filesystem::exists(log_path)) std::filesystem::rename(log_path, bak_path); +#ifdef _DEBUG + log_path = L"D:\\test.log"; +#endif + logfile.open(log_path, std::ios::trunc); + if (logfile.is_open()) + { + logfile << "\xEF\xBB\xBF"; // (0xEF, 0xBB, 0xBF) - UTF-8 BOM + logfile.imbue(std::locale("en-US.utf8")); + logfile << GetTimeStamp() << "[ " << szTitle << " ] Start log." << std::endl; + logfile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl; + } + else + { + MessageBoxW(NULL, L"Can't open logfile! Working without logging", szTitle, MB_OK | MB_ICONWARNING); + } + return; +} + +void CloseLogFile() +{ + if (logfile) + { + logfile << GetTimeStamp() << "[ " << szTitle << " ] Stop log." << std::endl; + logfile.close(); + } +} + diff --git a/wCenterWindow/wCenterWindow.vcxproj b/wCenterWindow/wCenterWindow.vcxproj index 76aa750..a0f52fa 100644 --- a/wCenterWindow/wCenterWindow.vcxproj +++ b/wCenterWindow/wCenterWindow.vcxproj @@ -194,6 +194,7 @@ +