• 666阅读
  • 0回复

[提问]使用QFile写日志文件,打开关闭后发现内存增加 [复制链接]

上一主题 下一主题
离线zainan1234
 

只看楼主 倒序阅读 楼主  发表于: 2023-05-06

qt版本是5.15.9。每次整点的时候内存都会增加,是代码哪里有问题吗?求助

SaveLogNew::SaveLogNew(QObject *parent)
    : QObject{parent}
{
    path = QString("%1/ReplayLog").arg(qApp->applicationDirPath());
    level_path = QString("%1/LevelLog").arg(qApp->applicationDirPath());
    //默认取应用程序可执行文件名称
    QString str = qApp->applicationFilePath();
    QStringList list = str.split("/");
    name = list.at(list.count() - 1).split(".").at(0);
    m_logFileName = "";
    m_logLevelfileName = "";
    m_logInitFileName = "";
    cleaning_timer = new QTimer(this);
    cleaning_timer->setInterval(8640000);
    connect(cleaning_timer, &QTimer::timeout,this,&SaveLogNew::CleanLogFile);
    CleanLogFile();
    cleaning_timer->start();

}

SaveLogNew* SaveLogNew::getInstance()
{
    if(m_save_log_new == nullptr){
        mutex_init.lock();
        if(m_save_log_new == nullptr){
            m_save_log_new = new SaveLogNew();
        }
        mutex_init.unlock();
    }
    return m_save_log_new;
}

void SaveLogNew::openLogFile()
{
    QDir dir(path);
    if (!dir.exists()) {
        dir.mkdir(path);
    }
    if (m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QFile::Text)) {
        m_logStream.setDevice(&m_logFile);
    } else {
        qDebug() << "Failed to open log file" << m_logFileName;
    }
}

void SaveLogNew::openInitLogFile()
{
    QDir dir(path);
    if (!dir.exists()) {
        dir.mkdir(path);
    }
    if (m_logInitFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
        m_logInitStream.setDevice(&m_logInitFile);
    } else {
        qDebug() << "Failed to open log file" << m_logInitFileName;
    }
}


void SaveLogNew::closeLogFile()
{
    m_logFile.close();

}

void SaveLogNew::closeInitLogFile()
{
    m_logInitFile.close();

}

void SaveLogNew::writeToLogFile(const QString& message)
{
    // 检查是否需要创建新的日志文件
    QString fileName;
    fileName = QString("%1/%2_log_%3.txt").arg(path).arg(name).arg(QDATEHOUR);
    if (fileName != m_logFileName) {
        createNewLogFile(fileName);
        openLogFile();
    }

    m_logStream << message << endl;
}

void SaveLogNew::writeToInitLogFile(const QString& message)
{
    // 检查是否需要创建新的日志文件
    QString fileName;
    fileName = QString("%1/init_%2_log_%3.txt").arg(path).arg(name).arg(QDATEHOUR);
    if (fileName != m_logInitFileName) {
        createNewInitLogFile(fileName);
        openLogFile();
    }

    m_logInitStream << message << endl;
    m_logInitStream.flush();
}

void SaveLogNew::createNewLogFile(QString& filename)
{
    closeLogFile();

    m_logFileName = filename;
    m_logFile.setFileName(m_logFileName);

}

void SaveLogNew::createNewInitLogFile(QString& filename)
{
    closeInitLogFile();

    m_logInitFileName = filename;
    m_logInitFile.setFileName(m_logInitFileName);

}

void SaveLogNew::saveLogFile(const QString& msg,int type)
{
    QString str_log = QString("%1-%2").arg(QDateTime::currentDateTime().toMSecsSinceEpoch()).arg(msg);
    mutex_log.lock();
    switch (type) {
    case MOVE:
        writeToLogFile(str_log);
        break;
    case INIT:
        writeToInitLogFile(str_log);
        break;
    default:
        break;
    }
    mutex_log.unlock();

}

void SaveLogNew::CleanLogFile()
{
    QDateTime now = QDateTime::currentDateTime();
    int clean_day;
    QFile file(ServerConifgFileName);
    if(file.exists() == false){
        QSettings serverConfigInfo(ServerConifgFileName,QSettings::IniFormat,0);
    }
    else{
        QSettings dbConfig(ServerConifgFileName, QSettings::IniFormat, 0);
        bool bIsContains = true;
        bIsContains = dbConfig.contains("LOG_INFO/Clean_Cycle_Day");
        if(bIsContains)
        {
            clean_day = dbConfig.value("LOG_INFO/Clean_Cycle_Day", QVariant()).toString().toInt();
        }
        else
        {
            clean_day = 5;
        }
    }
    QDir dir(path);
    QStringList nameFilters;
    QStringList foldernames;
    //#ifdef Q_OS_LINUX
    //    nameFilters<<"*.txt";
    //#else
    nameFilters<<"*";
    //#endif
    dir.setNameFilters(nameFilters);
    foreach(QString file, dir.entryList(nameFilters, QDir::Files)){
        foldernames += path + file;
    }
    foreach(QString fileName, foldernames)
    {
        QFileInfo info(fileName);
        QDateTime dt = info.metadataChangeTime();//获取文件最后一次修改时间
        int days = dt.daysTo(now);//获取文件相对于当前时间的天数
        if (days > clean_day)
        {
            QFile::remove(fileName);//大于5天,移除文件
        }
    }
}


快速回复
限100 字节
 
上一个 下一个