diff --git a/wCenterWindow/CLogger.cpp b/wCenterWindow/CLogger.cpp index 9bcc9a6..fe79fa3 100644 --- a/wCenterWindow/CLogger.cpp +++ b/wCenterWindow/CLogger.cpp @@ -28,14 +28,17 @@ #include #include -inline wchar_t* CLogger::GetTimeStamp() { +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); return logTimeBuffer; } -void CLogger::Out(const wchar_t* fmt, ...) { - if (fsLogFile.is_open()) { +void CLogger::Out(const wchar_t* fmt, ...) +{ + if (fsLogFile.is_open()) + { va_list args; va_start(args, fmt); EnterCriticalSection(&cs); @@ -46,16 +49,19 @@ void CLogger::Out(const wchar_t* fmt, ...) { } } -void CLogger::Init() { +void CLogger::Init() +{ wchar_t szPath[MAX_PATH] = { 0 }; DWORD dwPathLength = GetModuleFileNameW(NULL, szPath, MAX_PATH); DWORD dwError = GetLastError(); - if (ERROR_INSUFFICIENT_BUFFER == dwError) { - MessageBoxW(NULL, L"Warning!\nPath to log file is too long! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING); + if (ERROR_INSUFFICIENT_BUFFER == dwError) + { + MessageBoxW(NULL, L"Warning!\nPath to log file is too long! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); return; } - if (NULL == dwPathLength) { - MessageBoxW(NULL, L"Warning!\nCan't get application's filename! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING); + if (NULL == dwPathLength) + { + MessageBoxW(NULL, L"Warning!\nCan't get application's filename! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); return; } @@ -69,26 +75,31 @@ void CLogger::Init() { log_path = L"D:\\test.log"; #endif fsLogFile.open(log_path, std::ios::trunc); - if (fsLogFile.is_open()) { + 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; } - else { - MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING); + else + { + MessageBoxW(NULL, L"Warning!\nCan't create log file! Working without logging.", szAppTitle.c_str(), MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); } } -CLogger::CLogger(const wchar_t* _appTitle) { +CLogger::CLogger(const wchar_t* _appTitle) +{ szAppTitle = _appTitle; szAppTitleVer = _appTitle; Init(); } -CLogger::~CLogger() { - if (fsLogFile) { +CLogger::~CLogger() +{ + if (fsLogFile) + { fsLogFile << GetTimeStamp() << "Stop log." << std::endl; fsLogFile.close(); DeleteCriticalSection(&cs);