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 <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <Qt>
|
||||
#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) {
|
||||
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)) {
|
||||
qCritical().noquote() << "Could not save response! " + responseFile.errorString();
|
||||
return;
|
||||
}
|
||||
responseFile.write(( //
|
||||
ioutils::methodString(context.reply->operation()) + // write method
|
||||
" " + // space
|
||||
context.reply->url().toString() + // write url
|
||||
" " + // space
|
||||
QString::number(context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()) //
|
||||
+ // write status
|
||||
"\n" // newline
|
||||
)
|
||||
.toUtf8());
|
||||
|
||||
if (!requestFile.open(QIODevice::Append)) {
|
||||
qCritical().noquote() << "Could not append request! " + responseFile.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto header : context.reply->rawHeaderList()) responseFile.write(header + "\n");
|
||||
responseFile.write("\n\n" + context.response);
|
||||
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 <QNetworkReply>
|
||||
#include <settings.hpp>
|
||||
|
||||
struct RequestContext {
|
||||
QByteArray response;
|
||||
@ -12,7 +13,35 @@ struct RequestContext {
|
||||
|
||||
namespace requestlogging {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user