From e37020d3493dd65177ad9cec5cfd9b192863f270 Mon Sep 17 00:00:00 2001 From: sunwen Date: Fri, 31 May 2024 15:08:24 +0800 Subject: [PATCH] feat: Add system log. --- src/UserOperationLogForm.cpp | 4 +- src/device/DeviceManager.cpp | 15 ++++++- src/device/UsctStateManager.cpp | 6 ++- src/log/SystemOperationLog.cpp | 72 +++++++++++++++++++++++++++++++++ src/log/SystemOperationLog.h | 28 +++++++++++++ src/log/UserOperationLog.cpp | 5 ++- src/main.cpp | 2 + src/recon/ReconManager.cpp | 17 ++++++-- 8 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 src/log/SystemOperationLog.cpp create mode 100644 src/log/SystemOperationLog.h diff --git a/src/UserOperationLogForm.cpp b/src/UserOperationLogForm.cpp index f34b521..79829dd 100644 --- a/src/UserOperationLogForm.cpp +++ b/src/UserOperationLogForm.cpp @@ -15,11 +15,11 @@ #include "event/EventCenter.h" QString fileNameToDate(QString fileName) { - return fileName.split("log/")[1].replace("-op.log", ""); + return fileName.split("log/UserOperationLog/")[1].replace("-op.log", ""); } QString dateToFileName(QString date) { - return QString("./log/") + date + QString("-op.log"); + return QString("./log/UserOperationLog/") + date + QString("-op.log"); } UserOperationLogForm::UserOperationLogForm(QWidget* parent) { diff --git a/src/device/DeviceManager.cpp b/src/device/DeviceManager.cpp index 2a25fea..1f95afb 100644 --- a/src/device/DeviceManager.cpp +++ b/src/device/DeviceManager.cpp @@ -24,6 +24,7 @@ #include "dialogs/MultyMessageDialog.h" #include "UsctStateManager.h" #include "utilities/ScanProcessSequence.h" +#include "log/SystemOperationLog.h" #define TRIGGER_EVENT EventCenter::Default()->triggerEvent @@ -753,6 +754,7 @@ void DeviceManager::processAlarm(const QString& aAlarm) int alarmCode = jsonObj["code"].toInt(); QString alarm = QString::number(alarmCode); qDebug()<<"processAlarm : "<= 400 && alarmCode < 500) { if(mIsScanning) @@ -823,6 +825,7 @@ bool DeviceManager::startFullScan(const QString& aPatientInfo) { QString message = QString("Dms connection error. Reason:%1").arg(result.mData); THROW_ERROR(message); + LOG_SYS_OPERATION("Start full scan failed. " + message) return false; } @@ -831,10 +834,12 @@ bool DeviceManager::startFullScan(const QString& aPatientInfo) if(code != 0) { QString msg = jsonObj["info"].toString(); + LOG_SYS_OPERATION("Start full scan failed. " + msg) THROW_ERROR(msg); return false; } mCurrentScanMeasurementID = jsonObj["measurement id"].toString(); + //LOG_SYS_OPERATION("Start full scan succeed.") return true; } @@ -1263,12 +1268,14 @@ void DeviceManager::updateReconConnectionState(bool aIsConnected) void DeviceManager::startScanProcess() { + LOG_SYS_OPERATION("Start scan process.") if( !UsctStateManager::getInstance()->getState(ReconConnectionState) || !UsctStateManager::getInstance()->getState(ReconState) || !UsctStateManager::getInstance()->getState(ReconDBState)) { - TRIGGER_EVENT(StopScanProcess, nullptr, nullptr); QString errorMessage = tr("Recon error, can't start scan process"); + LOG_SYS_OPERATION(errorMessage) + TRIGGER_EVENT(StopScanProcess, nullptr, nullptr); THROW_ERROR(errorMessage); return; } @@ -1279,6 +1286,7 @@ void DeviceManager::startScanProcess() void DeviceManager::stopScanProcess() { AppGlobalValues::setInProcessing(false); + LOG_SYS_OPERATION("Stop scan process.") stopAutoLocate(); } @@ -1298,12 +1306,14 @@ bool DeviceManager::startAutoLocate() { emit startAutoLocateResult(true); mGetAutoLocatePositionTimer = startTimer(1000); + //LOG_SYS_OPERATION("Start auto locate succeed.") return true; } } TRIGGER_EVENT(StopScanProcess, nullptr, nullptr); QString errorMessage = tr("Start auto locate failed"); THROW_ERROR(errorMessage) + LOG_SYS_OPERATION("Start auto locate failed.") return false; } @@ -1366,11 +1376,13 @@ void DeviceManager::checkDataQuality() QJsonObject jsonObj = toJsonObject(result.mData); if(jsonObj["qualities"].toInt() == 1 ) { + LOG_SYS_OPERATION("Check data quality succeed. Data quality is good.") prepareFinishScan(true); return; } else { + LOG_SYS_OPERATION("Check data quality succeed. Data quality is bad.") QString errorMessage = tr("The data quality is low, please restart the data scan.") + jsonObj["info"].toString(); prepareFinishScan(false, errorMessage); TRIGGER_EVENT(StopScanProcess, nullptr, nullptr); @@ -1380,6 +1392,7 @@ void DeviceManager::checkDataQuality() QString errorMessage = tr("Dms connection error"); prepareFinishScan(false, errorMessage); TRIGGER_EVENT(StopScanProcess, nullptr, nullptr); + LOG_SYS_OPERATION("Check data quality failed. " + errorMessage) return; } diff --git a/src/device/UsctStateManager.cpp b/src/device/UsctStateManager.cpp index fe96f2e..771e098 100644 --- a/src/device/UsctStateManager.cpp +++ b/src/device/UsctStateManager.cpp @@ -1,6 +1,7 @@ #include "UsctStateManager.h" #include "event/EventCenter.h" +#include "log/SystemOperationLog.h" UsctStateManager* UsctStateManager::getInstance() { @@ -34,11 +35,14 @@ void UsctStateManager::setState(int aStateCode, bool aState) if(aState) { int msg = aStateCode; + LOG_SYS_OPERATION(getStateErrorMessage(aStateCode) + " recovery.") EventCenter::Default()->triggerEvent(ErrorStateUnactive, nullptr, (QObject*)&msg); } else { - QPair msg(QPair(aStateCode, getStateErrorMessage(aStateCode))); + QString errorMessage = getStateErrorMessage(aStateCode); + LOG_SYS_OPERATION(errorMessage) + QPair msg(QPair(aStateCode, errorMessage)); EventCenter::Default()->triggerEvent(ErrorStateActive, nullptr, (QObject*)&msg); } } diff --git a/src/log/SystemOperationLog.cpp b/src/log/SystemOperationLog.cpp new file mode 100644 index 0000000..d94f6c0 --- /dev/null +++ b/src/log/SystemOperationLog.cpp @@ -0,0 +1,72 @@ +#include "SystemOperationLog.h" + +#include "appvals/AppGlobalValues.h" + +#include + +namespace +{ + const QString LOG_DIR = "./log/SystemOperationLog"; + const QString SYS_LOG_SUFFIX = "-sys.log"; +} + +SystemOperationLog* SystemOperationLog::getInstance() +{ + static SystemOperationLog instance; + return &instance; +} + + +SystemOperationLog::SystemOperationLog() + : mCurrentFileName() + , mLogFile() + , mStreamOut() +{ + QDir log_dir("./"); + if (!log_dir.exists("log")) log_dir.mkdir("log"); + if (!log_dir.exists(LOG_DIR)) log_dir.mkdir(LOG_DIR); + mCurrentFileName = LOG_DIR + QDate::currentDate().toString("/yyyy-MM-dd") + SYS_LOG_SUFFIX; + mLogFile.setFileName(mCurrentFileName); + if (mLogFile.exists()) + { + mLogFile.open(QFile::OpenModeFlag::Append | QFile::OpenModeFlag::Text); + } + else + { + mLogFile.open(QFile::OpenModeFlag::NewOnly | QFile::OpenModeFlag::Text); + } + mStreamOut.setDevice(&mLogFile); +} + +SystemOperationLog::~SystemOperationLog() +{ + if (mLogFile.isOpen()) + { + mLogFile.flush(); + mLogFile.close(); + } +} + +void SystemOperationLog::reloadFile() +{ + QString newFileName = LOG_DIR + QDate::currentDate().toString("/yyyy-MM-dd") + SYS_LOG_SUFFIX; + if (newFileName == mCurrentFileName && !AppGlobalValues::InProcessing().toBool()) return; + mLogFile.close(); + mLogFile.setFileName(newFileName); + if (mLogFile.exists()) + { + mLogFile.open(QFile::OpenModeFlag::Append | QFile::OpenModeFlag::Text); + } + else + { + mLogFile.open(QFile::OpenModeFlag::NewOnly | QFile::OpenModeFlag::Text); + } + mStreamOut.setDevice(&mLogFile); +} + +void SystemOperationLog::log(const QString &aOperationText) +{ + reloadFile(); + QDateTime now = QDateTime::currentDateTime(); + mStreamOut << now.toString(Qt::DateFormat::ISODateWithMs).replace("T","\t")<<"\t"< +#include +#include + +#define LOG_SYS_OPERATION(...)\ +SystemOperationLog::getInstance()->log(__VA_ARGS__); + +class SystemOperationLog +{ +public: + static SystemOperationLog* getInstance(); + void log(const QString& aOperationText); + void reloadFile(); + +private: + SystemOperationLog(); + ~SystemOperationLog(); + +private: + QString mCurrentFileName; + QFile mLogFile; + QTextStream mStreamOut; +}; + +#endif // SYSTEMOPERATIONLOG_H diff --git a/src/log/UserOperationLog.cpp b/src/log/UserOperationLog.cpp index a67e903..a117b52 100644 --- a/src/log/UserOperationLog.cpp +++ b/src/log/UserOperationLog.cpp @@ -9,12 +9,13 @@ namespace { - const QString logDir = "./log"; + const QString logDir = "./log/UserOperationLog"; } void UserOperationLog::init() { QDir log_dir("./"); - if (!log_dir.exists(logDir)) log_dir.mkdir("log"); + if (!log_dir.exists("log")) log_dir.mkdir("log"); + if (!log_dir.exists(logDir)) log_dir.mkdir(logDir); currentFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log"); logFile.setFileName(currentFileName); if (logFile.exists()) diff --git a/src/main.cpp b/src/main.cpp index fed6156..a300bf8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "utilities/TouchScreenSignalSender.h" #include "keyboard/KeyboardManager.h" #include "appvals/AppGlobalValues.h" +#include "log/SystemOperationLog.h" #include QString loadFontFromFile(QString path) @@ -93,6 +94,7 @@ int main(int argc, char* argv[]) MainWindow w; DialogManager::Default()->init(&w); UserOperationLog::Default()->init(); + SystemOperationLog::getInstance(); QObject::connect(TouchScreenSignalSender::getInstance(), SIGNAL(touchScreen()), Locker::getInstance(), SLOT(refreshTimer())); QList gestures; diff --git a/src/recon/ReconManager.cpp b/src/recon/ReconManager.cpp index 5068968..c425a8c 100644 --- a/src/recon/ReconManager.cpp +++ b/src/recon/ReconManager.cpp @@ -1,6 +1,7 @@ #include "ReconManager.h" #include "ReconClient.h" #include "json/jsonobject.h" +#include "log/SystemOperationLog.h" #include #include "QDebug" @@ -54,11 +55,13 @@ void ReconManager::createEmptyScan(const QString& aScanID, const QString& aPath) if(result.good()) { qDebug()<< "create empty scan success, scanID: "<< aScanID; + LOG_SYS_OPERATION("create empty scan success, scanID: " + aScanID) emit createEmptyScanResponsed(true, aScanID); } else { qDebug()<< "Create empty scan fail by %s\n" << result.error().data(); + LOG_SYS_OPERATION("Create empty scan failed. " + QString(result.error().data())) emit createEmptyScanResponsed(false, aScanID, result.error().data()); } } @@ -70,11 +73,13 @@ void ReconManager::createScan(const QString& aScanID, const QString& aStudyUID,c if(response.good()) { qDebug()<< "Recon create scan success, scanID: "<< aScanID; + LOG_SYS_OPERATION("Recon create scan success, scanID: " + aScanID) emit createScanResponsed(true, aScanID); } else { qDebug()<< "Recon create scan failed by " << response.error().data(); + LOG_SYS_OPERATION("Recon create scan failed" + QString(response.error().data())) emit createScanResponsed(false, aScanID, response.error().data()); } } @@ -130,11 +135,13 @@ void ReconManager::setPacsSettings(const QString& aClientAETitle, const QString& { QString msg = QString("Set PACS settings failed : ") + response.error().data(); qDebug()<< msg; + LOG_SYS_OPERATION(msg) emit setPacsSettingsResponsed(false, QVariant::fromValue(msg)); return; } - - emit setPacsSettingsResponsed(true, QVariant::fromValue(QString("Set PACS settings succeed."))); + QString message = "Set PACS settings succeed."; + LOG_SYS_OPERATION(message) + emit setPacsSettingsResponsed(true, QVariant::fromValue(message)); } void ReconManager::setMppsSettings(bool aIsOpen, const QString& aServerAETitle, const QString& aServerIP, int aServerPort) @@ -145,11 +152,13 @@ void ReconManager::setMppsSettings(bool aIsOpen, const QString& aServerAETitle, { QString msg = QString("Set MPPS settings failed : ") + response.error().data(); qDebug()<< msg; + LOG_SYS_OPERATION(msg) emit setMppsSettingsResponsed(false, QVariant::fromValue(msg)); return; } - - emit setMppsSettingsResponsed(true, QVariant::fromValue(QString("Set MPPS settings succeed."))); + QString message = "Set MPPS settings succeed."; + LOG_SYS_OPERATION(message) + emit setMppsSettingsResponsed(true, QVariant::fromValue(message)); } void ReconManager::getReconVersion()