COTSUtilityDllFunExport.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  82. {
  83. #ifdef LOGINFO_SWITCH
  84. /*if (NULL == g_LpLogFile)
  85. {
  86. InitLogFile(g_csLogName);
  87. }*/
  88. std::string strLog;
  89. va_list arglist;
  90. va_start(arglist, lpTraceLog);
  91. strLog = getFormattedStr( lpTraceLog, arglist);
  92. va_end(arglist);
  93. SendLogMessageToNlog((LPCSTR)strLog.c_str(), (int)PostLogLevel::info);
  94. LPCTSTR szFileName = ::PathFindFileName(szFile);
  95. CString str;
  96. str.Format("%s(%ld) ", szFileName, lLine);
  97. str.Append((LPCTSTR)strLog.c_str());
  98. SendLogMessageToNlog(str, (int)PostLogLevel::info);
  99. //g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  100. #endif
  101. }
  102. void LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...)
  103. {
  104. /*if (NULL == g_LpLogFile)
  105. {
  106. InitLogFile(g_csLogName);
  107. }*/
  108. std::string strLog;
  109. va_list arglist;
  110. va_start(arglist, lpErrorTrace);
  111. strLog = getFormattedStr(lpErrorTrace, arglist);
  112. va_end(arglist);
  113. LPCTSTR szFileName = ::PathFindFileName(szFile);
  114. CString str;
  115. str.Format("%s(%ld) ", szFileName, lLine);
  116. str.Append((LPCTSTR)strLog.c_str());
  117. SendLogMessageToNlog(str, (int)PostLogLevel::error);
  118. //g_LpLogFile->TraceError(szFileName, lLine, (LPCSTR)strLog.c_str());
  119. }
  120. void LogBinaryTrace(LPCTSTR szHead, BYTE * pbyData, UINT nLen)
  121. {
  122. if (NULL == g_LpLogFile)
  123. {
  124. InitLogFile(g_csLogName);
  125. }
  126. g_LpLogFile->LogBinaryData(szHead, pbyData, nLen);
  127. }
  128. void WaitingTime(int iMilliseconds)
  129. {
  130. auto m_LpUTools = new CUtilityTools();
  131. m_LpUTools->WaitingWithEventLoop(iMilliseconds);
  132. delete m_LpUTools;
  133. }
  134. void EndLogFile()
  135. {
  136. if (NULL != g_LpLogFile)
  137. {
  138. delete g_LpLogFile;
  139. g_LpLogFile = NULL;
  140. }
  141. }
  142. bool SendLogMessageToNlog(LPCTSTR Msg, int postLogLevel)
  143. {
  144. static HWND m_hWnd;
  145. if (m_hWnd == NULL)
  146. {
  147. HWND hd = GetDesktopWindow(); //得到桌面窗口
  148. hd = GetWindow(hd, GW_CHILD); //得到屏幕上第一个子窗口
  149. char s[200] = { 0 };
  150. int num = 1;
  151. while (hd != NULL) //循环得到所有的子窗口
  152. {
  153. memset(s, 0, 200);
  154. GetWindowText(hd, s, 200);
  155. CString winTxt(s);
  156. if (winTxt.Find(_T("OTSMeasureApp"))>-1)//find the measure app mainform
  157. {
  158. m_hWnd = hd;
  159. break;
  160. }
  161. if (winTxt.Find(_T("OTSReportApp")) > -1)//find the report app mainform
  162. {
  163. m_hWnd = hd;
  164. break;
  165. }
  166. hd = GetNextWindow(hd, GW_HWNDNEXT);
  167. }
  168. }
  169. if (NULL == m_hWnd)
  170. {
  171. return false;
  172. }
  173. CString msg = Msg;
  174. /*PostLogMsg postlog;*/
  175. memset(&postlog, 0, sizeof(PostLogMsg));
  176. postlog.logLevel = postLogLevel;
  177. int l = msg.GetLength();
  178. if (l > 200)
  179. {
  180. l = 200;
  181. }
  182. for (int i = 0; i < l; i++)
  183. {
  184. postlog.logMsg[i] = msg.GetAt(i);
  185. }
  186. ::SendMessage(m_hWnd, 0x400+101, 0, (LPARAM)&postlog);
  187. return true;
  188. }