123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- #pragma once
- #include <stdlib.h>
- #include <crtdbg.h>
- #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;
- }
|