logger.h 1.5 KB

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