MyLogger.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "Logstdafx.h"
  3. #include "COTSUtilityDllFunExport.h"
  4. //#include "CLogFile.h"
  5. #include "MyLogger.h"
  6. #define LOGINFO_SWITCH
  7. MyLogger::MyLogger()
  8. {
  9. }
  10. void MyLogger::InitLogFile(LPCTSTR lpLogName)
  11. {
  12. // is log file created?
  13. if (NULL == g_LpLogFile)
  14. {
  15. CString csLogPath = GetCompanyLogPathName();
  16. if (!Exists(csLogPath))
  17. {
  18. csLogPath = ".\\Log\\SysMgrApp";
  19. csLogPath += lpLogName;
  20. g_LpLogFile = new class CCLogFile(csLogPath);
  21. }
  22. else
  23. {
  24. csLogPath += lpLogName;
  25. g_LpLogFile = new class CCLogFile(csLogPath);
  26. }
  27. }
  28. }
  29. void MyLogger::LogTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  30. {
  31. if (NULL == g_LpLogFile)
  32. {
  33. return;
  34. }
  35. LPCTSTR szFileName = ::PathFindFileName(szFile);
  36. g_LpLogFile->TraceProgress(szFileName, lLine, lpTraceLog);
  37. }
  38. void MyLogger::LogInfoTrace(LPCTSTR szFile, long lLine, LPCTSTR lpTraceLog, ...)
  39. {
  40. #ifdef LOGINFO_SWITCH
  41. if (NULL == g_LpLogFile)
  42. {
  43. return;
  44. }
  45. LPCTSTR szFileName = ::PathFindFileName(szFile);
  46. g_LpLogFile->TraceProgress(szFileName, lLine, lpTraceLog);
  47. #endif
  48. }
  49. void MyLogger::LogErrorTrace(LPCTSTR szFile, long lLine, LPCTSTR lpErrorTrace, ...)
  50. {
  51. if (NULL == g_LpLogFile)
  52. {
  53. return;
  54. }
  55. LPCTSTR szFileName = ::PathFindFileName(szFile);
  56. g_LpLogFile->TraceError(szFileName, lLine, lpErrorTrace);
  57. }
  58. void MyLogger::LogBinaryTrace(LPCTSTR szHead, BYTE * pbyData, UINT nLen)
  59. {
  60. if (NULL == g_LpLogFile)
  61. {
  62. return;
  63. }
  64. g_LpLogFile->LogBinaryData(szHead, pbyData, nLen);
  65. }
  66. void MyLogger::WaitingTime(int iMilliseconds)
  67. {
  68. if (NULL == m_LpUTools)
  69. {
  70. m_LpUTools = new CUtilityTools();
  71. }
  72. m_LpUTools->WaitingWithEventLoop(iMilliseconds);
  73. }
  74. void MyLogger::EndLogFile()
  75. {
  76. if (NULL != g_LpLogFile)
  77. {
  78. delete g_LpLogFile;
  79. g_LpLogFile = NULL;
  80. }
  81. if (NULL != m_LpUTools)
  82. {
  83. delete m_LpUTools;
  84. m_LpUTools = NULL;
  85. }
  86. }
  87. MyLogger::~MyLogger()
  88. {
  89. EndLogFile();
  90. }
  91. CString MyLogger::GetOSCommonDataPathName()
  92. {
  93. CString strPathName(".\\");
  94. return strPathName;
  95. }
  96. CString MyLogger::GetCompanyLogPathName()
  97. { // get common data pathname string
  98. CString strCommonDataPathName = GetOSCommonDataPathName();
  99. if (strCommonDataPathName.IsEmpty())
  100. {
  101. // failed to get company system data folder string
  102. LogErrorTrace(__FILE__, __LINE__, _T("GetCompayLogPathName: failed to common data pathname string."));
  103. return _T("");
  104. }
  105. // software package log path
  106. // e.g. "c:\ProgramData\Log\"
  107. CString strCompanyLogPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_LOG + _T("\\");
  108. // return software package log path
  109. return strCompanyLogPathName;
  110. }
  111. BOOL MyLogger::Exists(LPCTSTR a_sPath)
  112. {
  113. return ::PathFileExists(a_sPath) == TRUE;
  114. }