#include "qlog.hpp"
#include <QFile>
#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 \t%2 \t%3").arg(text).arg(strDateTime).arg(msg);
    QString logDir = "/opt/compatibility_layer/logs/";
    QDir dir(logDir);
    if (!dir.exists())
    {
        qDebug() << "DEBUG - 日志目录不存在，创建日志目录";
        dir.mkdir(logDir);
    }
    QString logFile = logDir + "dockerAgent.log";
    QFile outFile(logFile);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream textStream(&outFile);
    textStream << strMessage << "\r\n";
    outFile.flush();
    outFile.close();
}
