2021-11-19 15:36:12 +08:00
|
|
|
//
|
|
|
|
|
// Created by Krad on 2021/11/19.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "UserOperationLog.h"
|
|
|
|
|
#include <QDate>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include "appvals/AppGlobalValues.h"
|
|
|
|
|
#include "models/User.h"
|
2021-11-23 13:22:00 +08:00
|
|
|
#include <QDirIterator>
|
|
|
|
|
#include <qdebug.h>
|
2021-11-19 15:36:12 +08:00
|
|
|
|
|
|
|
|
|
2021-11-23 13:22:00 +08:00
|
|
|
const char * logDir = "./log";
|
|
|
|
|
|
2021-11-19 15:36:12 +08:00
|
|
|
void UserOperationLog::init() {
|
|
|
|
|
QDir log_dir("./");
|
2021-11-23 13:22:00 +08:00
|
|
|
if (!log_dir.exists(logDir)) log_dir.mkdir("log");
|
|
|
|
|
currentFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
|
2021-11-19 15:36:12 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 13:22:00 +08:00
|
|
|
const char space = ' ';
|
2021-11-26 10:49:09 +08:00
|
|
|
QString addSpace(const char* str)
|
2021-11-23 13:22:00 +08:00
|
|
|
{
|
|
|
|
|
QString s;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; str[i]!='\0'; ++i) {
|
|
|
|
|
if (str[i]>='A' && str[i]<='Z' && i>0)
|
|
|
|
|
{
|
|
|
|
|
s.append(space);
|
|
|
|
|
s.append(str[i]);
|
|
|
|
|
} else{
|
|
|
|
|
s.append(str[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 15:36:12 +08:00
|
|
|
QString getOperationName(UserOperation operation)
|
|
|
|
|
{
|
|
|
|
|
switch (operation) {
|
|
|
|
|
#define ADD_OPERATION(name)\
|
2021-11-23 13:22:00 +08:00
|
|
|
case name: return addSpace(#name);
|
2021-11-19 15:36:12 +08:00
|
|
|
USER_OPERATIONS()
|
|
|
|
|
#undef ADD_OPERATION
|
|
|
|
|
default:
|
|
|
|
|
return "unknow error";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UserOperationLog::log(UserOperation operation, bool processing) {
|
2021-11-19 17:21:12 +08:00
|
|
|
reloadFile();
|
2021-11-19 15:36:12 +08:00
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
AppGlobalValues::setLastOperationTime(now);
|
|
|
|
|
AppGlobalValues::setLastOperation(operation);
|
|
|
|
|
QString operationName = getOperationName(operation);
|
|
|
|
|
AppGlobalValues::setInProcessing(processing);
|
2022-01-11 13:04:38 +08:00
|
|
|
QString UserName = (!User::Current() || User::Current()->getUserCode().isEmpty())?"anonymous":User::Current()->getUserCode();
|
2021-11-23 13:22:00 +08:00
|
|
|
out << now.toString(Qt::DateFormat::ISODateWithMs).replace("T","\t")<<"\t"<<UserName<<"\t"<<operationName<<endl;
|
2021-11-19 15:36:12 +08:00
|
|
|
}
|
2021-11-19 17:21:12 +08:00
|
|
|
|
|
|
|
|
void UserOperationLog::reloadFile() {
|
2021-11-23 13:22:00 +08:00
|
|
|
QString newFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
|
2021-11-19 17:21:12 +08:00
|
|
|
//inprocessing 暂时没有使用
|
|
|
|
|
if (newFileName == currentFileName && !AppGlobalValues::InProcessing().toBool()) return;
|
|
|
|
|
logFile.close();
|
|
|
|
|
logFile.setFileName(newFileName);
|
|
|
|
|
if (logFile.exists())
|
|
|
|
|
{
|
|
|
|
|
logFile.open(QFile::OpenModeFlag::Append | QFile::OpenModeFlag::Text);
|
|
|
|
|
} else{
|
|
|
|
|
logFile.open(QFile::OpenModeFlag::NewOnly | QFile::OpenModeFlag::Text);
|
|
|
|
|
}
|
|
|
|
|
out.setDevice(&logFile);
|
|
|
|
|
}
|
2021-11-23 13:22:00 +08:00
|
|
|
|
|
|
|
|
QStringList UserOperationLog::getHistoryLogFiles() {
|
|
|
|
|
QDirIterator iter(logDir, QDir::Files | QDir::NoSymLinks);
|
|
|
|
|
QStringList list;
|
|
|
|
|
while(iter.hasNext())
|
|
|
|
|
{
|
|
|
|
|
iter.next();
|
|
|
|
|
list << iter.fileInfo().absoluteFilePath();
|
|
|
|
|
qDebug()<<iter.fileInfo().absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UserOperationLog::loadLogFromFile(QString path, QStringList& result) {
|
|
|
|
|
QFile f;
|
|
|
|
|
f.setFileName(path);
|
|
|
|
|
if (!f.exists()) return;
|
|
|
|
|
if(f.open(QFile::OpenModeFlag::ReadOnly | QFile::OpenModeFlag::Text))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QTextStream in(&f);
|
|
|
|
|
while(!in.atEnd()){
|
|
|
|
|
result << in.readLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|