#pragma once #include #include #include "Logstdafx.h" #include "COTSUtilityDllFunExport.h" #include "CLogFile.h" #define LOGINFO_SWITCH std::string getFormattedStr( const char *strFormat, va_list arglist) { CString str; str.FormatV(strFormat, arglist); std::string strFormatted = str; return strFormatted; } CString GetLogPathName() { // get common data pathname string CString strCommonDataPathName = ".\\"; CString strLogPathName = strCommonDataPathName + "Log" + _T("\\"); // return software package log path return strLogPathName; } BOOL Exists(LPCTSTR a_sPath) { return ::PathFileExists(a_sPath) == TRUE; } const CString g_csLogName = "OTSLog"; const CString STR_LOG = _T("Log"); CCLogFile* g_LpLogFile = NULL; void InitLogFile(LPCTSTR lpLogName) { // is log file created? if (NULL == g_LpLogFile) { CString csLogPath = GetLogPathName(); if (!Exists(csLogPath)) { csLogPath = ".\\Log\\SysMgrApp"; csLogPath += lpLogName; g_LpLogFile = new class CCLogFile(csLogPath); } else { csLogPath += lpLogName; g_LpLogFile = new class CCLogFile(csLogPath); } } } void LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...) { if (NULL == g_LpLogFile) { InitLogFile(g_csLogName); } std::string strLog; va_list arglist; va_start(arglist, lpTraceLog); strLog = getFormattedStr( lpTraceLog, arglist); va_end(arglist); LPCTSTR szFileName = ::PathFindFileName(szFile); g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str()); } void LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...) { #ifdef LOGINFO_SWITCH if (NULL == g_LpLogFile) { InitLogFile(g_csLogName); } std::string strLog; va_list arglist; va_start(arglist, lpTraceLog); strLog = getFormattedStr( lpTraceLog, arglist); va_end(arglist); LPCTSTR szFileName = ::PathFindFileName(szFile); g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str()); #endif } void LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...) { if (NULL == g_LpLogFile) { InitLogFile(g_csLogName); } std::string strLog; va_list arglist; va_start(arglist, lpErrorTrace); strLog = getFormattedStr(lpErrorTrace, arglist); va_end(arglist); LPCTSTR szFileName = ::PathFindFileName(szFile); g_LpLogFile->TraceError(szFileName, lLine, (LPCSTR)strLog.c_str()); } void LogBinaryTrace(LPCTSTR szHead, BYTE * pbyData, UINT nLen) { if (NULL == g_LpLogFile) { InitLogFile(g_csLogName); } g_LpLogFile->LogBinaryData(szHead, pbyData, nLen); } void WaitingTime(int iMilliseconds) { auto m_LpUTools = new CUtilityTools(); m_LpUTools->WaitingWithEventLoop(iMilliseconds); delete m_LpUTools; } void EndLogFile() { if (NULL != g_LpLogFile) { delete g_LpLogFile; g_LpLogFile = NULL; } }