You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB
C

1 year ago
#pragma once
#include <QMutex>
//#include <QList>
#include <QObject>
class LogManager : public QObject
{
Q_OBJECT
public:
static LogManager *GetInstance() {
if(!m_pInstance)
{
m_mutex.lock();
if(!m_pInstance)
{
m_pInstance = new LogManager();
}
m_mutex.unlock();
}
return m_pInstance;
}
LogManager(const LogManager&)= delete;
LogManager& operator=(const LogManager&)= delete;
void addLog(QString file, int line, QString func, int level, QString log);
void addRepeatFiltedLog(QString file, int line, QString func, int level, QString log);
bool isSameLog(QString file, int line, QString func, int level, QString log);
QString buildLogString(QString file, int line, QString func, int level, QString log);
signals:
void logAdded(QString log);
private:
LogManager() : m_lastFile(""), m_lastLine(0), m_lastFunc(""), m_lastLevel(0), m_lastLog(""), m_lastTimes(0)
{
};
private:
static LogManager *m_pInstance;
static QMutex m_mutex;
// 保存最后一次日志内容,批量消息时可节省控件
QString m_lastFile;
int m_lastLine;
QString m_lastFunc;
int m_lastLevel;
QString m_lastLog;
unsigned int m_lastTimes;
//QList<QString> m_logList;
private:
// 垃圾回收类它的唯一工作就是在析构函数中删除DeviceFactory的实例
class Garbo
{
public:
~Garbo()
{
if (LogManager::m_pInstance)
{
// 销毁设备管理单例实例
delete LogManager::m_pInstance;
}
}
};
// 定义一个静态成员,在程序结束时,系统会调用它的析构函数
static Garbo m_Garbo;
};
#define logDebug(log) LogManager::GetInstance()->addLog(__FILE__, __LINE__, __FUNCTION__, 0, log)
#define logInfo(log) LogManager::GetInstance()->addLog(__FILE__, __LINE__, __FUNCTION__, 1, log)
#define logWarn(log) LogManager::GetInstance()->addLog(__FILE__, __LINE__, __FUNCTION__, 2, log)
#define logError(log) LogManager::GetInstance()->addLog(__FILE__, __LINE__, __FUNCTION__, 3, log)