Added CLogger class into solution tree, and removed other unnecessary files.

This commit is contained in:
2023-11-28 16:46:33 +03:00
parent 56e6a5b6b2
commit 14ee12cb85
4 changed files with 2 additions and 103 deletions

View File

@@ -1,15 +0,0 @@
// wCenterWindow
// globals.h
//
#pragma once
#include "wCenterWindow.h"
#define MAX_LOADSTRING 50
#define TBUFLEN 32
#define DBUFLEN 256
extern WCHAR szTitle[];
extern SYSTEMTIME lt;
extern wchar_t debugBuffer[DBUFLEN];
extern std::wofstream logfile;
extern CRITICAL_SECTION cs;

View File

@@ -1,68 +0,0 @@
// wCenterWindow
// logger.cpp
//
#include "globals.h"
#include "logger.h"
SYSTEMTIME lt;
wchar_t debugTimeBuffer[TBUFLEN];
wchar_t debugBuffer[DBUFLEN];
std::wofstream logfile;
wchar_t* GetTimeStamp()
{
GetLocalTime(&lt);
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(const wchar_t appTitle[], const wchar_t appVersion[])
{
WCHAR appTitleVer[MAX_LOADSTRING]{ 0 };
StringCchPrintfW(appTitleVer, MAX_LOADSTRING, L"%s v%s", appTitle, appVersion);
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", appTitle, MB_OK | MB_ICONWARNING);
return;
}
if (NULL == dwPathLength)
{
MessageBoxW(NULL, L"Can't get module filename! Working without logging", appTitle, 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() << "[ " << appTitleVer << " ] Start log." << std::endl;
logfile << GetTimeStamp() << "Logfile: \"" << log_path.native() << "\"" << std::endl;
}
else
{
MessageBoxW(NULL, L"Can't open logfile! Working without logging", appTitle, MB_OK | MB_ICONWARNING);
}
return;
}
void CloseLogFile()
{
if (logfile)
{
logfile << GetTimeStamp() << "Stop log." << std::endl;
logfile.close();
}
}

View File

@@ -1,17 +0,0 @@
// wCenterWindow
// logger.h
// wLogger v3.3 (Edited version from RBTray project [https://github.com/benbuck/rbtray])
//
// Usage: LOG_TO_FILE(L"%s(%d): Log message", TEXT(__FUNCTION__), __LINE__);
//
#pragma once
#define LOG_TO_FILE(fmt, ...) do {\
EnterCriticalSection(&cs); \
StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); \
logfile << GetTimeStamp() << debugBuffer << std::endl; \
LeaveCriticalSection(&cs); \
} while (0);
wchar_t* GetTimeStamp();
void OpenLogFile(const wchar_t[], const wchar_t[]);
void CloseLogFile();

View File

@@ -186,9 +186,8 @@
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CLogger.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="globals.h" />
<ClInclude Include="logger.h" />
<ClInclude Include="picojson.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="targetver.h" />
@@ -196,7 +195,7 @@
<ClInclude Include="wCenterWindow.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="logger.cpp" />
<ClCompile Include="CLogger.cpp" />
<ClCompile Include="updater.cpp" />
<ClCompile Include="wCenterWindow.cpp" />
</ItemGroup>