Revamp logging system
Plan: * Have a dialog that shows history * Once you double click an item it opens the response in a new window
This commit is contained in:
parent
206387578c
commit
4ecade7b2e
@ -1,33 +1,64 @@
|
|||||||
#include "requestlogging.hpp"
|
#include "requestlogging.hpp"
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <Qt>
|
||||||
#include <io/ioutils.hpp>
|
#include <io/ioutils.hpp>
|
||||||
#include <settings.hpp>
|
|
||||||
|
|
||||||
QDir responses(settings::dir().absoluteFilePath("response"));
|
// $type $url $status $time
|
||||||
|
// $type = GET POST PATCH DELETE etc
|
||||||
|
// $url = request target
|
||||||
|
// $status = response code
|
||||||
|
// $time = time of request, file name for response: $SETTINGS_DIR/responses/$time
|
||||||
|
|
||||||
|
QDir responses(settings::dir().absoluteFilePath("responses"));
|
||||||
|
QString requestPath = settings::dir().absoluteFilePath("history");
|
||||||
|
|
||||||
void requestlogging::addEntry(RequestContext context) {
|
void requestlogging::addEntry(RequestContext context) {
|
||||||
if (!responses.exists()) responses.mkpath(".");
|
if (!responses.exists()) responses.mkpath(".");
|
||||||
QFile responseFile(responses.absoluteFilePath(context.sender + "-" + QDateTime().toString("yyyy-MM-dd HH-mm-ss")));
|
QString timeNow = QDateTime::currentDateTime().toUTC().toString("yyyy-MM-dd HH-mm-ss-zzz");
|
||||||
|
QFile responseFile(responses.absoluteFilePath(timeNow));
|
||||||
|
QFile requestFile(requestPath);
|
||||||
|
|
||||||
if (!responseFile.open(QIODevice::WriteOnly)) {
|
if (!responseFile.open(QIODevice::WriteOnly)) {
|
||||||
qCritical().noquote() << "Could not save response! " + responseFile.errorString();
|
qCritical().noquote() << "Could not save response! " + responseFile.errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
responseFile.write(( //
|
|
||||||
ioutils::methodString(context.reply->operation()) + // write method
|
if (!requestFile.open(QIODevice::Append)) {
|
||||||
" " + // space
|
qCritical().noquote() << "Could not append request! " + responseFile.errorString();
|
||||||
context.reply->url().toString() + // write url
|
return;
|
||||||
" " + // space
|
}
|
||||||
QString::number(context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()) //
|
|
||||||
+ // write status
|
|
||||||
"\n" // newline
|
|
||||||
)
|
|
||||||
.toUtf8());
|
|
||||||
for (auto header : context.reply->rawHeaderList()) responseFile.write(header + "\n");
|
for (auto header : context.reply->rawHeaderList()) responseFile.write(header + "\n");
|
||||||
responseFile.write("\n\n" + context.response);
|
responseFile.write("\n\n" + context.response);
|
||||||
responseFile.close();
|
responseFile.close();
|
||||||
|
|
||||||
addGUIEntry(context, QFileInfo(responseFile).absoluteFilePath());
|
QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type
|
||||||
|
<< context.reply->url().toString() << " " // $url
|
||||||
|
<< context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() // $status
|
||||||
|
<< timeNow; // $time
|
||||||
|
requestFile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void requestlogging::addGUIEntry(RequestContext context, QString file) {
|
using requestlogging::LoggedRequest;
|
||||||
|
|
||||||
|
QList<LoggedRequest> requestlogging::getRequests() {
|
||||||
|
QList<LoggedRequest> ret;
|
||||||
|
|
||||||
|
QFile requestFile(requestPath);
|
||||||
|
|
||||||
|
QByteArray line;
|
||||||
|
while ((line = requestFile.readLine()).size() != 0) {
|
||||||
|
LoggedRequest r;
|
||||||
|
QTextStream stream(&line);
|
||||||
|
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");
|
||||||
|
ret.append(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
|
#include <settings.hpp>
|
||||||
|
|
||||||
struct RequestContext {
|
struct RequestContext {
|
||||||
QByteArray response;
|
QByteArray response;
|
||||||
@ -12,7 +13,35 @@ struct RequestContext {
|
|||||||
|
|
||||||
namespace requestlogging {
|
namespace requestlogging {
|
||||||
void addEntry(RequestContext context);
|
void addEntry(RequestContext context);
|
||||||
void addGUIEntry(RequestContext context, QString file);
|
|
||||||
|
class LoggedRequest {
|
||||||
|
friend QList<LoggedRequest> getRequests();
|
||||||
|
|
||||||
|
public:
|
||||||
|
QString getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
QString getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
QDateTime 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString url;
|
||||||
|
QString type;
|
||||||
|
QDateTime time;
|
||||||
|
int responseCode;
|
||||||
|
};
|
||||||
|
|
||||||
|
QList<LoggedRequest> getRequests();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // REQUESTLOGGING_HPP
|
#endif // REQUESTLOGGING_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user