COTSUtilityDllFunExport.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <crtdbg.h>
  4. #include "Logstdafx.h"
  5. #include "COTSUtilityDllFunExport.h"
  6. #include "CLogFile.h"
  7. #define LOGINFO_SWITCH
  8. static PostLogMsg postlog;
  9. std::string getFormattedStr( const char *strFormat, va_list arglist)
  10. {
  11. CString str;
  12. str.FormatV(strFormat, arglist);
  13. std::string strFormatted = str;
  14. return strFormatted;
  15. }
  16. CString GetLogPathName()
  17. { // get common data pathname string
  18. CString strCommonDataPathName = ".\\";
  19. CString strLogPathName = strCommonDataPathName + "Log" + _T("\\");
  20. // return software package log path
  21. return strLogPathName;
  22. }
  23. BOOL Exists(LPCTSTR a_sPath)
  24. {
  25. return ::PathFileExists(a_sPath) == TRUE;
  26. }
  27. const CString g_csLogName = "OTSLog";
  28. const CString STR_LOG = _T("Log");
  29. CCLogFile* g_LpLogFile = NULL;
  30. void InitLogFile(LPCTSTR lpLogName)
  31. {
  32. // is log file created?
  33. if (NULL == g_LpLogFile)
  34. {
  35. CString csLogPath = GetLogPathName();
  36. if (!Exists(csLogPath))
  37. {
  38. csLogPath = ".\\Log";
  39. csLogPath += lpLogName;
  40. g_LpLogFile = new class CCLogFile(csLogPath);
  41. }
  42. else
  43. {
  44. csLogPath += lpLogName;
  45. g_LpLogFile = new class CCLogFile(csLogPath);
  46. }
  47. }
  48. }
  49. void LogToFile(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  50. {
  51. if (NULL == g_LpLogFile)
  52. {
  53. InitLogFile(g_csLogName);
  54. }
  55. std::string strLog;
  56. va_list arglist;
  57. va_start(arglist, lpTraceLog);
  58. strLog = getFormattedStr(lpTraceLog, arglist);
  59. va_end(arglist);
  60. LPCTSTR szFileName = ::PathFindFileName(szFile);
  61. g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  62. }
  63. void LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  64. {
  65. /*if (NULL == g_LpLogFile)
  66. {
  67. InitLogFile(g_csLogName);
  68. }*/
  69. std::string strLog;
  70. va_list arglist;
  71. va_start(arglist, lpTraceLog);
  72. strLog = getFormattedStr( lpTraceLog, arglist);
  73. va_end(arglist);
  74. LPCTSTR szFileName = ::PathFindFileName(szFile);
  75. CString str;
  76. str.Format("%s(%ld) ", szFileName, lLine);
  77. str.Append((LPCTSTR)strLog.c_str());
  78. SendLogMessageToNlog(str, (int)PostLogLevel::info);
  79. //g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  80. }
  81. void LogWarn(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  82. {
  83. std::string strLog;
  84. va_list arglist;
  85. va_start(arglist, lpTraceLog);
  86. strLog = getFormattedStr(lpTraceLog, arglist);
  87. va_end(arglist);
  88. LPCTSTR szFileName = ::PathFindFileName(szFile);
  89. CString str;
  90. str.Format("%s(%ld) ", szFileName, lLine);
  91. str.Append((LPCTSTR)strLog.c_str());
  92. SendLogMessageToNlog(str, (int)PostLogLevel::warn);
  93. }
  94. void LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  95. {
  96. #ifdef LOGINFO_SWITCH
  97. /*if (NULL == g_LpLogFile)
  98. {
  99. InitLogFile(g_csLogName);
  100. }*/
  101. std::string strLog;
  102. va_list arglist;
  103. va_start(arglist, lpTraceLog);
  104. strLog = getFormattedStr( lpTraceLog, arglist);
  105. va_end(arglist);
  106. SendLogMessageToNlog((LPCSTR)strLog.c_str(), (int)PostLogLevel::info);
  107. LPCTSTR szFileName = ::PathFindFileName(szFile);
  108. CString str;
  109. str.Format("%s(%ld) ", szFileName, lLine);
  110. str.Append((LPCTSTR)strLog.c_str());
  111. SendLogMessageToNlog(str, (int)PostLogLevel::info);
  112. //g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  113. #endif
  114. }
  115. void LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...)
  116. {
  117. /*if (NULL == g_LpLogFile)
  118. {
  119. InitLogFile(g_csLogName);
  120. }*/
  121. std::string strLog;
  122. va_list arglist;
  123. va_start(arglist, lpErrorTrace);
  124. strLog = getFormattedStr(lpErrorTrace, arglist);
  125. va_end(arglist);
  126. LPCTSTR szFileName = ::PathFindFileName(szFile);
  127. CString str;
  128. str.Format("%s(%ld) ", szFileName, lLine);
  129. str.Append((LPCTSTR)strLog.c_str());
  130. SendLogMessageToNlog(str, (int)PostLogLevel::error);
  131. //g_LpLogFile->TraceError(szFileName, lLine, (LPCSTR)strLog.c_str());
  132. }
  133. void LogBinaryTrace(LPCTSTR szHead, BYTE * pbyData, UINT nLen)
  134. {
  135. if (NULL == g_LpLogFile)
  136. {
  137. InitLogFile(g_csLogName);
  138. }
  139. g_LpLogFile->LogBinaryData(szHead, pbyData, nLen);
  140. }
  141. void WaitingTime(int iMilliseconds)
  142. {
  143. auto m_LpUTools = new CUtilityTools();
  144. m_LpUTools->WaitingWithEventLoop(iMilliseconds);
  145. delete m_LpUTools;
  146. }
  147. void EndLogFile()
  148. {
  149. if (NULL != g_LpLogFile)
  150. {
  151. delete g_LpLogFile;
  152. g_LpLogFile = NULL;
  153. }
  154. }
  155. bool SendLogMessageToNlog(LPCTSTR Msg, int postLogLevel)
  156. {
  157. static HWND m_hWnd;
  158. if (m_hWnd == NULL)
  159. {
  160. HWND hd = GetDesktopWindow(); //得到桌面窗口
  161. hd = GetWindow(hd, GW_CHILD); //得到屏幕上第一个子窗口
  162. char s[1000] = { 0 };
  163. int num = 1;
  164. while (hd != NULL) //循环得到所有的子窗口
  165. {
  166. memset(s, 0, 1000);
  167. GetWindowText(hd, s, 1000);
  168. CString winTxt(s);
  169. if (winTxt.Find(_T("OTSMeasureApp"))>-1)//find the measure app mainform
  170. {
  171. m_hWnd = hd;
  172. break;
  173. }
  174. if (winTxt.Find(_T("OTSReportApp")) > -1)//find the report app mainform
  175. {
  176. m_hWnd = hd;
  177. break;
  178. }
  179. if (winTxt.Find(_T("SysMgrApp")) > -1)//find the sysmgrApp app mainform
  180. {
  181. m_hWnd = hd;
  182. break;
  183. }
  184. hd = GetNextWindow(hd, GW_HWNDNEXT);
  185. }
  186. }
  187. if (NULL == m_hWnd)
  188. {
  189. return false;
  190. }
  191. CString msg = Msg;
  192. /*PostLogMsg postlog;*/
  193. memset(&postlog, 0, sizeof(PostLogMsg));
  194. postlog.logLevel = postLogLevel;
  195. int l = msg.GetLength();
  196. if (l > 2000)
  197. {
  198. l = 2000;
  199. }
  200. for (int i = 0; i < l; i++)
  201. {
  202. postlog.logMsg[i] = msg.GetAt(i);
  203. }
  204. ::SendMessage(m_hWnd, 0x400+101, 0, (LPARAM)&postlog);
  205. return true;
  206. }