feat: merge the UserOperationLog and SystemOperationLog in LogManager.
This commit is contained in:
47
src/log/LogManager.cpp
Normal file
47
src/log/LogManager.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "LogManager.h"
|
||||
|
||||
#include <QThread>
|
||||
|
||||
LogManager* LogManager::getInstance()
|
||||
{
|
||||
static LogManager instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
LogManager::LogManager()
|
||||
: QObject()
|
||||
, mThread(new QThread())
|
||||
, mSysLog(new SystemOperationLog())
|
||||
, mUserLog(new UserOperationLog())
|
||||
{
|
||||
mSysLog->moveToThread(mThread);
|
||||
mUserLog->moveToThread(mThread);
|
||||
connect(this, &LogManager::doWriteSystemOperationLog, mSysLog, &SystemOperationLog::log);
|
||||
connect(this, &LogManager::doWriteUserOperationLog, mUserLog, &UserOperationLog::log);
|
||||
mThread->start();
|
||||
}
|
||||
|
||||
LogManager::~LogManager()
|
||||
{
|
||||
mThread->quit();
|
||||
mThread->wait();
|
||||
|
||||
delete mUserLog;
|
||||
delete mSysLog;
|
||||
mThread->deleteLater();
|
||||
}
|
||||
|
||||
void LogManager::writeSystemOperationLog(const QString& aMessage)
|
||||
{
|
||||
emit doWriteSystemOperationLog(aMessage);
|
||||
}
|
||||
|
||||
void LogManager::writeUserOperationLog(const QString& aMessage)
|
||||
{
|
||||
emit doWriteUserOperationLog(aMessage);
|
||||
}
|
||||
|
||||
QString LogManager::getCurrentUserOperationLogFile()
|
||||
{
|
||||
return mUserLog->currentLogFile();
|
||||
}
|
||||
41
src/log/LogManager.h
Normal file
41
src/log/LogManager.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef LOGMANAGER_H
|
||||
#define LOGMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "SystemOperationLog.h"
|
||||
#include "UserOperationLog.h"
|
||||
|
||||
class QThread;
|
||||
|
||||
#define LOG_SYS_OPERATION(...)\
|
||||
LogManager::getInstance()->writeSystemOperationLog(__VA_ARGS__);
|
||||
|
||||
|
||||
#define LOG_USER_OPERATION(...)\
|
||||
LogManager::getInstance()->writeUserOperationLog(__VA_ARGS__);
|
||||
|
||||
class LogManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static LogManager* getInstance();
|
||||
void writeSystemOperationLog(const QString& aMessage);
|
||||
void writeUserOperationLog(const QString& aMessage);
|
||||
QString getCurrentUserOperationLogFile();
|
||||
|
||||
signals:
|
||||
void doWriteSystemOperationLog(const QString& aMessage);
|
||||
void doWriteUserOperationLog(const QString& aMessage);
|
||||
|
||||
private:
|
||||
LogManager();
|
||||
~LogManager();
|
||||
|
||||
private:
|
||||
QThread* mThread;
|
||||
SystemOperationLog* mSysLog;
|
||||
UserOperationLog* mUserLog;
|
||||
};
|
||||
|
||||
#endif // LOGMANAGER_H
|
||||
@@ -10,15 +10,9 @@ namespace
|
||||
const QString SYS_LOG_SUFFIX = "-sys.log";
|
||||
}
|
||||
|
||||
SystemOperationLog* SystemOperationLog::getInstance()
|
||||
{
|
||||
static SystemOperationLog instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
|
||||
SystemOperationLog::SystemOperationLog()
|
||||
: mCurrentFileName()
|
||||
: QObject()
|
||||
, mCurrentFileName()
|
||||
, mLogFile()
|
||||
, mStreamOut()
|
||||
{
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
#ifndef SYSTEMOPERATIONLOG_H
|
||||
#define SYSTEMOPERATIONLOG_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
#define LOG_SYS_OPERATION(...)\
|
||||
SystemOperationLog::getInstance()->log(__VA_ARGS__);
|
||||
|
||||
class SystemOperationLog
|
||||
class SystemOperationLog : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static SystemOperationLog* getInstance();
|
||||
void log(const QString& aOperationText);
|
||||
SystemOperationLog();
|
||||
~SystemOperationLog();
|
||||
void reloadFile();
|
||||
|
||||
private:
|
||||
SystemOperationLog();
|
||||
~SystemOperationLog();
|
||||
public slots:
|
||||
void log(const QString& aOperationText);
|
||||
|
||||
private:
|
||||
QString mCurrentFileName;
|
||||
|
||||
@@ -12,6 +12,24 @@ namespace
|
||||
const QString logDir = "./log/UserOperationLog";
|
||||
}
|
||||
|
||||
UserOperationLog::UserOperationLog()
|
||||
: QObject()
|
||||
, currentFileName()
|
||||
, logFile()
|
||||
, out()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
UserOperationLog::~UserOperationLog()
|
||||
{
|
||||
if (logFile.isOpen())
|
||||
{
|
||||
logFile.flush();
|
||||
logFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
void UserOperationLog::init() {
|
||||
QDir log_dir("./");
|
||||
if (!log_dir.exists("log")) log_dir.mkdir("log");
|
||||
@@ -44,20 +62,6 @@ QString addSpace(const char* str)
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
QString getOperationName(UserOperation operation)
|
||||
{
|
||||
switch (operation) {
|
||||
#define ADD_OPERATION(name)\
|
||||
case name: return addSpace(#name);
|
||||
USER_OPERATIONS()
|
||||
#undef ADD_OPERATION
|
||||
default:
|
||||
return "unknow error";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void UserOperationLog::log(const QString& aOperationText)
|
||||
{
|
||||
reloadFile();
|
||||
|
||||
@@ -1,53 +1,18 @@
|
||||
#ifndef GUI_USEROPERATIONLOG_H
|
||||
#define GUI_USEROPERATIONLOG_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
#define USER_OPERATIONS()\
|
||||
ADD_OPERATION(Login)\
|
||||
ADD_OPERATION(Logout)\
|
||||
ADD_OPERATION(ChangePassword)\
|
||||
ADD_OPERATION(ChangeUserName)\
|
||||
ADD_OPERATION(AddPatient)\
|
||||
ADD_OPERATION(ChangePatientInfo)\
|
||||
ADD_OPERATION(DeletePatient)\
|
||||
ADD_OPERATION(SelectPatient)\
|
||||
ADD_OPERATION(StartRefresh)\
|
||||
ADD_OPERATION(StopScan)\
|
||||
ADD_OPERATION(StopPreview)\
|
||||
ADD_OPERATION(StartPreview)\
|
||||
ADD_OPERATION(StartScan)\
|
||||
ADD_OPERATION(ConfirmError)\
|
||||
ADD_OPERATION(AdminChangeAcountInformation)
|
||||
|
||||
#define LOG_USER_OPERATION(...)\
|
||||
UserOperationLog::Default()->log(__VA_ARGS__);
|
||||
|
||||
|
||||
enum UserOperation{
|
||||
#define ADD_OPERATION(name) name,\
|
||||
|
||||
USER_OPERATIONS()
|
||||
#undef ADD_OPERATION
|
||||
};
|
||||
class UserOperationLog {
|
||||
class UserOperationLog : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
UserOperationLog(){}
|
||||
~UserOperationLog(){
|
||||
if (logFile.isOpen())
|
||||
{
|
||||
logFile.flush();
|
||||
logFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
UserOperationLog();
|
||||
~UserOperationLog();
|
||||
void init();
|
||||
static UserOperationLog* Default(){
|
||||
static UserOperationLog d;
|
||||
return &d;
|
||||
}
|
||||
static void cleanHistoryLog();
|
||||
|
||||
static QStringList getHistoryLogFiles();
|
||||
|
||||
Reference in New Issue
Block a user