Simple UserOperationLog table.
This commit is contained in:
56
src/log/LogFileTableModel.cpp
Normal file
56
src/log/LogFileTableModel.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by Krad on 2021/11/23.
|
||||
//
|
||||
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QTextStream>
|
||||
#include "LogFileTableModel.h"
|
||||
|
||||
LogFileTableModel::LogFileTableModel(QObject *parent) : QAbstractTableModel(parent) {
|
||||
|
||||
}
|
||||
|
||||
void LogFileTableModel::setFileName(QString fileName) {
|
||||
logdata.clear();
|
||||
QFile f;
|
||||
f.setFileName(fileName);
|
||||
if (!f.exists()) return;
|
||||
if(f.open(QFile::OpenModeFlag::ReadOnly | QFile::OpenModeFlag::Text))
|
||||
{
|
||||
|
||||
QTextStream in(&f);
|
||||
while(!in.atEnd()){
|
||||
logdata.push_back(in.readLine().split("\t"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVariant LogFileTableModel::data(const QModelIndex &index, int role) const {
|
||||
if (!index.isValid()) return QVariant();
|
||||
if (role == Qt::TextAlignmentRole) {
|
||||
return Qt::AlignCenter;
|
||||
}
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole )
|
||||
{
|
||||
return logdata[index.row()][index.column()];
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
int LogFileTableModel::rowCount(const QModelIndex &parent) const {
|
||||
if (!logdata.isEmpty()) return logdata.count();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LogFileTableModel::columnCount(const QModelIndex &parent) const {
|
||||
if (!logdata.isEmpty()) return logdata[0].count();
|
||||
return 0;
|
||||
}
|
||||
|
||||
QVariant LogFileTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||
if(role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
if(orientation == Qt::Horizontal)
|
||||
return headerStrings.at(section);
|
||||
return QVariant();
|
||||
}
|
||||
34
src/log/LogFileTableModel.h
Normal file
34
src/log/LogFileTableModel.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by Krad on 2021/11/23.
|
||||
//
|
||||
|
||||
#ifndef GUI_LOGFILETABLEMODEL_H
|
||||
#define GUI_LOGFILETABLEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QList>
|
||||
#include <QStringList>
|
||||
class LogFileTableModel : public QAbstractTableModel{
|
||||
public:
|
||||
LogFileTableModel(QObject *parent = nullptr);
|
||||
~LogFileTableModel(){}
|
||||
void setFileName(QString filename);
|
||||
void setHeader(QStringList header){
|
||||
headerStrings = header;
|
||||
}
|
||||
protected:
|
||||
//数据展示用
|
||||
QVariant data(const QModelIndex &index, int role) const;
|
||||
//行数,重新实现
|
||||
int rowCount(const QModelIndex &parent) const;
|
||||
//列数,重新实现
|
||||
int columnCount(const QModelIndex &parent) const;
|
||||
//标头
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
private:
|
||||
QList<QStringList> logdata;
|
||||
QStringList headerStrings;
|
||||
};
|
||||
|
||||
|
||||
#endif //GUI_LOGFILETABLEMODEL_H
|
||||
@@ -7,12 +7,16 @@
|
||||
#include <QDir>
|
||||
#include "appvals/AppGlobalValues.h"
|
||||
#include "models/User.h"
|
||||
#include <QDirIterator>
|
||||
#include <qdebug.h>
|
||||
|
||||
|
||||
const char * logDir = "./log";
|
||||
|
||||
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");
|
||||
if (!log_dir.exists(logDir)) log_dir.mkdir("log");
|
||||
currentFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
|
||||
logFile.setFileName(currentFileName);
|
||||
if (logFile.exists())
|
||||
{
|
||||
@@ -23,11 +27,29 @@ void UserOperationLog::init() {
|
||||
out.setDevice(&logFile);
|
||||
}
|
||||
|
||||
const char space = ' ';
|
||||
QString addSpace(char* str)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
QString getOperationName(UserOperation operation)
|
||||
{
|
||||
switch (operation) {
|
||||
#define ADD_OPERATION(name)\
|
||||
case name: return #name;
|
||||
case name: return addSpace(#name);
|
||||
USER_OPERATIONS()
|
||||
#undef ADD_OPERATION
|
||||
default:
|
||||
@@ -44,11 +66,11 @@ void UserOperationLog::log(UserOperation operation, bool processing) {
|
||||
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;
|
||||
out << now.toString(Qt::DateFormat::ISODateWithMs).replace("T","\t")<<"\t"<<UserName<<"\t"<<operationName<<endl;
|
||||
}
|
||||
|
||||
void UserOperationLog::reloadFile() {
|
||||
QString newFileName = "./log/" + QDate::currentDate().toString("yyyy-MM-dd")+QString("-op.log");
|
||||
QString newFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
|
||||
//inprocessing 暂时没有使用
|
||||
if (newFileName == currentFileName && !AppGlobalValues::InProcessing().toBool()) return;
|
||||
logFile.close();
|
||||
@@ -61,3 +83,29 @@ void UserOperationLog::reloadFile() {
|
||||
}
|
||||
out.setDevice(&logFile);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +50,16 @@ public:
|
||||
static UserOperationLog d;
|
||||
return &d;
|
||||
}
|
||||
static void cleanHistoryLog();
|
||||
|
||||
static QStringList getHistoryLogFiles();
|
||||
|
||||
void log(UserOperation operation, bool processing = false);
|
||||
void reloadFile();
|
||||
QString currentLogFile(){
|
||||
return currentFileName;
|
||||
}
|
||||
static void loadLogFromFile(QString filePath, QStringList& result);
|
||||
private:
|
||||
QString currentFileName;
|
||||
QFile logFile;
|
||||
|
||||
Reference in New Issue
Block a user