LHQLogAPI.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "LHQLogAPI.h"
  2. #include <QTime>
  3. #include <QCoreApplication>
  4. // #include "./External/Core/MyDebugLog.h"
  5. /* 加载动态库 */
  6. void LH_LoadLib()
  7. {
  8. QString libPath = QCoreApplication::applicationDirPath();
  9. #ifdef Q_OS_WINDOWS
  10. libPath += "/LHQLog.dll";
  11. g_apiLhQLog.Load(libPath);
  12. #else
  13. libPath += "/libLHQLog.so";
  14. g_apiLhQLog.Load(libPath);
  15. #endif
  16. }
  17. void LH_LoadLib(const QString& strLibPath)
  18. {
  19. g_apiLhQLog.Load(strLibPath);
  20. }
  21. CLHQLogApi g_apiLhQLog;
  22. void WRITE_LOG(QString log)
  23. {
  24. int idx = log.indexOf(QLatin1String("]"));
  25. QString msg = idx >= 0 ? log.mid(idx + 1) : log;
  26. #ifdef QT_NO_DEBUG
  27. g_apiLhQLog.DoWriteLog(ELT_Log_Common, msg);
  28. #else
  29. // log.insert(idx+1, QTime::currentTime().toString("hh:mm:ss.zzz"));
  30. // LOG(log);
  31. #endif
  32. }
  33. void WRITE_ERROR(QString log)
  34. {
  35. int idx = log.indexOf(QLatin1String("]"));
  36. QString msg = idx >= 0 ? log.mid(idx + 1) : log;
  37. #ifdef QT_NO_DEBUG
  38. g_apiLhQLog.DoWriteLog(ELT_Log_Error, msg);
  39. #else
  40. // log.insert(idx+1, QTime::currentTime().toString("hh:mm:ss.zzz"));
  41. // LOG(log);
  42. #endif
  43. }
  44. void WRITE_LOG_DEBUG(QString log)
  45. {
  46. int idx = log.indexOf(QLatin1String("]"));
  47. QString msg = idx >= 0 ? log.mid(idx + 1) : log;
  48. #ifdef QT_NO_DEBUG
  49. g_apiLhQLog.DoWriteLog(ELT_Log_Debug, msg);
  50. #else
  51. // log.insert(idx+1, QTime::currentTime().toString("hh:mm:ss.zzz"));
  52. // LOG(log);
  53. #endif
  54. }
  55. CLHQLogApi::CLHQLogApi()
  56. {
  57. fnDoInitial = nullptr;
  58. fnDoWriteLog = nullptr;
  59. fnDoWriteFileLog = nullptr;
  60. }
  61. CLHQLogApi::~CLHQLogApi()
  62. {
  63. m_QLib.unload();
  64. }
  65. bool CLHQLogApi::Load(QString file)
  66. {
  67. m_QLib.setFileName(file);
  68. if (!m_QLib.load())
  69. {
  70. qDebug() << QString("Liberal %1 load failed!").arg(file);
  71. //LOG(QString("模块%1加载失败-1").arg(file));
  72. return false;
  73. }
  74. // qDebug() << QString("Liberal %1 load success!").arg(file);
  75. fnDoInitial = (FunDoInitial)m_QLib.resolve("Initial");
  76. fnDoWriteLog = (FunDoWriteLog)m_QLib.resolve("WriteLog");
  77. fnDoWriteFileLog = (FunDoWriteFileLog)m_QLib.resolve("WriteFileLog");
  78. if(fnDoInitial == nullptr || fnDoWriteLog == nullptr || fnDoWriteFileLog == nullptr)
  79. {
  80. //LOG(QString("模块%1加载失败,接口错误-2").arg(file));
  81. return false;
  82. }
  83. return true;
  84. }
  85. int CLHQLogApi::DoInitial(QString strLogName)
  86. {
  87. m_strLogName = strLogName;
  88. if(fnDoInitial == nullptr) return -1;
  89. return fnDoInitial(strLogName.toUtf8());
  90. }
  91. int CLHQLogApi::DoWriteLogEx(QString strLogName, int nLogType, QString strLog)
  92. {
  93. if(fnDoWriteLog == nullptr) return -1;
  94. return fnDoWriteLog(strLogName.toUtf8(), nLogType, strLog.toUtf8());
  95. }
  96. int CLHQLogApi::DoWriteLog(int nLogType, QString strLog)
  97. {
  98. if(fnDoWriteLog == nullptr) return -1;
  99. return fnDoWriteLog(m_strLogName.toUtf8(), nLogType, strLog.toUtf8());
  100. }
  101. int CLHQLogApi::DoWriteFileLog(QString strLogFilePath, QString strLog)
  102. {
  103. if(fnDoWriteFileLog == nullptr) return -1;
  104. return fnDoWriteFileLog(strLogFilePath.toUtf8(), strLog.toUtf8());
  105. }