#include "LogManager.h" #include #include #include #include LogManager *LogManager::m_pInstance = nullptr; QMutex LogManager::m_mutex; QString LogManager::buildLogString(QString file, int line, QString func, int level, QString log) { QString s; s.append(QObject::tr("[%1 %2] ").arg(QDate::currentDate().toString("yyyy-MM-dd")).arg(QTime::currentTime().toString("hh:mm:ss"))); #ifdef _DEBUG s.append(QObject::tr("[%1 : %2] ").arg(file).arg(line)); s.append(QObject::tr("[%1] ").arg(func)); #endif s.append(QObject::tr("%1").arg(log)); return s; } void LogManager::addLog(QString file, int line, QString func, int level, QString log) { // QString s = buildLogString(file, line, func, level, log); // emit logAdded(s); addRepeatFiltedLog(file, line, func, level, log); } // 过滤重复日志 void LogManager::addRepeatFiltedLog(QString file, int line, QString func, int level, QString log) { if(isSameLog(file, line, func, level, log)) { m_lastTimes++; return; } else { if(m_lastTimes > 1) { QString lastLogString = buildLogString(m_lastFile, m_lastLine, m_lastFunc, m_lastLevel, m_lastLog); lastLogString.append(tr(" - repeat times[%1]").arg(m_lastTimes)); emit logAdded(lastLogString); } QString logString = buildLogString(file, line, func, level, log); emit logAdded(logString); m_lastFile = file; m_lastLine = line; m_lastFunc = func; m_lastLevel = level; m_lastLog = log; m_lastTimes = 1; } } bool LogManager::isSameLog(QString file, int line, QString func, int level, QString log) { if(!m_lastFile.isEmpty() && !m_lastFunc.isEmpty() && !m_lastLog.isEmpty() && (m_lastFile.compare(file) == 0) && (m_lastLine == line) && (m_lastFunc.compare(func) == 0) && (m_lastLevel == level) && (m_lastLog.compare(log) == 0)) { return true; } return false; }