logger.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef LOGGER_H
  2. #define LOGGER_H
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QDateTime>
  6. #include <QMutex>
  7. #include <QString>
  8. #include <QScopedPointer>
  9. class Logger
  10. {
  11. public:
  12. enum LogLevel {
  13. Debug,
  14. Info,
  15. Warning,
  16. Error,
  17. Critical
  18. };
  19. // Public static method to get the single instance of the Logger
  20. static Logger& getInstance(const QString &filePath = "");
  21. // Direct logging methods
  22. void debug(const QString &message);
  23. void info(const QString &message);
  24. void warn(const QString &message);
  25. void error(const QString &message);
  26. void critical(const QString &message);
  27. void setMaxFileSize(qint64 bytes);
  28. void setMaxBackupFiles(int count);
  29. // Make the destructor public so QScopedPointer can call it
  30. ~Logger(); // <-- CHANGE THIS LINE: MOVED FROM PRIVATE TO PUBLIC
  31. private:
  32. // Private constructor to prevent direct instantiation
  33. explicit Logger(const QString &filePath);
  34. // Delete copy constructor and assignment operator to prevent copying
  35. Logger(const Logger&) = delete;
  36. Logger& operator=(const Logger&) = delete;
  37. QFile m_logFile;
  38. QTextStream m_textStream;
  39. QMutex m_mutex;
  40. qint64 m_maxFileSize;
  41. int m_maxBackupFiles;
  42. void writeLog(LogLevel level, const QString &message);
  43. void rotateLogFile();
  44. QString logLevelToString(LogLevel level) const;
  45. // Static members for the singleton instance
  46. static QScopedPointer<Logger> m_instance;
  47. static QMutex m_instanceMutex;
  48. };
  49. #endif // LOGGER_H