From db24653f7dd5578fcfd245f3b3f274715c8b5b8a Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Mon, 21 Aug 2017 16:17:20 +0200 Subject: [PATCH] Request logging --- KShare.pro | 12 ++++-- io/ioutils.cpp | 25 ++++++++++-- logs/historydialog.cpp | 37 +++++++++++++++++ logs/historydialog.h | 24 +++++++++++ logs/historydialog.ui | 88 +++++++++++++++++++++++++++++++++++++++++ logs/requestlogging.cpp | 18 +++++---- logs/requestlogging.hpp | 17 ++++---- mainwindow.cpp | 6 +++ mainwindow.hpp | 1 + mainwindow.ui | 31 +++++++++------ monospacetextdialog.cpp | 13 ++++++ monospacetextdialog.h | 21 ++++++++++ monospacetextdialog.ui | 73 ++++++++++++++++++++++++++++++++++ 13 files changed, 331 insertions(+), 35 deletions(-) create mode 100644 logs/historydialog.cpp create mode 100644 logs/historydialog.h create mode 100644 logs/historydialog.ui create mode 100644 monospacetextdialog.cpp create mode 100644 monospacetextdialog.h create mode 100644 monospacetextdialog.ui diff --git a/KShare.pro b/KShare.pro index 1b95264..78b1496 100644 --- a/KShare.pro +++ b/KShare.pro @@ -64,7 +64,9 @@ SOURCES += main.cpp\ uploaders/default/imgursettingsdialog.cpp \ uploaders/default/imgplusuploader.cpp \ filenamevalidator.cpp \ - logs/requestlogging.cpp + logs/requestlogging.cpp \ + logs/historydialog.cpp \ + monospacetextdialog.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -109,7 +111,9 @@ HEADERS += mainwindow.hpp \ uploaders/default/imgursettingsdialog.hpp \ uploaders/default/imgplusuploader.hpp \ filenamevalidator.hpp \ - logs/requestlogging.hpp + logs/requestlogging.hpp \ + logs/historydialog.h \ + monospacetextdialog.h nopkg { # win32 { @@ -161,7 +165,9 @@ FORMS += mainwindow.ui \ settingsdialog.ui \ aboutbox.ui \ hotkeyinputdialog.ui \ - uploaders/default/imgursettingsdialog.ui + uploaders/default/imgursettingsdialog.ui \ + logs/historydialog.ui \ + monospacetextdialog.ui DISTFILES += \ README.md \ diff --git a/io/ioutils.cpp b/io/ioutils.cpp index 5753178..0f54d0e 100644 --- a/io/ioutils.cpp +++ b/io/ioutils.cpp @@ -3,9 +3,19 @@ #include #include #include +#include QNetworkAccessManager ioutils::networkManager; +void addLogEntry(QNetworkReply *reply, QByteArray data) { + requestlogging::RequestContext ctx; + + ctx.reply = reply; + ctx.response = data; + + requestlogging::addEntry(ctx); +} + void ioutils::postMultipart(QUrl target, QList> headers, QHttpMultiPart *body, @@ -17,6 +27,7 @@ void ioutils::postMultipart(QUrl target, QNetworkReply *reply = networkManager.post(req, body); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QByteArray data = reply->readAll(); + addLogEntry(reply, data); callback(QJsonDocument::fromJson(data), data, reply); delete reply; }); @@ -32,7 +43,9 @@ void ioutils::postMultipartData(QUrl target, } QNetworkReply *reply = networkManager.post(req, body); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { - callback(reply->readAll(), reply); + QByteArray data = reply->readAll(); + addLogEntry(reply, data); + callback(data, reply); delete reply; }); } @@ -47,6 +60,7 @@ void ioutils::getJson(QUrl target, QNetworkReply *reply = networkManager.get(req); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QByteArray data = reply->readAll(); + addLogEntry(reply, data); callback(QJsonDocument::fromJson(data), data, reply); reply->deleteLater(); }); @@ -63,6 +77,7 @@ void ioutils::postJson(QUrl target, QNetworkReply *reply = networkManager.post(req, body); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QByteArray data = reply->readAll(); + addLogEntry(reply, data); callback(QJsonDocument::fromJson(data), data, reply); delete reply; }); @@ -75,7 +90,9 @@ void ioutils::getData(QUrl target, QList> headers, std:: } QNetworkReply *reply = networkManager.get(req); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { - callback(reply->readAll(), reply); + QByteArray data = reply->readAll(); + addLogEntry(reply, data); + callback(data, reply); delete reply; }); } @@ -90,7 +107,9 @@ void ioutils::postData(QUrl target, } QNetworkReply *reply = networkManager.post(req, body); QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { - callback(reply->readAll(), reply); + QByteArray data = reply->readAll(); + addLogEntry(reply, data); + callback(data, reply); delete reply; }); } diff --git a/logs/historydialog.cpp b/logs/historydialog.cpp new file mode 100644 index 0000000..9cd2f47 --- /dev/null +++ b/logs/historydialog.cpp @@ -0,0 +1,37 @@ +#include "historydialog.h" +#include "requestlogging.hpp" +#include "ui_historydialog.h" + +#include + +using requestlogging::LoggedRequest; + +HistoryDialog::HistoryDialog(QWidget *parent) : QDialog(parent), ui(new Ui::HistoryDialog) { + ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + ui->treeWidget->setColumnWidth(0, 50); + ui->treeWidget->setColumnWidth(1, 150); + ui->treeWidget->setColumnWidth(2, 50); + ui->treeWidget->setColumnWidth(3, 100); + + QList requests = requestlogging::getRequests(); + for (LoggedRequest req : requests) { + ui->treeWidget->addTopLevelItem(new QTreeWidgetItem( + { req.getType(), req.getUrl(), QString::number(req.getResponseCode()), req.getTime() + " UTC" })); + } +} + +HistoryDialog::~HistoryDialog() { + delete ui; +} + +void HistoryDialog::on_treeWidget_doubleClicked(const QModelIndex &) { + QString file = ui->treeWidget->currentItem()->text(3); + file = settings::dir().absoluteFilePath("responses/" + file.left(file.length() - 4)); + + QFile dataFile(file); + if (!dataFile.open(QIODevice::ReadOnly)) return; + MonospaceTextDialog *dialog = new MonospaceTextDialog(file, dataFile.readAll()); + dialog->setAttribute(Qt::WA_DeleteOnClose); + dialog->show(); +} diff --git a/logs/historydialog.h b/logs/historydialog.h new file mode 100644 index 0000000..a3d3d36 --- /dev/null +++ b/logs/historydialog.h @@ -0,0 +1,24 @@ +#ifndef HISTORYDIALOG_H +#define HISTORYDIALOG_H + +#include + +namespace Ui { +class HistoryDialog; +} + +class HistoryDialog : public QDialog { + Q_OBJECT + +public: + explicit HistoryDialog(QWidget *parent = 0); + ~HistoryDialog(); + +private slots: + void on_treeWidget_doubleClicked(const QModelIndex &); + +private: + Ui::HistoryDialog *ui; +}; + +#endif // HISTORYDIALOG_H diff --git a/logs/historydialog.ui b/logs/historydialog.ui new file mode 100644 index 0000000..556f425 --- /dev/null +++ b/logs/historydialog.ui @@ -0,0 +1,88 @@ + + + HistoryDialog + + + + 0 + 0 + 400 + 300 + + + + Request History + + + + + + + Type + + + + + URL + + + + + Status + + + + + Time + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + HistoryDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + HistoryDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/logs/requestlogging.cpp b/logs/requestlogging.cpp index 3ff5edd..593436f 100644 --- a/logs/requestlogging.cpp +++ b/logs/requestlogging.cpp @@ -13,6 +13,7 @@ QDir responses(settings::dir().absoluteFilePath("responses")); QString requestPath = settings::dir().absoluteFilePath("history"); + void requestlogging::addEntry(RequestContext context) { if (!responses.exists()) responses.mkpath("."); QString timeNow = QDateTime::currentDateTime().toUTC().toString("yyyy-MM-dd HH-mm-ss-zzz"); @@ -29,14 +30,15 @@ void requestlogging::addEntry(RequestContext context) { return; } - for (auto header : context.reply->rawHeaderList()) responseFile.write(header + "\n"); + for (auto header : context.reply->rawHeaderList()) + responseFile.write(header + ": " + context.reply->rawHeader(header) + "\n"); responseFile.write("\n\n" + context.response); responseFile.close(); - QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type - << context.reply->url().toString() << " " // $url - << context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() // $status - << timeNow; // $time + QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type + << context.reply->url().toString() << " " // $url + << context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << " " // $status + << timeNow.replace(" ", "_"); // $time requestFile.close(); } @@ -46,6 +48,7 @@ QList requestlogging::getRequests() { QList ret; QFile requestFile(requestPath); + if (!requestFile.exists() || !requestFile.open(QIODevice::ReadOnly)) return ret; QByteArray line; while ((line = requestFile.readLine()).size() != 0) { @@ -54,9 +57,8 @@ QList requestlogging::getRequests() { stream >> r.type; stream >> r.url; stream >> r.responseCode; - QString time; - stream >> time; - r.time = QDateTime::fromString(time, "yyyy-MM-dd HH-mm-ss-zzz"); + stream >> r.time; + r.time = r.time.replace("_", " "); ret.append(r); } diff --git a/logs/requestlogging.hpp b/logs/requestlogging.hpp index ae2483c..87202ef 100644 --- a/logs/requestlogging.hpp +++ b/logs/requestlogging.hpp @@ -5,14 +5,12 @@ #include #include -struct RequestContext { - QByteArray response; - QString sender; - QNetworkReply *reply; -}; namespace requestlogging { -void addEntry(RequestContext context); +struct RequestContext { + QByteArray response; + QNetworkReply *reply; +}; class LoggedRequest { friend QList getRequests(); @@ -24,24 +22,25 @@ public: QString getType() { return type; } - QDateTime getTime() { + QString getTime() { return time; } int getResponseCode() { return responseCode; } QByteArray getResponse() { - return QFile(settings::dir().absoluteFilePath("responses/" + time.toString("yyyy-MM-dd HH-mm-ss-zzz"))).readAll(); + return QFile(settings::dir().absoluteFilePath("responses/" + time)).readAll(); } private: QString url; QString type; - QDateTime time; + QString time; int responseCode; }; QList getRequests(); +void addEntry(RequestContext context); } #endif // REQUESTLOGGING_HPP diff --git a/mainwindow.cpp b/mainwindow.cpp index 22375ba..afd8370 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -181,3 +182,8 @@ void MainWindow::on_actionActive_window_triggered() { void MainWindow::on_actionAbort_triggered() { controller->abort(); } + +void MainWindow::on_history_clicked() { + HistoryDialog *dialog = new HistoryDialog; + dialog->show(); +} diff --git a/mainwindow.hpp b/mainwindow.hpp index 5bb6e75..9cf807e 100644 --- a/mainwindow.hpp +++ b/mainwindow.hpp @@ -30,6 +30,7 @@ private slots: void on_actionAbout_triggered(); void on_actionActive_window_triggered(); void on_actionAbort_triggered(); + void on_history_clicked(); public: static MainWindow *inst(); diff --git a/mainwindow.ui b/mainwindow.ui index 3328122..63f1a40 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 352 - 220 + 340 + 239 @@ -25,13 +25,6 @@ - - - - Settings - - - @@ -44,15 +37,30 @@ + + + + Settings + + + + + + + Open request history + + + + 0 0 - 352 - 25 + 340 + 22 @@ -91,7 +99,6 @@ - &Quit diff --git a/monospacetextdialog.cpp b/monospacetextdialog.cpp new file mode 100644 index 0000000..cef111a --- /dev/null +++ b/monospacetextdialog.cpp @@ -0,0 +1,13 @@ +#include "monospacetextdialog.h" +#include "ui_monospacetextdialog.h" + +MonospaceTextDialog::MonospaceTextDialog(QString name, QByteArray data, QWidget *parent) +: QDialog(parent), ui(new Ui::MonospaceTextDialog) { + ui->setupUi(this); + setWindowTitle(name); + ui->textEdit->setText(data); +} + +MonospaceTextDialog::~MonospaceTextDialog() { + delete ui; +} diff --git a/monospacetextdialog.h b/monospacetextdialog.h new file mode 100644 index 0000000..d2b6463 --- /dev/null +++ b/monospacetextdialog.h @@ -0,0 +1,21 @@ +#ifndef MONOSPACETEXTDIALOG_H +#define MONOSPACETEXTDIALOG_H + +#include + +namespace Ui { +class MonospaceTextDialog; +} + +class MonospaceTextDialog : public QDialog { + Q_OBJECT + +public: + explicit MonospaceTextDialog(QString name, QByteArray data, QWidget *parent = 0); + ~MonospaceTextDialog(); + +private: + Ui::MonospaceTextDialog *ui; +}; + +#endif // MONOSPACETEXTDIALOG_H diff --git a/monospacetextdialog.ui b/monospacetextdialog.ui new file mode 100644 index 0000000..8c72446 --- /dev/null +++ b/monospacetextdialog.ui @@ -0,0 +1,73 @@ + + + MonospaceTextDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + Monospace + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + MonospaceTextDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + MonospaceTextDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +