#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

// 日志宏定义
#define LOG(level, module, message) Logger::instance().log(Logger::level, module, message)
#define LOG_DEBUG(module, message) LOG(DEBUG, module, message)
#define LOG_INFO(module, message) LOG(INFO, module, message)
#define LOG_WARN(module, message) LOG(WARNING, module, message)
#define LOG_ERROR(module, message) LOG(ERROR, module, message)
#define LOG_FATAL(module, message) LOG(FATAL, module, message)

class Logger : public QObject
{
    Q_OBJECT

public:
    enum LogLevel
    {
        FATAL = 0,
        ERROR,
        WARNING,
        INFO,
        DEBUG
    };

    //获取单例实例
    static Logger& instance();
    void log(LogLevel, const QString&, const QString&);

private:
    explicit Logger();
    ~Logger();
    //禁止拷贝
    Logger(const Logger&) = delete; //delete关键字表示该函数不可用，包括编译器自动生成的函数
    Logger& operator=(const Logger&) = delete;

    void initialize();
    void loadConfig(/*const QString&*/); //本系统是通过Settings类进行配置文件信息读取
    void setLogFile(const QString&);
    void shutdown();

    void writeToFile(const QString&);
    void rollLogFiles(); //当文件大小超过设置上线时会触发'滚动'
    QString formatLogMessage(LogLevel, const QString&, const QString&);

    struct LogEntry
    {
        QDateTime time;
        LogLevel level;
        QString module;
        QString message;
        Qt::HANDLE threadId;
    };

    //配置参数
    LogLevel m_logLevel;
    QString m_logFilePath;
    QFile m_logFile;
    qint64 m_maxFileSize;
    int m_maxBackupFiles;
    bool m_outputToConsole;
    bool m_outputOtFile;
};

#endif //LOGGER_H
