User operation log, v1

This commit is contained in:
Krad
2021-11-19 15:36:12 +08:00
parent bca10e3e60
commit 99754ab84f
9 changed files with 144 additions and 44 deletions

View File

@@ -0,0 +1,47 @@
//
// Created by Krad on 2021/11/19.
//
#include "UserOperationLog.h"
#include <QDate>
#include <QDir>
#include "appvals/AppGlobalValues.h"
#include "models/User.h"
void UserOperationLog::init() {
QDir log_dir("./");
if (!log_dir.exists("./log")) log_dir.mkdir("log");
currentFileName = "./log/" + QDate::currentDate().toString("yyyy-MM-dd")+QString("-op.log");
logFile.setFileName(currentFileName);
if (logFile.exists())
{
logFile.open(QFile::OpenModeFlag::Append | QFile::OpenModeFlag::Text);
} else{
logFile.open(QFile::OpenModeFlag::NewOnly | QFile::OpenModeFlag::Text);
}
out.setDevice(&logFile);
}
QString getOperationName(UserOperation operation)
{
switch (operation) {
#define ADD_OPERATION(name)\
case name: return #name;
USER_OPERATIONS()
#undef ADD_OPERATION
default:
return "unknow error";
}
}
void UserOperationLog::log(UserOperation operation, bool processing) {
QDateTime now = QDateTime::currentDateTime();
AppGlobalValues::setLastOperationTime(now);
AppGlobalValues::setLastOperation(operation);
QString operationName = getOperationName(operation);
AppGlobalValues::setInProcessing(processing);
QString UserName = User::Current()->getUserCode().isEmpty()?"anonymous":User::Current()->getUserCode();
out << now.toString(Qt::DateFormat::ISODateWithMs)<<"\t"<<UserName<<"\t"<<operationName<<endl;
}

View File

@@ -0,0 +1,62 @@
//
// Created by Krad on 2021/11/19.
//
#ifndef GUI_USEROPERATIONLOG_H
#define GUI_USEROPERATIONLOG_H
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#define USER_OPERATIONS()\
ADD_OPERATION(Login)\
ADD_OPERATION(Logout)\
ADD_OPERATION(ChangePassword)\
ADD_OPERATION(ChangeUserName)\
ADD_OPERATION(AddPatient)\
ADD_OPERATION(ChangePatientInfo)\
ADD_OPERATION(DeletePatient)\
ADD_OPERATION(SelectPatient)\
ADD_OPERATION(StartRefresh)\
ADD_OPERATION(Stop)\
ADD_OPERATION(StartPreview)\
ADD_OPERATION(StartScan)\
ADD_OPERATION(ConfirmError)\
#define LOG_USER_OPERATION(...)\
UserOperationLog::Default()->log(__VA_ARGS__);
enum UserOperation{
#define ADD_OPERATION(name) name,\
USER_OPERATIONS()
#undef ADD_OPERATION
};
class UserOperationLog {
public:
UserOperationLog(){}
~UserOperationLog(){
if (logFile.isOpen())
{
logFile.flush();
logFile.close();
}
}
void init();
static UserOperationLog* Default(){
static UserOperationLog d;
return &d;
}
void log(UserOperation operation, bool processing = false);
private:
QString currentFileName;
QFile logFile;
QTextStream out;
};
#endif //GUI_USEROPERATIONLOG_H