diff --git a/CellFrameDashboardGUI/DapServiceController.cpp b/CellFrameDashboardGUI/DapServiceController.cpp index a381b25db16fe76d7d9a274b3c7c51a45b9952f4..6075f77838b9961a2fb24a35e8dd69bdf16b768a 100644 --- a/CellFrameDashboardGUI/DapServiceController.cpp +++ b/CellFrameDashboardGUI/DapServiceController.cpp @@ -47,17 +47,17 @@ void DapServiceController::requestToService(const QString &asServicename, const const QVariant &arg8, const QVariant &arg9, const QVariant &arg10) { DapAbstractCommand * transceiver = m_transceivers.find(asServicename).value().first; - + Q_ASSERT(transceiver); - - transceiver->requestToService(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); connect(transceiver, SIGNAL(serviceResponded(QVariant)), SLOT(findEmittedSignal(QVariant))); + transceiver->requestToService(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } /// Register command. void DapServiceController::registerCommand() { m_transceivers.insert("ADD", qMakePair(new DapAddWalletCommand("ADD", m_DAPRpcSocket, this), QString("addWalletResponded"))); + m_transceivers.insert("GET_LOG", qMakePair(new DapUpdateLogsCommand("GET_LOG", m_DAPRpcSocket, this),QString("historyLogResponded"))); } /// Find the emitted signal. diff --git a/CellFrameDashboardGUI/DapServiceController.h b/CellFrameDashboardGUI/DapServiceController.h index ee81bc39fe1ca7de011da9375617d7555ff066fa..d141636ae9fca607a051cb59d597cac14a266913 100644 --- a/CellFrameDashboardGUI/DapServiceController.h +++ b/CellFrameDashboardGUI/DapServiceController.h @@ -12,6 +12,7 @@ #include "DapServiceClient.h" #include "Handlers/DapAbstractCommand.h" #include "Handlers/DapAddWalletCommand.h" +#include "Handlers/DapUpdateLogsCommand.h" class DapServiceController : public QObject { @@ -75,6 +76,9 @@ signals: void versionChanged(const QString &version); void addWalletResponded(const QVariant& wallet); + ///A signal that is used to transmit data to the log model. + /// @param historyString QStringList + void historyLogResponded(const QVariant& historyString); private slots: /// Register command. diff --git a/CellFrameDashboardGUI/screen/desktop/Logs/DapLogsScreen.qml b/CellFrameDashboardGUI/screen/desktop/Logs/DapLogsScreen.qml index 8cded9efc57fbe0895dbd01a575f8d6245108d7f..3c1151cdcac3668a06029e81571bb3b8e2fff74b 100644 --- a/CellFrameDashboardGUI/screen/desktop/Logs/DapLogsScreen.qml +++ b/CellFrameDashboardGUI/screen/desktop/Logs/DapLogsScreen.qml @@ -2,6 +2,7 @@ import QtQuick.Window 2.2 import QtQuick.Controls 2.1 import QtQuick 2.0 import QtGraphicalEffects 1.0 +import QtQml 2.0 DapLogsScreenForm { @@ -35,9 +36,25 @@ DapLogsScreenForm property var todayYear property var stringTime } + //Slot for updating data in the model. The signal comes from C++. + Connections + { + target: dapServiceController + onHistoryLogResponded:fillModel(historyString); + + } + //Timer for updating data in the model + Timer + { + id:loadContentLogTimer + interval: 60000 + repeat: true + onTriggered: dapServiceController.requestToService("GET_LOG",200); + } //Creates a list model for the example - Component.onCompleted: { + Component.onCompleted: + { dapLogsListViewIndex = -1; privateDate.today = new Date(); privateDate.todayDay = privateDate.today.getDate(); @@ -45,14 +62,10 @@ DapLogsScreenForm privateDate.todayYear = privateDate.today.getFullYear(); var timeString = new Date(); var day = new Date(86400); - var count = 1000 - for (var i = 0; i < count; i++) - { - var momentTime = timeString/1000 - (day/6) * i; - var momentDay = getDay(momentTime); - dapLogsModel.append({"type":"DBG"+i, "info":"Add problems"+i, "file":"dup_chein"+i, "time":getTime(momentTime), - "date":getDay(momentTime)}); - } + + dapServiceController.requestToService("GET_LOG",200); + loadContentLogTimer.start(); + } ListModel @@ -126,6 +139,7 @@ DapLogsScreenForm anchors.bottom: parent.bottom width: 43 * pt color: parent.color + clip: true Text { id: typeLog @@ -148,6 +162,7 @@ DapLogsScreenForm anchors.right: frameFileLog.left anchors.rightMargin: thirdMarginList color: parent.color + clip: true Text { id: textLog @@ -169,6 +184,7 @@ DapLogsScreenForm anchors.rightMargin: thirdMarginList width: 326 * pt color: parent.color + clip: true Text { id: fileLog @@ -189,6 +205,7 @@ DapLogsScreenForm anchors.right: parent.right width: 62 * pt color: parent.color + clip: true Text { id: timeLog @@ -232,19 +249,41 @@ DapLogsScreenForm } } + //Splits a string from the log. + function parceStringFromLog(string) + { + var split = string.split(/ \[|\] \[|\]|\[/); + return split; + } + + //Fills in the model. + function fillModel(stringList) + { + dapLogsModel.clear(); + var count = Object.keys(stringList).length + for (var ind = count-1; ind >= 0; ind--) + { + var arrLogString = parceStringFromLog(stringList[ind]); + var stringTime = parceTime(arrLogString[1]); + + dapLogsModel.append({"type":arrLogString[2], "info":arrLogString[4], "file":arrLogString[3], "time":getTime(stringTime), + "date":getDay(stringTime)}); + } + } + //This function converts the string representation of time to the Date format function parceTime(thisTime) { var aDate = thisTime.split('-'); var aDay = aDate[0].split('/'); var aTime = aDate[1].split(':'); - privateDate.stringTime = new Date(20+aDay[2], aDay[0] - 1, aDay[1], aTime[0], aTime[1], aTime[2]); + return new Date(20+aDay[2], aDay[0] - 1, aDay[1], aTime[0], aTime[1], aTime[2]); } //Returns the time in the correct form for the delegate function getTime(thisTime) { - var tmpTime = new Date(thisTime * 1000) + var tmpTime = new Date(thisTime) var thisHour = tmpTime.getHours(); var thisMinute = tmpTime.getMinutes(); var thisSecond = tmpTime.getSeconds(); @@ -258,7 +297,7 @@ DapLogsScreenForm { var monthArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - var tmpDate = new Date(thisTime*1000); + var tmpDate = new Date(thisTime); var thisMonth = tmpDate.getMonth(); var thisDay = tmpDate.getDate(); var thisYear = tmpDate.getFullYear(); diff --git a/CellFrameDashboardService/DapServiceController.cpp b/CellFrameDashboardService/DapServiceController.cpp index 5c2702c2871666d2b312ce082820266f8ff76e3b..bc9e3ba5bdaebe96da40de46ad532dde78480d72 100755 --- a/CellFrameDashboardService/DapServiceController.cpp +++ b/CellFrameDashboardService/DapServiceController.cpp @@ -36,4 +36,5 @@ bool DapServiceController::start() void DapServiceController::registerCommand() { m_pServer->addService(new DapAddWalletCommand("ADD", nullptr, this)); + m_pServer->addService(new DapUpdateLogsCommand("GET_LOG", nullptr, this)); } diff --git a/CellFrameDashboardService/DapServiceController.h b/CellFrameDashboardService/DapServiceController.h index 18be98a0c331a7735cdfafb5922ca04edd231324..5bf180c16e2c0017c1aa68a7bdac8429e34e7bd4 100755 --- a/CellFrameDashboardService/DapServiceController.h +++ b/CellFrameDashboardService/DapServiceController.h @@ -23,6 +23,7 @@ typedef class DapRpcLocalServer DapUiService; #include "Handlers/DapAbstractCommand.h" #include "Handlers/DapAddWalletCommand.h" +#include "Handlers/DapUpdateLogsCommand.h" /** * @brief The DapServiceController class diff --git a/libCellFrameDashboardCommon/Handlers/DapAddWalletCommand.cpp b/libCellFrameDashboardCommon/Handlers/DapAddWalletCommand.cpp index 2f7e93edd2a45bd7eee451430c5ac2bf3e30caea..4ca6c832cb3ed700e0b2585303bcdf3c574958a3 100644 --- a/libCellFrameDashboardCommon/Handlers/DapAddWalletCommand.cpp +++ b/libCellFrameDashboardCommon/Handlers/DapAddWalletCommand.cpp @@ -28,7 +28,7 @@ QVariant DapAddWalletCommand::respondToService(const QVariant &arg1, const QVari Q_UNUSED(arg8) Q_UNUSED(arg9) Q_UNUSED(arg10) - + return QVariant(); } diff --git a/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.cpp b/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.cpp new file mode 100644 index 0000000000000000000000000000000000000000..771ca9826643ed2e9e188e458afc5546e6f2e575 --- /dev/null +++ b/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.cpp @@ -0,0 +1,107 @@ +#include "DapUpdateLogsCommand.h" + +/// Overloaded constructor. +/// @param asServiceName Service name. +/// @param apSocket Client connection socket with service. +/// @param parent Parent. +DapUpdateLogsCommand::DapUpdateLogsCommand(const QString &asServiceName, DapRpcSocket *apSocket, QObject *parent) + : DapAbstractCommand(asServiceName, apSocket, parent),m_seekFile(0),m_bufferSize(DEFAULT_BUFFER_SIZE),m_bufLog(),m_watcherDapLogFile(nullptr) +{ + if(apSocket == nullptr) + { + m_watcherDapLogFile = new QFileSystemWatcher(parent); + + + if (! m_watcherDapLogFile->addPath(LOG_FILE)) + { + qCritical("File not found"); + } + + connect(m_watcherDapLogFile, &QFileSystemWatcher::fileChanged, this,&DapUpdateLogsCommand::dapGetLog); + } +} +/// Send a response to the service. +/// @param arg1...arg10 Parameters. +/// @return Reply to service. +QVariant DapUpdateLogsCommand::respondToService(const QVariant &arg1, const QVariant &arg2, const QVariant &arg3, + const QVariant &arg4, const QVariant &arg5, const QVariant &arg6, + const QVariant &arg7, const QVariant &arg8, const QVariant &arg9, + const QVariant &arg10) +{ + Q_UNUSED(arg1) + Q_UNUSED(arg2) + Q_UNUSED(arg3) + Q_UNUSED(arg4) + Q_UNUSED(arg5) + Q_UNUSED(arg6) + Q_UNUSED(arg7) + Q_UNUSED(arg8) + Q_UNUSED(arg9) + Q_UNUSED(arg10) + + return QVariant(); +} + +/// The log file is being read. +/// @param arg1...arg10 Parameters. +/// @return Reply to client. +QVariant DapUpdateLogsCommand::respondToClient(const QVariant &arg1, const QVariant &arg2, const QVariant &arg3, + const QVariant &arg4, const QVariant &arg5, const QVariant &arg6, + const QVariant &arg7, const QVariant &arg8, const QVariant &arg9, + const QVariant &arg10) +{ + Q_UNUSED(arg2) + Q_UNUSED(arg3) + Q_UNUSED(arg4) + Q_UNUSED(arg5) + Q_UNUSED(arg6) + Q_UNUSED(arg7) + Q_UNUSED(arg8) + Q_UNUSED(arg9) + Q_UNUSED(arg10) + + if(m_bufferSize != arg1.toInt()||m_bufLog.isEmpty()) + { + m_bufferSize = arg1.toInt(); + m_seekFile = 0; + dapGetLog(); + } + return m_bufLog; +} + +///The slot reads logs to the buffer. +void DapUpdateLogsCommand::dapGetLog() +{ + QFile dapLogFile(LOG_FILE); + if (!dapLogFile.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qCritical("The node log file does not open."); + return; + } + + QTextStream readFile(&dapLogFile); + QString line; + + readFile.seek(m_seekFile); + + while(!readFile.atEnd()) + { + m_bufLog.append(readFile.readLine()); + + if(m_bufLog.size() > m_bufferSize) + { + m_bufLog.removeFirst(); + } + } + m_seekFile = readFile.pos(); + + dapLogFile.close(); +} + +/// Reply from service. +/// @return Service reply. +void DapUpdateLogsCommand::replyFromService() +{ + DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender()); + emit serviceResponded(reply->response().toJsonValue()); +} diff --git a/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.h b/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.h new file mode 100644 index 0000000000000000000000000000000000000000..e923752161770d4f0069fdd810e60ef248a84b40 --- /dev/null +++ b/libCellFrameDashboardCommon/Handlers/DapUpdateLogsCommand.h @@ -0,0 +1,56 @@ +#ifndef DAPUPDATELOGSCOMMAND_H +#define DAPUPDATELOGSCOMMAND_H + +#include <QFile> +#include <QFileSystemWatcher> + +#include "DapAbstractCommand.h" + + +#define LOG_FILE "/opt/cellframe-node/var/log/cellframe-node.log" +#define DEFAULT_BUFFER_SIZE 200 + +class DapUpdateLogsCommand : public DapAbstractCommand +{ + ///The cursor position from which to start reading the file. + qint64 m_seekFile {0}; + ///The number of rows that are stored in memory. + int m_bufferSize {0}; + ///The container with the lines from the log. + QStringList m_bufLog; + ///Monitors changes in the log file. + QFileSystemWatcher *m_watcherDapLogFile {nullptr}; + +public: + /// Overloaded constructor. + /// @param asServiceName Service name. + /// @param apSocket Client connection socket with service. + /// @param parent Parent. + explicit DapUpdateLogsCommand(const QString &asServicename, DapRpcSocket *apSocket = nullptr, QObject *parent = nullptr); + +protected slots: + ///The slot reads logs to the buffer. + void dapGetLog(); +public slots: + /// Send a response to the service. + /// @param arg1...arg10 Parameters. + /// @return Reply to service. + QVariant respondToService(const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant(), + const QVariant &arg3 = QVariant(), const QVariant &arg4 = QVariant(), + const QVariant &arg5 = QVariant(), const QVariant &arg6 = QVariant(), + const QVariant &arg7 = QVariant(), const QVariant &arg8 = QVariant(), + const QVariant &arg9 = QVariant(), const QVariant &arg10 = QVariant())override; + /// Send a response to the client. + /// @param arg1...arg10 Parameters. + /// @return Reply to client. + QVariant respondToClient(const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant(), + const QVariant &arg3 = QVariant(), const QVariant &arg4 = QVariant(), + const QVariant &arg5 = QVariant(), const QVariant &arg6 = QVariant(), + const QVariant &arg7 = QVariant(), const QVariant &arg8 = QVariant(), + const QVariant &arg9 = QVariant(), const QVariant &arg10 = QVariant()) override; + /// Reply from service. + /// @return Service reply. + virtual void replyFromService()override; +}; + +#endif // DAPUPDATELOGSCOMMAND_H diff --git a/libCellFrameDashboardCommon/libCellFrameDashboardCommon.pri b/libCellFrameDashboardCommon/libCellFrameDashboardCommon.pri index 2a3b8fbf4630e39bfb011c8cfcecff90b0e7cb90..154d26ec624a0741e2651cbb5284ce0c4c66da7a 100755 --- a/libCellFrameDashboardCommon/libCellFrameDashboardCommon.pri +++ b/libCellFrameDashboardCommon/libCellFrameDashboardCommon.pri @@ -20,7 +20,8 @@ SOURCES +=\ $$PWD/DapLogMessage.cpp \ $$PWD/DapChainWallet.cpp \ $$PWD/Handlers/DapAbstractCommand.cpp \ - $$PWD/Handlers/DapAddWalletCommand.cpp + $$PWD/Handlers/DapAddWalletCommand.cpp \ + $$PWD/Handlers/DapUpdateLogsCommand.cpp HEADERS +=\ $$PWD/DapChainConvertor.h \ @@ -31,4 +32,5 @@ HEADERS +=\ $$PWD/DapChainWallet.h \ $$PWD/DapNodeType.h \ $$PWD/Handlers/DapAbstractCommand.h \ - $$PWD/Handlers/DapAddWalletCommand.h + $$PWD/Handlers/DapAddWalletCommand.h \ + $$PWD/Handlers/DapUpdateLogsCommand.h