From d60c0d72dfb004c0e43ab2768f3922fb635c166f Mon Sep 17 00:00:00 2001 From: Alexander <aleksandr.martynov@demlabs.net> Date: Tue, 24 Sep 2019 13:39:56 -0400 Subject: [PATCH] [+] clear logs when service shut down delete clearLogModel --- .../DapCommandController.cpp | 7 +-- CellFrameDashboardGUI/DapCommandController.h | 7 +-- .../DapServiceController.cpp | 50 ++++++----------- CellFrameDashboardGUI/DapServiceController.h | 2 - .../DapChainDashboardService.cpp | 8 --- .../DapChainDashboardService.h | 2 - .../DapChainLogHandler.cpp | 56 ++++++++++--------- .../DapChainLogHandler.h | 3 +- 8 files changed, 52 insertions(+), 83 deletions(-) diff --git a/CellFrameDashboardGUI/DapCommandController.cpp b/CellFrameDashboardGUI/DapCommandController.cpp index 7a3cc5b53..99f4dd5de 100755 --- a/CellFrameDashboardGUI/DapCommandController.cpp +++ b/CellFrameDashboardGUI/DapCommandController.cpp @@ -39,11 +39,6 @@ void DapCommandController::processCommandResult() emit sigCommandResult(reply->response().result()); } -void DapCommandController::clearLogModel() -{ - emit onClearLogModel(); -} - /// Get node logs. void DapCommandController::getNodeLogs() { @@ -68,7 +63,7 @@ void DapCommandController::processChangedLog() // QStringList tempLogModel; // for(int x{0}; x < aLogModel.count(); ++x) // tempLogModel.append(aLogModel.at(x).toString()); - emit onLogModel(); + emit onChangeLogModel(); } /// Handling service response for receiving node logs. diff --git a/CellFrameDashboardGUI/DapCommandController.h b/CellFrameDashboardGUI/DapCommandController.h index 78152aba3..fcfce5213 100755 --- a/CellFrameDashboardGUI/DapCommandController.h +++ b/CellFrameDashboardGUI/DapCommandController.h @@ -42,10 +42,8 @@ signals: void sendNodeStatus(const QVariant& aData); /// void executeCommandChanged(const QString& result); - /// Signal for cleaning log - void onClearLogModel(); - /// - void onLogModel(); + /// Signal for changing logs + void onChangeLogModel(); /// Signal for sending new transaction history void sendHistory(const QVariant& aData); @@ -102,7 +100,6 @@ public slots: void executeCommand(const QString& command); - void clearLogModel(); /// Get node logs. void getNodeLogs(); diff --git a/CellFrameDashboardGUI/DapServiceController.cpp b/CellFrameDashboardGUI/DapServiceController.cpp index 3b8f210af..514393449 100755 --- a/CellFrameDashboardGUI/DapServiceController.cpp +++ b/CellFrameDashboardGUI/DapServiceController.cpp @@ -3,6 +3,8 @@ #include "DapLogMessage.h" #include "DapChainWallet.h" +#include <QRegularExpression> + DapServiceController::DapServiceController(QObject *apParent) : QObject(apParent) { @@ -29,6 +31,8 @@ void DapServiceController::closeClient() void DapServiceController::init(DapServiceClient *apDapServiceClient) { m_pDapServiceClient = apDapServiceClient; + + connect(m_pDapServiceClient, SIGNAL(sigDisconnected()), SLOT(clearLogModel())); // Creating rpc controller m_pDapCommandController = new DapCommandController(apDapServiceClient->getClientSocket(), this); @@ -51,7 +55,7 @@ void DapServiceController::init(DapServiceClient *apDapServiceClient) connect(m_pDapCommandController, SIGNAL(executeCommandChanged(QString)), SLOT(processExecuteCommandInfo(QString))); connect(m_pDapCommandController, SIGNAL(sendNodeNetwork(QVariant)), this, SLOT(processGetNodeNetwork(QVariant))); - connect(m_pDapCommandController, SIGNAL(onLogModel()), SLOT(get())); + connect(m_pDapCommandController, SIGNAL(onChangeLogModel()), SLOT(getNodeLogs())); connect(&DapChainNodeNetworkModel::getInstance(), SIGNAL(requestNodeNetwork()), this, SLOT(getNodeNetwork())); connect(&DapChainNodeNetworkModel::getInstance(), SIGNAL(requestNodeStatus(bool)), this, SLOT(setNodeStatus(bool))); @@ -80,7 +84,7 @@ QString DapServiceController::getResult() void DapServiceController::getWallets() const { - qInfo() << QString("getNodeLogs()"); + qInfo() << QString("getWallets()"); m_pDapCommandController->getWallets(); } @@ -88,44 +92,26 @@ void DapServiceController::getWallets() const /// @param aNodeLogs List of node logs. void DapServiceController::processGetNodeLogs(const QStringList &aNodeLogs) { - if(aNodeLogs.count() <= 0) + if(aNodeLogs.isEmpty()) return; - int counter {0}; - QStringList list; + int xx = DapLogModel::getInstance().rowCount(); - for(int x{0}; x <= aNodeLogs.size(); ++x) + QRegularExpression re("(?<=])\\s"); + for (auto const & log : aNodeLogs) { - if(counter == 4) - { - DapLogMessage message; - message.setTimeStamp(list.at(0)); - message.setType(list.at(1)); - message.setFile(list.at(2)); - message.setMessage(list.at(3)); - DapLogModel::getInstance().append(message); - list.clear(); - counter = 0; - if(x != aNodeLogs.size()) - --x; - } - else if( x != aNodeLogs.size()) - { - list.append(aNodeLogs[x]); - ++counter; - } + const QStringList list = log.split(re); + DapLogMessage logMessage; + logMessage.setTimeStamp(list.at(0)); + logMessage.setType(list.at(1)); + logMessage.setFile(list.at(2)); + logMessage.setMessage(list.at(3)); + DapLogModel::getInstance().append(logMessage); } - emit logCompleted(); -} -void DapServiceController::get() -{ - clearLogModel(); - getNodeLogs(); + emit logCompleted(); } /// Get node logs. -/// @param aiTimeStamp Timestamp start reading logging. -/// @param aiRowCount Number of lines displayed. void DapServiceController::getNodeLogs() const { qInfo() << QString("getNodeLogs()"); diff --git a/CellFrameDashboardGUI/DapServiceController.h b/CellFrameDashboardGUI/DapServiceController.h index 36f5ba190..5dea5db29 100755 --- a/CellFrameDashboardGUI/DapServiceController.h +++ b/CellFrameDashboardGUI/DapServiceController.h @@ -124,8 +124,6 @@ public slots: /// Change status of node /// @param it is true if a node is online void setNodeStatus(const bool aIsOnline); - /// - void get(); /// Get node logs. Q_INVOKABLE void getNodeLogs() const; diff --git a/CellFrameDashboardService/DapChainDashboardService.cpp b/CellFrameDashboardService/DapChainDashboardService.cpp index 836db4b3c..afda918e8 100755 --- a/CellFrameDashboardService/DapChainDashboardService.cpp +++ b/CellFrameDashboardService/DapChainDashboardService.cpp @@ -4,7 +4,6 @@ DapChainDashboardService::DapChainDashboardService() : DapRpcService(nullptr) { // Log reader m_pDapChainLogHandler = new DapChainLogHandler(this); - connect(m_pDapChainLogHandler, SIGNAL(onUpdateModel()), SLOT(clearLogModel())); connect(m_pDapChainLogHandler, SIGNAL(onChangedLog()), SLOT(changedLogModel())); m_pDapChainWalletHandler = new DapChainWalletHandler(this); connect(this, &DapChainDashboardService::onNewClientConnected, [=] { @@ -107,13 +106,6 @@ QString DapChainDashboardService::sendToken(const QString &asWalletName, const Q return m_pDapChainWalletHandler->sendToken(asWalletName, asReceiverAddr, asToken, asAmount); } -void DapChainDashboardService::clearLogModel() -{ - qDebug() << "clearLogModel()"; - QJsonArray arguments; - m_pServer->notifyConnectedClients("RPCClient.clearLogModel", arguments); -} - void DapChainDashboardService::changedLogModel() { qDebug() << "changedLogModel()"; diff --git a/CellFrameDashboardService/DapChainDashboardService.h b/CellFrameDashboardService/DapChainDashboardService.h index 4e4c0f9f0..5d65d5dd4 100755 --- a/CellFrameDashboardService/DapChainDashboardService.h +++ b/CellFrameDashboardService/DapChainDashboardService.h @@ -60,8 +60,6 @@ signals: void onNewClientConnected(); public slots: - void clearLogModel(); - void changedLogModel(); /// Activate the main client window by double-clicking the application icon in the system tray. /// @param reason Type of action on the icon in the system tray. diff --git a/CellFrameDashboardService/DapChainLogHandler.cpp b/CellFrameDashboardService/DapChainLogHandler.cpp index 809610d45..6f5cbaae7 100755 --- a/CellFrameDashboardService/DapChainLogHandler.cpp +++ b/CellFrameDashboardService/DapChainLogHandler.cpp @@ -1,5 +1,7 @@ #include "DapChainLogHandler.h" +#include <QRegularExpression> + DapChainLogHandler::DapChainLogHandler(QObject *parent) : QObject(parent) { m_fileSystemWatcher.addPath(LOG_FILE); @@ -13,31 +15,31 @@ DapChainLogHandler::DapChainLogHandler(QObject *parent) : QObject(parent) QStringList DapChainLogHandler::request() { - /// TODO: The application doesn't work because of it. It needs to be changed -// QStringList m_listLogs; -// QFile file(LOG_FILE); -// if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) -// { -// emit onUpdateModel(); -// } -// else -// { -// QTextStream in(&file); -//// QRegExp rx("(\\[|\\]|\\s)([\\w*]{1,1}[\\w\\s\\W]+)([\\n]|\\])" ); !!! DO NOT DELETE!!! -// QRegExp rx("(\\[|\\]|\\s)([\\w*]{1,1}[\\w\\s\\W]+)(\\]|$)" ); -// rx.setMinimal(true); - - -// while (!in.atEnd()) { -// QString line = in.readLine(); -// int pos{0}; -// while((pos = rx.indexIn(line, pos)) != -1) -// { -// m_listLogs.append(rx.cap(2)); -// pos += rx.matchedLength(); -// } -// } -// } -// return m_listLogs; - return QStringList(); + QFile file(LOG_FILE); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qWarning() << "Failed to open file " << file.fileName(); + return QStringList(); + } + else + { + QTextStream in(&file); + in.seek(m_currentCaretPosition); + const QRegularExpression re("(\\[\\d\\d\\/\\d\\d\\/\\d\\d\\-\\d\\d\\:\\d\\d\\:\\d\\d])\\s(\\[\\w+\\])\\s(\\[\\w+\\])(.+)"); + + QStringList listLogs; + while (!in.atEnd()) { + const QString line = in.readLine(); + const auto match = re.match(line); + if(!match.hasMatch()) + continue; + + const QString matchedLog = match.captured(); + listLogs.append(matchedLog); + m_currentCaretPosition += matchedLog.length(); + } + + return listLogs; + + } } diff --git a/CellFrameDashboardService/DapChainLogHandler.h b/CellFrameDashboardService/DapChainLogHandler.h index 45453ca25..8f0fb7573 100644 --- a/CellFrameDashboardService/DapChainLogHandler.h +++ b/CellFrameDashboardService/DapChainLogHandler.h @@ -17,7 +17,8 @@ class DapChainLogHandler : public QObject /// Log file change watcher. QFileSystemWatcher m_fileSystemWatcher; - + /// Current caret position in log file + qint64 m_currentCaretPosition{0}; public: explicit DapChainLogHandler(QObject *parent = nullptr); -- GitLab