#include "QLogUtil.h"


QLogUtil::QLogUtil(QObject *parent) : QObject(parent)
{

}

void QLogUtil::writeRawDataLog(QString filename, QString content)
{
    content.append("\n");
    QFile rawLogFile(filename);
    rawLogFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
    rawLogFile.write(content.toUtf8());
    rawLogFile.close();
}

void QLogUtil::writeChannelDataLog(QString filename, QString content)
{
    content.append("\n");
    QFile chLogFile(filename);
    chLogFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
    chLogFile.write(content.toUtf8());
    chLogFile.close();
}

void QLogUtil::writeDebugLog(QString message)
{

}

void QLogUtil::writeInfoLog(QString message)
{

}

void QLogUtil::writeRawDataLogByDate(QString date, QString filename, QString content)
{
    QString basePath = SettingConfig::getInstance().BASE_LOG_PATH;
    QString datePath = basePath + date + "/";

    QStringList pathList;
    pathList.append(basePath);
    pathList.append(datePath);

    // 检查并创建目录
    QLogUtil::checkLogPath(pathList);

    // 输出内容到日志中
    content.append("\n");
    QFile rawLogFile(datePath + filename);
    rawLogFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
    rawLogFile.write(content.toUtf8());
    rawLogFile.close();
}

void QLogUtil::writeChannelDataLogByDate(QString date, QString filename, QString content)
{
    QString basePath = SettingConfig::getInstance().BASE_LOG_PATH;
    QString datePath = basePath + date + "/";

    QStringList pathList;
    pathList.append(basePath);
    pathList.append(datePath);

    // 检查并创建目录
    QLogUtil::checkLogPath(pathList);

    // 输出内容到日志中
    content.append("\n");
    QFile chLogFile(datePath + filename);
    chLogFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
    chLogFile.write(content.toUtf8());
    chLogFile.close();
}

void QLogUtil::checkLogPath(QStringList path)
{
    QDir dir;
    bool exist = false;

    for (int i = 0; i < path.size(); i++)
    {
        // 判断是否存在日志根目录
        exist = dir.exists(path.at(i));
        if (exist == false)
        {
            // 不存在则创建目录
            dir.mkdir(path.at(i));
        }
    }
}
