#pragma once #include #include #include "Logstdafx.h" #include "COTSUtilityDllFunExport.h" #include "CLogFile.h" #define LOGINFO_SWITCH static PostLogMsg postlog; 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"; csLogPath += lpLogName; g_LpLogFile = new class CCLogFile(csLogPath); } else { csLogPath += lpLogName; g_LpLogFile = new class CCLogFile(csLogPath); } } } void LogToFile(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 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); CString str; str.Format("%s(%ld) ", szFileName, lLine); str.Append((LPCTSTR)strLog.c_str()); SendLogMessageToNlog(str, (int)PostLogLevel::info); //g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str()); } void LogWarn(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...) { std::string strLog; va_list arglist; va_start(arglist, lpTraceLog); strLog = getFormattedStr(lpTraceLog, arglist); va_end(arglist); LPCTSTR szFileName = ::PathFindFileName(szFile); CString str; str.Format("%s(%ld) ", szFileName, lLine); str.Append((LPCTSTR)strLog.c_str()); SendLogMessageToNlog(str, (int)PostLogLevel::warn); } 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); SendLogMessageToNlog((LPCSTR)strLog.c_str(), (int)PostLogLevel::info); LPCTSTR szFileName = ::PathFindFileName(szFile); CString str; str.Format("%s(%ld) ", szFileName, lLine); str.Append((LPCTSTR)strLog.c_str()); SendLogMessageToNlog(str, (int)PostLogLevel::info); //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); CString str; str.Format("%s(%ld) ", szFileName, lLine); str.Append((LPCTSTR)strLog.c_str()); SendLogMessageToNlog(str, (int)PostLogLevel::error); //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; } } bool SendLogMessageToNlog(LPCTSTR Msg, int postLogLevel) { static HWND m_hWnd; if (m_hWnd == NULL) { HWND hd = GetDesktopWindow(); //得到桌面窗口 hd = GetWindow(hd, GW_CHILD); //得到屏幕上第一个子窗口 char s[1000] = { 0 }; int num = 1; while (hd != NULL) //循环得到所有的子窗口 { memset(s, 0, 1000); GetWindowText(hd, s, 1000); CString winTxt(s); if (winTxt.Find(_T("OTSMeasureApp"))>-1)//find the measure app mainform { m_hWnd = hd; break; } if (winTxt.Find(_T("OTSReportApp")) > -1)//find the report app mainform { m_hWnd = hd; break; } if (winTxt.Find(_T("SysMgrApp")) > -1)//find the sysmgrApp app mainform { m_hWnd = hd; break; } hd = GetNextWindow(hd, GW_HWNDNEXT); } } if (NULL == m_hWnd) { return false; } CString msg = Msg; /*PostLogMsg postlog;*/ memset(&postlog, 0, sizeof(PostLogMsg)); postlog.logLevel = postLogLevel; int l = msg.GetLength(); if (l > 2000) { l = 2000; } for (int i = 0; i < l; i++) { postlog.logMsg[i] = msg.GetAt(i); } ::SendMessage(m_hWnd, 0x400+101, 0, (LPARAM)&postlog); return true; }