-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CLogWriter for portability and optimization.
This update improves the `CLogWriter` class by enhancing portability and performance: - Replaced WinAPI's `CreateFile` with `std::fstream` for file operations, ensuring compatibility across platforms. - Updated timestamp generation to use `std::chrono`, improving code clarity and modernizing time handling. - Introduced a `std::mutex` for thread-safe logging to address potential concurrency issues. - Simplified and standardized log messages using `std::format`, improving readability and maintainability. These changes modernize the logging implementation, making it more portable and easier to maintain while adhering to modern C++ standards.
- Loading branch information
Showing
30 changed files
with
176 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
|
||
#include <random> | ||
#include <filesystem> | ||
#include <chrono> | ||
#include <mutex> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,74 @@ | ||
// LogWriter.cpp: implementation of the CLogWriter class. | ||
// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
#include <stdio.h> | ||
#include "StdAfx.h" | ||
#include "LogWriter.h" | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
// Construction/Destruction | ||
////////////////////////////////////////////////////////////////////// | ||
//HANDLE CLogWriter::s_hFile = NULL; | ||
fs::path CLogWriter::s_fsLogFile; | ||
|
||
CLogWriter::CLogWriter() {} | ||
|
||
CLogWriter::~CLogWriter() {} | ||
static std::mutex s_mtxLog; | ||
|
||
void CLogWriter::Open(const fs::path & fsFile) { | ||
// if (s_hFile || fsFile.empty()) { | ||
// return; | ||
// } | ||
if (fsFile.empty()) { | ||
return; | ||
} | ||
|
||
s_fsLogFile = fsFile; | ||
std::lock_guard<std::mutex> lock(s_mtxLog); | ||
|
||
HANDLE hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
return; | ||
} | ||
} | ||
s_fsLogFile = fsFile; | ||
|
||
DWORD dwSizeHigh = 0; | ||
DWORD dwSizeLow = ::GetFileSize(hFile, &dwSizeHigh); | ||
if (dwSizeLow > 256000) // 파일 사이즈가 너무 크면 지운다.. | ||
{ | ||
CloseHandle(hFile); | ||
// Remove the file if its size exceeds 512 KB | ||
if (fs::exists(s_fsLogFile) && fs::file_size(s_fsLogFile) > 512000) { | ||
fs::remove(s_fsLogFile); | ||
hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
return; | ||
} | ||
} | ||
|
||
::SetFilePointer(hFile, 0, NULL, FILE_END); // 추가 하기 위해서 파일의 끝으로 옮기고.. | ||
|
||
char szBuff[1024]; | ||
SYSTEMTIME time; | ||
GetLocalTime(&time); | ||
DWORD dwRWC = 0; | ||
|
||
sprintf(szBuff, "---------------------------------------------------------------------------\r\n"); | ||
int iLength = lstrlen(szBuff); | ||
WriteFile(hFile, szBuff, iLength, &dwRWC, NULL); | ||
std::ofstream ofsFile(s_fsLogFile, std::ios::app | std::ios::binary); | ||
if (!ofsFile) { | ||
return; | ||
} | ||
|
||
sprintf(szBuff, "// Begin writing log... [%.2d/%.2d %.2d:%.2d]\r\n", time.wMonth, time.wDay, time.wHour, | ||
time.wMinute); | ||
iLength = lstrlen(szBuff); | ||
WriteFile(hFile, szBuff, iLength, &dwRWC, NULL); | ||
auto tpNow = std::chrono::system_clock::now(); | ||
auto tmLocal = std::chrono::current_zone()->to_local(tpNow); | ||
|
||
CloseHandle(hFile); | ||
ofsFile << "---------------------------------------------------------------------------\n"; | ||
ofsFile << std::format("// Begin writing log... [{:%m/%d %H:%M}]\n", tmLocal); | ||
} | ||
|
||
void CLogWriter::Close() { | ||
HANDLE hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
hFile = NULL; | ||
} | ||
if (s_fsLogFile.empty()) { | ||
return; | ||
} | ||
|
||
if (hFile) { | ||
::SetFilePointer(hFile, 0, NULL, FILE_END); // 추가 하기 위해서 파일의 끝으로 옮기고.. | ||
std::lock_guard<std::mutex> lock(s_mtxLog); | ||
|
||
char szBuff[1024]; | ||
SYSTEMTIME time; | ||
GetLocalTime(&time); | ||
DWORD dwRWC = 0; | ||
std::ofstream ofsFile(s_fsLogFile, std::ios::app | std::ios::binary); | ||
if (!ofsFile) { | ||
return; | ||
} | ||
|
||
sprintf(szBuff, "// End writing log... [%.2d/%.2d %.2d:%.2d]\r\n", time.wMonth, time.wDay, time.wHour, | ||
time.wMinute); | ||
int iLength = lstrlen(szBuff); | ||
WriteFile(hFile, szBuff, iLength, &dwRWC, NULL); | ||
auto tpNow = std::chrono::system_clock::now(); | ||
auto tmLocal = std::chrono::current_zone()->to_local(tpNow); | ||
|
||
sprintf(szBuff, "---------------------------------------------------------------------------\r\n"); | ||
iLength = lstrlen(szBuff); | ||
WriteFile(hFile, szBuff, iLength, &dwRWC, NULL); | ||
ofsFile << std::format("// End writing log... [{:%m/%d %H:%M}]\n", tmLocal); | ||
ofsFile << "---------------------------------------------------------------------------\n"; | ||
|
||
CloseHandle(hFile); | ||
hFile = NULL; | ||
} | ||
s_fsLogFile.clear(); | ||
} | ||
|
||
void CLogWriter::Write(const char * lpszFormat, ...) { | ||
if (s_fsLogFile.empty() || NULL == lpszFormat) { | ||
void CLogWriter::WriteImpl(std::string_view szFmt, const std::format_args fmtArgs) { | ||
if (s_fsLogFile.empty() || szFmt.empty()) { | ||
return; | ||
} | ||
|
||
static char szFinal[1024]; | ||
static SYSTEMTIME time; | ||
GetLocalTime(&time); | ||
szFinal[0] = NULL; | ||
|
||
DWORD dwRWC = 0; | ||
sprintf(szFinal, " [%.2d:%.2d:%.2d] ", time.wHour, time.wMinute, time.wSecond); | ||
|
||
static char szBuff[1024]; | ||
szBuff[0] = NULL; | ||
va_list argList; | ||
va_start(argList, lpszFormat); | ||
vsprintf(szBuff, lpszFormat, argList); | ||
va_end(argList); | ||
N3_DEBUG("CLogWriter::Write: {}", szBuff); | ||
|
||
lstrcat(szFinal, szBuff); | ||
lstrcat(szFinal, "\r\n"); | ||
int iLength = lstrlen(szFinal); | ||
|
||
HANDLE hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
hFile = CreateFileW(s_fsLogFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (INVALID_HANDLE_VALUE == hFile) { | ||
hFile = NULL; | ||
} | ||
} | ||
std::lock_guard<std::mutex> lock(s_mtxLog); | ||
|
||
std::string szMsg = std::vformat(szFmt, fmtArgs); | ||
N3_DEBUG("CLogWriter::Write: {:s}", szMsg); | ||
|
||
auto tpNow = std::chrono::system_clock::now(); | ||
auto tmLocal = std::chrono::current_zone()->to_local(tpNow); | ||
|
||
if (hFile) { | ||
::SetFilePointer(hFile, 0, NULL, FILE_END); // 추가 하기 위해서 파일의 끝으로 옮기고.. | ||
std::string szLog = std::format(" [{:%H:%M:%S}] ", tmLocal) + szMsg + "\n"; | ||
|
||
WriteFile(hFile, szFinal, iLength, &dwRWC, NULL); | ||
CloseHandle(hFile); | ||
std::ofstream ofsFile(s_fsLogFile, std::ios::app | std::ios::binary); | ||
if (ofsFile) { | ||
ofsFile.write(szLog.c_str(), szLog.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
// LogWriter.h: interface for the CLogWriter class. | ||
// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
class CLogWriter { | ||
protected: | ||
// static HANDLE s_hFile; | ||
static fs::path s_fsLogFile; | ||
|
||
public: | ||
static void Open(const fs::path & fsFile); | ||
static void Close(); | ||
static void Write(const char * lpszFormat, ...); | ||
|
||
CLogWriter(); | ||
virtual ~CLogWriter(); | ||
template <class... T> static void Write(std::string_view szFmt, T &&... fmtArgs) { | ||
WriteImpl(szFmt, std::make_format_args(fmtArgs...)); | ||
} | ||
|
||
private: | ||
static void WriteImpl(std::string_view szFmt, const std::format_args fmtArgs); | ||
|
||
private: | ||
static fs::path s_fsLogFile; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.