#include "qlog.h"
#include <QFile>
#include <QApplication>
#include <stdlib.h>
#include <stdio.h>
#include <QMutex>
#include <QMutexLocker>
#include <QDateTime>
#include <QtDebug>
#include <QTextStream>
#include <QDir>
#include <QStandardPaths>

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QMutex mutex;
    QMutexLocker locker(&mutex);
    QString text;
    switch (type)
    {
    case QtDebugMsg:
        text = QString("Debug: ");
        break;
    case QtInfoMsg:
        text = QString("Info: ");
        break;
    case QtWarningMsg:
        text = QString("Warning: ");
        break;
    case QtCriticalMsg:
        text = QString("Critical: ");
        break;
    case QtFatalMsg:
        text = QString("Fatal: ");
        break;
    default:
        text = QString("Debug: ");
    }
    QString context_info = QString("F2:(%1) L1:(%2)").arg(QString(context.file)).arg(context.line);
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString strMessage = QString("%1 %2 \t%3 \t%4").arg(text).arg(context_info).arg(strDateTime).arg(msg);
    // QString strMessage = QString("%1 \t%2 \t%3").arg(text).arg(strDateTime).arg(msg);

    QString logDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    QString logDir = homeDir + "/.log/compatibility_layer/";
    QDir dir(logDir);
    if (!dir.exists())
    {
        qDebug() << "DEBUG - 日志目录不存在，创建日志目录";
        dir.mkdir(logDir);
    }
    QString logFile = logDir + "rightclick.log";
    QFile outFile(logFile);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream textStream(&outFile);
    textStream << strMessage << "\r\n";
    outFile.flush();
    outFile.close();
}
