提交原始齿轮泵代码

This commit is contained in:
kite_lucs
2026-08-26 16:13:59 +08:00
commit afc8fcf2a4
1688 changed files with 316635 additions and 0 deletions
+273
View File
@@ -0,0 +1,273 @@
#include "filelogger.h"
#include <QTime>
#include <QStringList>
#include <QThread>
#include <QtGlobal>
#include <QFile>
#include <QTimerEvent>
#include <QDir>
#include <QFileInfo>
#include <stdio.h>
#include<iostream>
#include<string>
#include<QDate>
#include "LogInfo.h"
#define LOG_FILE_PATH "/home/plg/loginfo/"
void FileLogger::refreshSettings()
{
mutex.lock();
QString oldFileName=fileName;
settings->sync();
fileName = LOG_FILE_PATH;
// fileName=settings->value("fileName").toString();
// LogInfo::setRunStatusText(tr("fileName: %1").arg(fileName));
QDate today = QDate::currentDate();
QString todayString=today.toString(Qt::ISODate);
// 计算前七天的日期
//QDate oneWeekAgo = today.addDays(-7);
fileName+=today.toString(Qt::ISODate);
fileName+="/";
fileName+="log.txt";
static QString lastDay = "";
if(lastDay!=todayString)
{
TimerDel->start(30000);
lastDay=todayString;
}
#ifdef Q_OS_WIN32
if (QDir::isRelativePath(fileName) && settings->format()!=QSettings::NativeFormat)
#else
if (QDir::isRelativePath(fileName))
#endif
{
QFileInfo configFile(settings->fileName());
fileName=QFileInfo(configFile.absolutePath(),fileName).absoluteFilePath();
}
maxSize=settings->value("maxSize",0).toLongLong();
maxBackups=settings->value("maxBackups",0).toInt();
msgFormat=settings->value("msgFormat","{timestamp} {type} {msg}").toString();
timestampFormat=settings->value("timestampFormat","yyyy-MM-dd hh:mm:ss.zzz").toString();
bufferSize=settings->value("bufferSize",0).toInt();
QByteArray minLevelStr = settings->value("minLevel","ALL").toByteArray();
if (minLevelStr=="ALL" || minLevelStr=="DEBUG" || minLevelStr=="0")
{
minLevel=QtDebugMsg;
}
else if (minLevelStr=="WARNING" || minLevelStr=="WARN" || minLevelStr=="1")
{
minLevel=QtWarningMsg;
}
else if (minLevelStr=="ERROR" || minLevelStr=="CRITICAL" || minLevelStr=="2")
{
minLevel=QtCriticalMsg;
}
else if (minLevelStr=="FATAL" || minLevelStr=="3")
{
// minLevel=QtMsgType::QtFatalMsg;
minLevel=QtFatalMsg;
}
// else if (minLevelStr=="INFO" || minLevelStr=="4")
// {
// minLevel=QtMsgType::QtInfoMsg;
// }
if (oldFileName!=fileName)
{
fprintf(stderr,"Logging to %s\n",qPrintable(fileName));
close();
open();
}
mutex.unlock();
}
FileLogger::FileLogger(QSettings *settings, const int refreshInterval, QObject* parent)
: Logger(parent)
{
TimerDel = new QTimer(this);
connect(TimerDel,SIGNAL(timeout()),this,SLOT(timeOutToDelete()));
Q_ASSERT(settings!=nullptr);
Q_ASSERT(refreshInterval>=0);
this->settings=settings;
file=nullptr;
if (refreshInterval>0)
{
refreshTimer.start(refreshInterval,this);
}
flushTimer.start(1000,this);
refreshSettings();
}
FileLogger::~FileLogger()
{
close();
}
void FileLogger::write(const LogMessage* logMessage)
{
if (file)
{
// qDebug()<<"Critical message output test\n";
// qCritical("This is a critical message of test\n");
file->write(qPrintable(logMessage->toString(msgFormat,timestampFormat)));
if (logMessage->getType()>=QtCriticalMsg)
{
file->flush();
}
if (file->error())
{
qWarning("Cannot write to log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
close();
}
}
if (!file)
{
Logger::write(logMessage);
}
}
void FileLogger::open()
{
if (fileName.isEmpty())
{
qWarning("Name of logFile is empty");
}
else {
file=new QFile(fileName);
QFileInfo fileInfo(fileName);
// 检查文件路径是否有效
if (!fileInfo.exists()) {
// 获取文件所在的目录路径
QString dirPath = QFileInfo(fileName).absoluteDir().path();
// 检查目录是否存在
if (!QDir(dirPath).exists()) {
// 如果目录不存在,则创建目录
QDir().mkpath(dirPath);
if (!QDir(dirPath).exists()) {
// 如果创建目录失败,显示错误消息
qDebug("Cannot create directory for log file: %s", qPrintable(dirPath));
delete file;
file = nullptr;
return;
}
}
}
if (!file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
qWarning("Cannot open log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
file=nullptr;
}
}
}
void FileLogger::close()
{
if (file)
{
file->close();
delete file;
file=nullptr;
}
}
void FileLogger::rotate() {
int count=0;
forever
{
QFile bakFile(QString("%1.%2").arg(fileName).arg(count+1));
if (bakFile.exists())
{
++count;
}
else
{
break;
}
}
while (maxBackups>0 && count>=maxBackups)
{
QFile::remove(QString("%1.%2").arg(fileName).arg(count));
--count;
}
for (int i=count; i>0; --i) {
QFile::rename(QString("%1.%2").arg(fileName).arg(i),QString("%1.%2").arg(fileName).arg(i+1));
}
QFile::rename(fileName,fileName+".1");
}
void FileLogger::timerEvent(QTimerEvent* event)
{
if (!event)
{
return;
}
else if (event->timerId()==refreshTimer.timerId())
{
refreshSettings();
}
else if (event->timerId()==flushTimer.timerId() && file)
{
mutex.lock();
file->flush();
if (maxSize>0 && file->size()>=maxSize)
{
close();
rotate();
open();
}
mutex.unlock();
}
}
bool FileLogger::removeDirectory(const QString &directoryPath) {
// 检查目录是否存在
QDir dir(directoryPath);
if (dir.exists()) {
system((char *)((QString("rm -r %1").arg(directoryPath)).toLatin1().data()));
return true;
}
return false;
}
void FileLogger::timeOutToDelete(){
QDate today = QDate::currentDate();
for(int daysAgo=14;daysAgo>=7;--daysAgo){
QString needToDelFoldPath=LOG_FILE_PATH;
QDate pastDate=today.addDays(-daysAgo);
needToDelFoldPath+=pastDate.toString(Qt::ISODate);
needToDelFoldPath+="/";
if (QDir(needToDelFoldPath).exists()) {
bool result= removeDirectory(needToDelFoldPath);
if(result){
qDebug()<<"The directory and all its contents have been removed";
LogInfo::setRunStatusText(tr("The directory and all its contents have been removed"));
}else{
qDebug() << "Failed to remove the directory";
LogInfo::setRunStatusText(tr("Failed to remove the directory"));
}
}
}
TimerDel->stop();
}