COTSUtilityDllFunExport.cpp 4.3 KB

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