CkLog.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // CkLog.cpp: implementation of the CkLog class.
  2. #include "CkLog.h"
  3. #if defined(CKLOG)
  4. extern CTraceManager *pLOG;
  5. #endif
  6. //获取完整路径中的文件名称
  7. std::string GetFileName(char* pstrFullPath)
  8. {
  9. std::string strRet = "";
  10. if(nullptr == pstrFullPath) return strRet;
  11. char* pLastSlash = strrchr(pstrFullPath, '/'); //find last slash
  12. if(nullptr == pLastSlash)
  13. strRet = pstrFullPath;
  14. else
  15. strRet = pLastSlash + 1;
  16. return strRet;
  17. }
  18. bool GetCurModulePath(char* pstrLibPath, char* pstrLibName)
  19. {
  20. Dl_info path_info;
  21. char sPath[256];
  22. memset(sPath, 0x00, sizeof(sPath));
  23. dladdr((void*)GetCurModulePath, &path_info); //根据内存空间所在地址 获取当前模块完整路径
  24. strcpy(sPath, path_info.dli_fname);
  25. char *pLastSlash=strrchr(sPath, '/'); //find last slash
  26. if(nullptr != pLastSlash)
  27. {
  28. *pLastSlash=0;
  29. if(nullptr != pstrLibPath)
  30. strcpy(pstrLibPath, sPath);
  31. if(nullptr != pstrLibName)
  32. strcpy(pstrLibName, pLastSlash + 1);
  33. return true;
  34. }
  35. return false;
  36. }
  37. int getFileVer(char* sFile, short &ch1, short &ch2)
  38. {
  39. ch1 = 0;
  40. ch2 = 0;
  41. char* pFind = strstr(sFile, ".so");
  42. char* pTemp = pFind;
  43. while(pTemp)
  44. {
  45. pFind = pTemp;
  46. pTemp = strstr(pFind + 3, ".so");
  47. }
  48. if(pFind == nullptr) return 0;
  49. pTemp = pFind - 1;
  50. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  51. if(*pTemp == '.')
  52. ch2 = atoi(pTemp + 1);
  53. pTemp--;
  54. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  55. if(*pTemp == '.')
  56. ch1 = atoi(pTemp + 1);
  57. return 1;
  58. }
  59. //加载SO
  60. void* LoadSO(char* pstrSOName)
  61. {
  62. void* pSO = nullptr;
  63. char sPath[256];
  64. char sFullPath[256];
  65. memset(sPath, 0x00, sizeof(sPath));
  66. memset(sFullPath, 0x00, sizeof(sFullPath));
  67. bool bRet = GetCurModulePath(sPath, nullptr);
  68. if(bRet)
  69. {
  70. sprintf(sFullPath, "%s/%s", sPath, pstrSOName); //拼出so完整路径
  71. pSO = dlopen(sFullPath, RTLD_LAZY); //RTLD_NOW
  72. if (nullptr == pSO)
  73. printf("LoadSO()->dlopen(%s, RTLD_LAZY) fail, dlerror=[%s]", sFullPath, dlerror());
  74. else
  75. printf("LoadSO()->dlopen(%s, RTLD_LAZY) OK!!!", sFullPath);
  76. }
  77. return pSO;
  78. }
  79. void* GetPtr(void* pVoid)
  80. {
  81. void* ptrTmp = pVoid;
  82. return ptrTmp;
  83. }
  84. bool CkLog_FuncTracer::m_bCkLogExtendInf = false;
  85. //==> Enter {GetEncryptFunc}, file: {PinPadClassImpl_ZT598M.cpp}, line: {1001}.
  86. //根据整型变量占空间的大小判断类型
  87. CkLog_FuncTracer::CkLog_FuncTracer(const char* pszFunc, const char* pszFile, int nLine, void* pfRet, int iSize)
  88. {
  89. memset(m_pszFunc, 0x00, sizeof(m_pszFunc));
  90. memset(m_pszFile, 0x00, sizeof(m_pszFile));
  91. snprintf(m_pszFunc,sizeof(m_pszFunc), "%s", pszFunc);
  92. snprintf(m_pszFile,sizeof(m_pszFile), "%s", pszFile);
  93. m_nLine = nLine;
  94. m_psfRet = nullptr; //INT16
  95. m_pifRet = nullptr; //INT32
  96. m_plfRet = nullptr; //INT64
  97. if(sizeof(short) == iSize)
  98. m_psfRet =(short*)pfRet;
  99. else if(sizeof(int) == iSize)
  100. m_pifRet =(int*)pfRet;
  101. else if(sizeof(long) == iSize)
  102. m_plfRet =(long*)pfRet;
  103. #if defined(CKLOG)
  104. pLOG->TraceInFormat(TRM_INT, TRM_LV_COMMN, "==> Enter {%s}, file: {%s}, line: {%d}.", m_pszFunc, m_pszFile, m_nLine);
  105. #endif
  106. }
  107. //<== Leave {DevOpen}, file: {PinPadClassImpl_ZT598M.cpp}, line: {171}.
  108. CkLog_FuncTracer::~CkLog_FuncTracer()
  109. {
  110. #if defined(CKLOG)
  111. if(nullptr != m_psfRet)
  112. pLOG->TraceInFormat(TRM_INT, TRM_LV_COMMN, "<== Leave {%s}, file: {%s}, line: {%d} , short RetCode=[%d].", m_pszFunc, m_pszFile, m_nLine, *m_psfRet);
  113. else if(nullptr != m_pifRet)
  114. pLOG->TraceInFormat(TRM_INT, TRM_LV_COMMN, "<== Leave {%s}, file: {%s}, line: {%d} , int RetCode=[%d].", m_pszFunc, m_pszFile, m_nLine, *m_pifRet);
  115. else if(nullptr != m_plfRet)
  116. pLOG->TraceInFormat(TRM_INT, TRM_LV_COMMN, "<== Leave {%s}, file: {%s}, line: {%d} , long RetCode=[%ld].", m_pszFunc, m_pszFile, m_nLine, *m_plfRet);
  117. else
  118. pLOG->TraceInFormat(TRM_INT, TRM_LV_COMMN, "<== Leave {%s}, file: {%s}, line: {%d}.", m_pszFunc, m_pszFile, m_nLine);
  119. #endif
  120. }