COTSUtilityDllFunExport.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. std::string getFormattedStr( const char *strFormat, va_list arglist)
  9. {
  10. CString str;
  11. str.FormatV(strFormat, arglist);
  12. std::string strFormatted = str;
  13. return strFormatted;
  14. }
  15. CString GetLogPathName()
  16. { // get common data pathname string
  17. CString strCommonDataPathName = ".\\";
  18. CString strLogPathName = strCommonDataPathName + "Log" + _T("\\");
  19. // return software package log path
  20. return strLogPathName;
  21. }
  22. BOOL Exists(LPCTSTR a_sPath)
  23. {
  24. return ::PathFileExists(a_sPath) == TRUE;
  25. }
  26. const CString g_csLogName = "OTSLog";
  27. const CString STR_LOG = _T("Log");
  28. CCLogFile* g_LpLogFile = NULL;
  29. void InitLogFile(LPCTSTR lpLogName)
  30. {
  31. // is log file created?
  32. if (NULL == g_LpLogFile)
  33. {
  34. CString csLogPath = GetLogPathName();
  35. if (!Exists(csLogPath))
  36. {
  37. csLogPath = ".\\Log\\SysMgrApp";
  38. csLogPath += lpLogName;
  39. g_LpLogFile = new class CCLogFile(csLogPath);
  40. }
  41. else
  42. {
  43. csLogPath += lpLogName;
  44. g_LpLogFile = new class CCLogFile(csLogPath);
  45. }
  46. }
  47. }
  48. void LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  49. {
  50. if (NULL == g_LpLogFile)
  51. {
  52. InitLogFile(g_csLogName);
  53. }
  54. std::string strLog;
  55. va_list arglist;
  56. va_start(arglist, lpTraceLog);
  57. strLog = getFormattedStr( lpTraceLog, arglist);
  58. va_end(arglist);
  59. LPCTSTR szFileName = ::PathFindFileName(szFile);
  60. g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  61. }
  62. void LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  63. {
  64. #ifdef LOGINFO_SWITCH
  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. g_LpLogFile->TraceProgress(szFileName, lLine, (LPCSTR)strLog.c_str());
  76. #endif
  77. }
  78. void LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...)
  79. {
  80. if (NULL == g_LpLogFile)
  81. {
  82. InitLogFile(g_csLogName);
  83. }
  84. std::string strLog;
  85. va_list arglist;
  86. va_start(arglist, lpErrorTrace);
  87. strLog = getFormattedStr(lpErrorTrace, arglist);
  88. va_end(arglist);
  89. LPCTSTR szFileName = ::PathFindFileName(szFile);
  90. g_LpLogFile->TraceError(szFileName, lLine, (LPCSTR)strLog.c_str());
  91. }
  92. void LogBinaryTrace(LPCTSTR szHead, BYTE * pbyData, UINT nLen)
  93. {
  94. if (NULL == g_LpLogFile)
  95. {
  96. InitLogFile(g_csLogName);
  97. }
  98. g_LpLogFile->LogBinaryData(szHead, pbyData, nLen);
  99. }
  100. void WaitingTime(int iMilliseconds)
  101. {
  102. auto m_LpUTools = new CUtilityTools();
  103. m_LpUTools->WaitingWithEventLoop(iMilliseconds);
  104. delete m_LpUTools;
  105. }
  106. void EndLogFile()
  107. {
  108. if (NULL != g_LpLogFile)
  109. {
  110. delete g_LpLogFile;
  111. g_LpLogFile = NULL;
  112. }
  113. }