Log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef LOG_H
  2. #define LOG_H
  3. #include <QObject>
  4. #include <cstdlib>
  5. #include <mutex>
  6. #include <cassert>
  7. #include "log4qt/consoleappender.h"
  8. #include "log4qt/logger.h"
  9. #include "log4qt/logmanager.h"
  10. #include "log4qt/ttcclayout.h"
  11. #include "log4qt/propertyconfigurator.h"
  12. #include "log4qt/basicconfigurator.h"
  13. #define LogDebug LogHelper(__FILE__, __LINE__, Q_FUNC_INFO).debug
  14. #define LogInfo LogHelper(__FILE__, __LINE__, Q_FUNC_INFO).info
  15. #define LogWarn LogHelper(__FILE__, __LINE__, Q_FUNC_INFO).warn
  16. #define LogError LogHelper(__FILE__, __LINE__, Q_FUNC_INFO).error
  17. #define LogFatal LogHelper(__FILE__, __LINE__, Q_FUNC_INFO).fatal
  18. #define LogQt LogHelper(__FILE__, __LINE__, Q_FUNC_INFO)
  19. class Log : public QObject
  20. {
  21. Q_OBJECT
  22. LOG4QT_DECLARE_QCLASS_LOGGER
  23. public:
  24. Log(QObject *parent = Q_NULLPTR);
  25. ~Log();
  26. static Log* instance();
  27. virtual void debug(const QString &log);
  28. virtual void info(const QString &log);
  29. virtual void warn(const QString &log);
  30. virtual void error(const QString &log);
  31. virtual void fatal(const QString &log);
  32. };
  33. class LogHelper
  34. {
  35. Q_DISABLE_COPY(LogHelper)
  36. public:
  37. Q_DECL_CONSTEXPR LogHelper()
  38. : version_(1),
  39. line_(0),
  40. file_(Q_NULLPTR),
  41. function_(Q_NULLPTR)
  42. {
  43. }
  44. LogHelper(const char *fileName, int lineNumber, const char *functionName);
  45. enum LogType
  46. {
  47. LGDebugMsg,
  48. LGInfoMsg,
  49. LGWarningMsg,
  50. LGErrorMsg,
  51. LGFatalMsg
  52. };
  53. QString TemplateParameter ()
  54. {
  55. return "";
  56. }
  57. template <typename T, typename ... Args>
  58. QString TemplateParameter(T head, Args ... args)
  59. {
  60. return QString("%1 ").arg(head) + TemplateParameter(args...);
  61. }
  62. template <typename T, typename ... Args>
  63. void debug(T head, Args ... args)
  64. {
  65. QString logmsg = QString("%1 ").arg(head) + TemplateParameter(args...);
  66. writelogToLocal(LGDebugMsg,logmsg);
  67. }
  68. template <typename T, typename ... Args>
  69. void info(T head, Args ... args)
  70. {
  71. QString logmsg = QString("%1 ").arg(head) + TemplateParameter(args...);
  72. writelogToLocal(LGInfoMsg,logmsg);
  73. }
  74. template <typename T, typename ... Args>
  75. void warn(T head, Args ... args)
  76. {
  77. QString logmsg = QString("%1 ").arg(head) + TemplateParameter(args...);
  78. writelogToLocal(LGWarningMsg,logmsg);
  79. }
  80. template <typename T, typename ... Args>
  81. void error(T head, Args ... args )
  82. {
  83. QString logmsg = QString("%1 ").arg(head) + TemplateParameter(args...);
  84. writelogToLocal(LGErrorMsg,logmsg);
  85. }
  86. template <typename T, typename ... Args>
  87. void fatal(T head, Args ... args)
  88. {
  89. QString logmsg = QString("%1 ").arg(head) + TemplateParameter(args...);
  90. writelogToLocal(LGFatalMsg,logmsg);
  91. }
  92. virtual void writelogToLocal(LogType logtype, const QString& logmsg);
  93. virtual void copy(const LogHelper &logContext)
  94. {
  95. Q_UNUSED(logContext)
  96. }
  97. template <typename T>
  98. inline LogHelper &operator<<(T logmsg)
  99. {
  100. writelogToLocal(LGInfoMsg, QString("%1").arg(logmsg));
  101. return *this;
  102. }
  103. public:
  104. static bool showFileFuncLine_;
  105. int version_;
  106. int line_;
  107. const char *file_;
  108. const char *function_;
  109. };
  110. #endif // LOG_H