hlp:荆州版本
This commit is contained in:
+461
-461
@@ -1,461 +1,461 @@
|
||||
#include "LogInfo.h"
|
||||
#include "ui_loginfo.h"
|
||||
#include <QTime>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QIODevice>
|
||||
#include <QTextDocumentWriter>
|
||||
#include <QMessageBox>
|
||||
#include "DevOp.h"
|
||||
#include <stdio.h>
|
||||
#include "myinclude.h"
|
||||
#include <QScrollBar>
|
||||
#include <QSettings>
|
||||
|
||||
#ifndef LOGINFOFILE_DIR
|
||||
#define LOGINFOFILE_DIR "/home/plg/loginfo/"
|
||||
#endif
|
||||
#define UDISK_LOGINFO_ROOT_DIR "/udisk/log/"
|
||||
|
||||
LogInfo::LogInfo(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::LogInfo)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->textEditRunStatus->setReadOnly(true);
|
||||
ui->textEditRunStatus_2->setReadOnly(true);
|
||||
textEdit = ui->textEditRunStatus;
|
||||
textEdit->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
textEdit2 = ui->textEditRunStatus_2;
|
||||
//textEdit2->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
textEdit2->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
|
||||
|
||||
textEditMachInfo = ui->machinInformation;
|
||||
runStatusTextIndex = 0;
|
||||
openFileName ="";
|
||||
ui->labelPreFileName->setText("");
|
||||
//currenDate="";
|
||||
//r_CurrentDate=QDate::currentDate().toString("yyyyMMdd");
|
||||
|
||||
textEdit2->append(tr("请先打开历史状态文件!"));
|
||||
if(!QDir("/home/plg").exists())
|
||||
QDir("/home/plg").mkdir("/home/plg");
|
||||
readRunStatusText();
|
||||
runStatusTextIndex = textEdit->document()->lineCount();
|
||||
fileDialog=0; // 先初始化为0 ,否则会死机
|
||||
|
||||
QFile file(SYS_ROOT_DIR"UseEn");
|
||||
if(!file.exists()){
|
||||
onReadRecorder( );
|
||||
}
|
||||
timerLogdel=new QTimer(this);
|
||||
connect(timerLogdel, SIGNAL(timeout()), this, SLOT(deleteOldLog()));
|
||||
//延时60秒删除日志文件
|
||||
timerLogdel->start(60000);
|
||||
setScrollBar();
|
||||
}
|
||||
|
||||
LogInfo::~LogInfo()
|
||||
{
|
||||
textEdit = 0;
|
||||
textEdit2 = 0;
|
||||
textEditMachInfo = 0;
|
||||
if(fileDialog)
|
||||
delete fileDialog;
|
||||
delete ui;
|
||||
|
||||
disconnect(timerLogdel, SIGNAL(timeout()), this, SLOT(deleteOldLog()));
|
||||
timerLogdel->stop();
|
||||
}
|
||||
|
||||
QTextEdit *LogInfo::textEdit = 0;
|
||||
QTextEdit *LogInfo::textEdit2 = 0;
|
||||
QTextEdit *LogInfo::textEditMachInfo = 0;
|
||||
|
||||
int LogInfo::runStatusTextIndex = 0;
|
||||
|
||||
void LogInfo::showMachineInformation(QString Mac,QString SerialNum, QString VernonBachVer, QString FpgaVer)
|
||||
{
|
||||
QString machInfo;
|
||||
machInfo = "Mac:" + Mac + "\n";
|
||||
machInfo += "VernonBach Ver:" + VernonBachVer + "\n";
|
||||
machInfo += "FPGA Ver:" + FpgaVer;
|
||||
textEditMachInfo->setText(machInfo);
|
||||
}
|
||||
|
||||
void LogInfo::showVerionInformation(QString Mac,QString VernonBachVer, QString FpgaVer,QString STM32Ver,bool powerOnMode)
|
||||
{
|
||||
static bool flag = false;
|
||||
if(!flag){
|
||||
QString machInfo;
|
||||
machInfo = "Mac:" + Mac + "\n";
|
||||
machInfo += "VernonBach Ver:" + VernonBachVer +"|"+STM32Ver+ "\n";
|
||||
machInfo += "FPGA Ver: NO" + FpgaVer + "\n";
|
||||
machInfo += "PowerOnMode: " + QString::number(powerOnMode) + "\n";
|
||||
textEditMachInfo->append(machInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::showRemoteControll(QString SuperNode, QString edgeIP)
|
||||
{
|
||||
QString machInfo = textEditMachInfo->toPlainText();
|
||||
machInfo += "SuperNode:" + SuperNode + "\n";
|
||||
machInfo += "edgeIP:" + edgeIP + "\n";
|
||||
textEditMachInfo->setText(machInfo);
|
||||
}
|
||||
|
||||
//edit@01-11
|
||||
void LogInfo::setRunStatusText(QString str){
|
||||
if(!textEdit){
|
||||
return;
|
||||
}
|
||||
const int maxTextEditHeight = 2000;
|
||||
if(textEdit->document()->size().height() > maxTextEditHeight)
|
||||
{
|
||||
textEdit->clear(); //显示今天全部信息,加载太多可能会死机
|
||||
}
|
||||
str.prepend(QString("%1. %2:").arg(QString::number(runStatusTextIndex ++ )).arg(QTime::currentTime().toString("hh:mm:ss")));
|
||||
textEdit->append(str);
|
||||
{
|
||||
// 使用静态缓冲区批量写入,避免每次调用都 open/close 文件导致 ARM 板 I/O 繁忙
|
||||
static QStringList pendingLines;
|
||||
static QString lastDate;
|
||||
static int pendingTotalLen = 0; // 增量维护总字符数,避免每次扫描
|
||||
const QString dts = QDate::currentDate().toString("yyyyMMdd");
|
||||
if(dts != lastDate){
|
||||
// 日期变化时先把旧缓冲写入旧文件
|
||||
if(!pendingLines.isEmpty() && !lastDate.isEmpty()){
|
||||
QString oldFile = QString(LOGINFOFILE_DIR) + lastDate + "_1.txt";
|
||||
QFile f(oldFile);
|
||||
if(f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
||||
QTextStream out(&f);
|
||||
for(const QString &line : pendingLines)
|
||||
out << line << endl;
|
||||
f.close();
|
||||
}
|
||||
pendingLines.clear();
|
||||
pendingTotalLen = 0;
|
||||
}
|
||||
lastDate = dts;
|
||||
}
|
||||
pendingLines.append(str);
|
||||
pendingTotalLen += str.length();
|
||||
// 每累积 10 条或字符总量超过 2KB 时批量写入一次
|
||||
if(pendingLines.size() >= 10 || pendingTotalLen > 2048){
|
||||
QString fileNameL = QString(LOGINFOFILE_DIR) + dts + "_1.txt";
|
||||
QFile file(fileNameL);
|
||||
if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
||||
QTextStream out(&file);
|
||||
for(const QString &line : pendingLines)
|
||||
out << line << endl;
|
||||
file.close();
|
||||
}
|
||||
pendingLines.clear();
|
||||
pendingTotalLen = 0;
|
||||
truncatelog();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString LogInfo::getRunStatusText(void){
|
||||
return "";
|
||||
}
|
||||
|
||||
void LogInfo::readRunStatusText()
|
||||
{
|
||||
QString fileName;
|
||||
if(textEdit){
|
||||
QDir dir1;
|
||||
dir1.setPath(LOGINFOFILE_DIR);
|
||||
if(!dir1.exists())
|
||||
dir1.mkdir(QString ("/home/plg/loginfo/"));
|
||||
fileName=QString("/home/plg/loginfo/")+QDate::currentDate().toString("yyyyMMdd")+"_1.txt";
|
||||
QFile file(fileName);
|
||||
if(file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + file.fileName());
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEdit->setText(in.readAll());
|
||||
}
|
||||
file.close();
|
||||
ui->tabWidget->setCurrentWidget(ui->tab);
|
||||
}
|
||||
|
||||
QDate date1 = QDate::currentDate();
|
||||
for(int i=0;i<8;i++)
|
||||
{
|
||||
fileName=QString("/home/plg/loginfo/")+date1.addDays(-i-5).toString("yyyyMMdd")+".txt";
|
||||
file.setFileName(fileName);
|
||||
if(file.exists()){
|
||||
file.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::on_pushButtonExportFile_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将所有日志文件导出到U盘?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
//QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_LOGINFO_ROOT_DIR;
|
||||
QDir dir(UDISK_LOGINFO_ROOT_DIR);
|
||||
if(!dir.exists()){
|
||||
// system((char *)((QString("mkdir -p %1").arg(UDISK_LOGINFO_ROOT_DIR)).toLatin1().data()));
|
||||
system((char *)((QString("mkdir -p %1").arg(UDISK_LOGINFO_ROOT_DIR)).toLocal8Bit().data()));
|
||||
}
|
||||
|
||||
if(!dir.exists()){
|
||||
QMessageBox::warning(this,tr("警告"),tr("U盘目录创建失败"));
|
||||
return;
|
||||
}
|
||||
|
||||
//插入U盘后,检测目录usb-storage是否存在
|
||||
if(!QDir("/proc/scsi/usb-storage/").exists()){
|
||||
QMessageBox::warning(this,tr("警告"),tr("U盘未插入或无法识别,请重新插拔设备!"));
|
||||
return;
|
||||
}
|
||||
|
||||
// system((char *)((QString("cp -r %1/* %2/").arg(LOGINFOFILE_DIR).arg(UDISK_LOGINFO_ROOT_DIR)).toLatin1().data()));
|
||||
system((char *)((QString("cp -r %1/* %2/").arg(LOGINFOFILE_DIR).arg(UDISK_LOGINFO_ROOT_DIR)).toLocal8Bit().data()));
|
||||
//refreshListView(EditMode);
|
||||
system("sync");
|
||||
QMessageBox::information(this, tr("提示"),
|
||||
tr("成功导出到U盘"));
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonPreState_pressed(){
|
||||
|
||||
if(textEdit2){
|
||||
//setRunStatusText(QString("FILE COUNT:%1").arg( QDir("/home/plg/loginfo/").count()));
|
||||
//if( QDir("/home/plg/loginfo/").count()<4) {
|
||||
if( QDir("/home/plg/loginfo/").count()<1) {
|
||||
//QMessageBox::warning(this,"警告","没有历史文件");
|
||||
textEdit2->setText( "目前历史文件数量为0个");
|
||||
return;
|
||||
}
|
||||
//qDebug()<<"3333";
|
||||
if(openFileName.isEmpty()) textEdit2->clear();
|
||||
if(fileDialog) {
|
||||
// qDebug()<<"4444";
|
||||
disconnect(fileDialog,SIGNAL(accepted()),0,0); // 如果对话框已经定义,则删除控件关联,否则也会出错。
|
||||
|
||||
}
|
||||
else {//qDebug()<<"5";
|
||||
fileDialog = new MyFileDialog(0);
|
||||
//qDebug()<<"6";
|
||||
}
|
||||
//qDebug()<<"7";
|
||||
QDir dir1;
|
||||
dir1.setPath(QString ("/home/plg/loginfo/"));
|
||||
|
||||
if(!dir1.exists())
|
||||
{dir1.mkdir(QString ("/home/plg/loginfo/"));}
|
||||
if(fileDialog){
|
||||
//qDebug()<<"8";
|
||||
|
||||
if(fileDialog->directory().path()!="/home/plg/loginfo")
|
||||
fileDialog->setDirectory(QDir("/home/plg/loginfo"));
|
||||
|
||||
|
||||
//qDebug()<<"read File 3.2"<<dir1.Name;
|
||||
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onOpen_FIle_select()));
|
||||
//qDebug()<<"read File 4"<<dir1.Name;
|
||||
fileDialog->showFileDialog(tr("选择历史记录文件"),dir1.path(),QString("Txt files (*.txt)"));
|
||||
//qDebug()<<"read File 5";
|
||||
}
|
||||
//else qDebug()<<"read File:"<<dir1.path();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::onOpen_FIle_select()
|
||||
{
|
||||
QStringList files = fileDialog->selectedFiles();
|
||||
if( files.size()==0) return ;
|
||||
if(QString(files[0]).isNull()) return;
|
||||
QString fileName = files[0];
|
||||
|
||||
if(!fileName.endsWith(".txt",Qt::CaseInsensitive)) return ;
|
||||
|
||||
openFileName = fileName;
|
||||
QFile file(openFileName);
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + openFileName);
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEdit2->setText(in.readAll());
|
||||
textEdit2->moveCursor(QTextCursor::End);//滚动条移到底部
|
||||
//ui->labelPreFileName->setText(file.fileName());
|
||||
QFileInfo info(file.fileName());
|
||||
ui->labelPreFileName->setText(info.fileName());
|
||||
}
|
||||
file.close();
|
||||
ui->tabWidget->setCurrentWidget(ui->tab_2);
|
||||
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonClear_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将历史日志文件全部清除?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
QDir dir1(QString("/home/plg/loginfo/"));
|
||||
if(dir1.exists())
|
||||
{
|
||||
system((char *)QString("rm /home/plg/loginfo/*.*").toLatin1().data());
|
||||
deleteDirectoryContents(dir1);
|
||||
}
|
||||
else system((char *)QString("mkdir /home/plg/loginfo/").toLatin1().data());
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonClearCurrent_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将当前日志全部清除?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
textEdit->clear();
|
||||
runStatusTextIndex=0;
|
||||
}
|
||||
}
|
||||
void LogInfo::onReadRecorder()
|
||||
{
|
||||
QString fileName = ":/buge_record.txt";
|
||||
|
||||
if(!fileName.endsWith(".txt",Qt::CaseInsensitive)) return ;
|
||||
QFile file(fileName);
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + openFileName);
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEditMachInfo->setText(in.readAll());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::deleteDirectoryContents(const QDir &directory) {
|
||||
QList<QFileInfo> entries = directory.entryInfoList(
|
||||
QDir::NoDotAndDotDot | QDir::AllEntries);
|
||||
QString today = QDate::currentDate().toString(Qt::ISODate);
|
||||
// 使用传统的 for 循环遍历所有条目
|
||||
for (int i = 0; i < entries.size(); ++i) {
|
||||
QFileInfo entryInfo = entries.at(i);
|
||||
QString dirName=entryInfo.baseName();
|
||||
if(today==dirName)
|
||||
continue;
|
||||
QString fullPath = entryInfo.absoluteFilePath();
|
||||
|
||||
if (entryInfo.isFile() || entryInfo.isSymLink()) {
|
||||
// 如果是文件或符号链接,尝试删除
|
||||
if (!QFile::remove(fullPath)) {
|
||||
qDebug() << "Failed to remove file or symlink:" << fullPath;
|
||||
}
|
||||
} else if (entryInfo.isDir()) {
|
||||
// 如果是目录,递归调用以删除其内容
|
||||
QDir subDir(fullPath);
|
||||
deleteDirectoryContents(subDir);
|
||||
|
||||
// 目录现在应该是空的,尝试删除它
|
||||
if (!QDir().rmdir(fullPath)) {
|
||||
qDebug() << "Failed to remove directory:" << fullPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除指定文件夹超过七天的文件
|
||||
void LogInfo::deleteOldLog(const QString folderPath,int days){
|
||||
QDir dir(folderPath); // 创建一个QDir对象用于操作传入的文件夹路径
|
||||
if (!dir.exists()) { // 判断传入文件夹是否存在
|
||||
qDebug() << "文件夹不存在";
|
||||
return;
|
||||
}
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime(); // 获取当前时间
|
||||
QDateTime sevenDaysAgo = currentDateTime.addDays(-days); // 计算days天前的日期时间
|
||||
// 目录下的文件和子目录的信息列表,列出所有文件和子目录并按照最后修改时间排序
|
||||
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
|
||||
foreach (QFileInfo fileInfo ,fileInfoList) {
|
||||
if (!fileInfo.isDir()){
|
||||
QString fileExtension = fileInfo.suffix().toLower(); // 获取文件的扩展名并确保它是小写的
|
||||
// 选择删除那种图片类型
|
||||
if (fileExtension == "txt") {
|
||||
if (fileInfo.lastModified() < sevenDaysAgo && fileInfo.fileName().indexOf('_')!=-1) { // 获取最后一个文件夹修改时间和days传入的时间最对比
|
||||
if (QFile::remove(fileInfo.absoluteFilePath())) { // 删除满足条件的文件的绝对路径
|
||||
qDebug() << "删除成功" << fileInfo.absoluteFilePath();
|
||||
} else {
|
||||
qDebug() << "文件删除失败" << fileInfo.absoluteFilePath();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::deleteOldLog()
|
||||
{
|
||||
qDebug()<<"延时60秒删除过期日志";
|
||||
deleteOldLog(LOGINFOFILE_DIR,7);//删除一周前的日志
|
||||
timerLogdel->stop();
|
||||
}
|
||||
|
||||
void LogInfo::truncatelog()
|
||||
{
|
||||
const QString dts=QDate::currentDate().toString("yyyyMMdd") ;
|
||||
const QString fileNameL=QString(LOGINFOFILE_DIR)+dts +"_1.txt";
|
||||
QFile file(fileNameL);
|
||||
|
||||
if(file.size()>204800)//文件大小
|
||||
{
|
||||
QSettings settings("MySoft", "MyApp");
|
||||
int filenum=settings.value(dts,2).toInt();
|
||||
if(filenum==2)
|
||||
settings.clear();
|
||||
settings.setValue(dts,filenum+1);
|
||||
|
||||
qDebug()<<"rename="<<fileNameL << QString(LOGINFOFILE_DIR)+dts +QString("_%1.txt").arg(filenum);
|
||||
file.rename(QString(LOGINFOFILE_DIR)+dts +QString("_%1.txt").arg(filenum));
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::setScrollBar(){
|
||||
QString styleSheet = "\n\
|
||||
QTextEdit QScrollBar:vertical {\n\
|
||||
width: 50px; /* 设置垂直滚动条的宽度 */\n\
|
||||
background: rgba(0, 0, 0, 0); /* 滚动条背景透明 */\n\
|
||||
margin: 20px 0 20px 0; /* 为按钮留出空间 */\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::handle:vertical {\n\
|
||||
background: rgb(125, 170, 177); /* 滑块颜色 */\n\
|
||||
border-radius: 5px; /* 滑块圆角 */\n\
|
||||
min-height: 40px; /* 滑块最小高度 */\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::add-line:vertical {\n\
|
||||
height: 40px; /* 设置向下按钮的高度 */\n\
|
||||
background: rgb(255, 170, 127); /* 按钮颜色 */\n\
|
||||
subcontrol-position: bottom;\n\
|
||||
subcontrol-origin: margin;\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::sub-line:vertical {\n\
|
||||
height: 40px; /* 设置向上按钮的高度 */\n\
|
||||
background: rgb(255, 170, 127); /* 按钮颜色 */\n\
|
||||
subcontrol-position: top;\n\
|
||||
subcontrol-origin: margin;\n\
|
||||
}";
|
||||
ui->textEditRunStatus->setStyleSheet(styleSheet);
|
||||
ui->textEditRunStatus_2->setStyleSheet(styleSheet);
|
||||
}
|
||||
#include "LogInfo.h"
|
||||
#include "ui_loginfo.h"
|
||||
#include <QTime>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QIODevice>
|
||||
#include <QTextDocumentWriter>
|
||||
#include <QMessageBox>
|
||||
#include "DevOp.h"
|
||||
#include <stdio.h>
|
||||
#include "myinclude.h"
|
||||
#include <QScrollBar>
|
||||
#include <QSettings>
|
||||
|
||||
#ifndef LOGINFOFILE_DIR
|
||||
#define LOGINFOFILE_DIR "/home/plg/loginfo/"
|
||||
#endif
|
||||
#define UDISK_LOGINFO_ROOT_DIR "/udisk/log/"
|
||||
|
||||
LogInfo::LogInfo(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::LogInfo)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->textEditRunStatus->setReadOnly(true);
|
||||
ui->textEditRunStatus_2->setReadOnly(true);
|
||||
textEdit = ui->textEditRunStatus;
|
||||
textEdit->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
textEdit2 = ui->textEditRunStatus_2;
|
||||
//textEdit2->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
textEdit2->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 40px; }");
|
||||
|
||||
|
||||
textEditMachInfo = ui->machinInformation;
|
||||
runStatusTextIndex = 0;
|
||||
openFileName ="";
|
||||
ui->labelPreFileName->setText("");
|
||||
//currenDate="";
|
||||
//r_CurrentDate=QDate::currentDate().toString("yyyyMMdd");
|
||||
|
||||
textEdit2->append(tr("请先打开历史状态文件!"));
|
||||
if(!QDir("/home/plg").exists())
|
||||
QDir("/home/plg").mkdir("/home/plg");
|
||||
readRunStatusText();
|
||||
runStatusTextIndex = textEdit->document()->lineCount();
|
||||
fileDialog=0; // 先初始化为0 ,否则会死机
|
||||
|
||||
QFile file(SYS_ROOT_DIR"UseEn");
|
||||
if(!file.exists()){
|
||||
onReadRecorder( );
|
||||
}
|
||||
timerLogdel=new QTimer(this);
|
||||
connect(timerLogdel, SIGNAL(timeout()), this, SLOT(deleteOldLog()));
|
||||
//延时60秒删除日志文件
|
||||
timerLogdel->start(60000);
|
||||
setScrollBar();
|
||||
}
|
||||
|
||||
LogInfo::~LogInfo()
|
||||
{
|
||||
textEdit = 0;
|
||||
textEdit2 = 0;
|
||||
textEditMachInfo = 0;
|
||||
if(fileDialog)
|
||||
delete fileDialog;
|
||||
delete ui;
|
||||
|
||||
disconnect(timerLogdel, SIGNAL(timeout()), this, SLOT(deleteOldLog()));
|
||||
timerLogdel->stop();
|
||||
}
|
||||
|
||||
QTextEdit *LogInfo::textEdit = 0;
|
||||
QTextEdit *LogInfo::textEdit2 = 0;
|
||||
QTextEdit *LogInfo::textEditMachInfo = 0;
|
||||
|
||||
int LogInfo::runStatusTextIndex = 0;
|
||||
|
||||
void LogInfo::showMachineInformation(QString Mac,QString SerialNum, QString VernonBachVer, QString FpgaVer)
|
||||
{
|
||||
QString machInfo;
|
||||
machInfo = "Mac:" + Mac + "\n";
|
||||
machInfo += "VernonBach Ver:" + VernonBachVer + "\n";
|
||||
machInfo += "FPGA Ver:" + FpgaVer;
|
||||
textEditMachInfo->setText(machInfo);
|
||||
}
|
||||
|
||||
void LogInfo::showVerionInformation(QString Mac,QString VernonBachVer, QString FpgaVer,QString STM32Ver,bool powerOnMode)
|
||||
{
|
||||
static bool flag = false;
|
||||
if(!flag){
|
||||
QString machInfo;
|
||||
machInfo = "Mac:" + Mac + "\n";
|
||||
machInfo += "VernonBach Ver:" + VernonBachVer +"|"+STM32Ver+ "\n";
|
||||
machInfo += "FPGA Ver: NO" + FpgaVer + "\n";
|
||||
machInfo += "PowerOnMode: " + QString::number(powerOnMode) + "\n";
|
||||
textEditMachInfo->append(machInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::showRemoteControll(QString SuperNode, QString edgeIP)
|
||||
{
|
||||
QString machInfo = textEditMachInfo->toPlainText();
|
||||
machInfo += "SuperNode:" + SuperNode + "\n";
|
||||
machInfo += "edgeIP:" + edgeIP + "\n";
|
||||
textEditMachInfo->setText(machInfo);
|
||||
}
|
||||
|
||||
//edit@01-11
|
||||
void LogInfo::setRunStatusText(QString str){
|
||||
if(!textEdit){
|
||||
return;
|
||||
}
|
||||
const int maxTextEditHeight = 2000;
|
||||
if(textEdit->document()->size().height() > maxTextEditHeight)
|
||||
{
|
||||
textEdit->clear(); //显示今天全部信息,加载太多可能会死机
|
||||
}
|
||||
str.prepend(QString("%1. %2:").arg(QString::number(runStatusTextIndex ++ )).arg(QTime::currentTime().toString("hh:mm:ss")));
|
||||
textEdit->append(str);
|
||||
{
|
||||
// 使用静态缓冲区批量写入,避免每次调用都 open/close 文件导致 ARM 板 I/O 繁忙
|
||||
static QStringList pendingLines;
|
||||
static QString lastDate;
|
||||
static int pendingTotalLen = 0; // 增量维护总字符数,避免每次扫描
|
||||
const QString dts = QDate::currentDate().toString("yyyyMMdd");
|
||||
if(dts != lastDate){
|
||||
// 日期变化时先把旧缓冲写入旧文件
|
||||
if(!pendingLines.isEmpty() && !lastDate.isEmpty()){
|
||||
QString oldFile = QString(LOGINFOFILE_DIR) + lastDate + "_1.txt";
|
||||
QFile f(oldFile);
|
||||
if(f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
||||
QTextStream out(&f);
|
||||
for(const QString &line : pendingLines)
|
||||
out << line << endl;
|
||||
f.close();
|
||||
}
|
||||
pendingLines.clear();
|
||||
pendingTotalLen = 0;
|
||||
}
|
||||
lastDate = dts;
|
||||
}
|
||||
pendingLines.append(str);
|
||||
pendingTotalLen += str.length();
|
||||
// 每累积 10 条或字符总量超过 2KB 时批量写入一次
|
||||
if(pendingLines.size() >= 10 || pendingTotalLen > 2048){
|
||||
QString fileNameL = QString(LOGINFOFILE_DIR) + dts + "_1.txt";
|
||||
QFile file(fileNameL);
|
||||
if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
||||
QTextStream out(&file);
|
||||
for(const QString &line : pendingLines)
|
||||
out << line << endl;
|
||||
file.close();
|
||||
}
|
||||
pendingLines.clear();
|
||||
pendingTotalLen = 0;
|
||||
truncatelog();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString LogInfo::getRunStatusText(void){
|
||||
return "";
|
||||
}
|
||||
|
||||
void LogInfo::readRunStatusText()
|
||||
{
|
||||
QString fileName;
|
||||
if(textEdit){
|
||||
QDir dir1;
|
||||
dir1.setPath(LOGINFOFILE_DIR);
|
||||
if(!dir1.exists())
|
||||
dir1.mkdir(QString ("/home/plg/loginfo/"));
|
||||
fileName=QString("/home/plg/loginfo/")+QDate::currentDate().toString("yyyyMMdd")+"_1.txt";
|
||||
QFile file(fileName);
|
||||
if(file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + file.fileName());
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEdit->setText(in.readAll());
|
||||
}
|
||||
file.close();
|
||||
ui->tabWidget->setCurrentWidget(ui->tab);
|
||||
}
|
||||
|
||||
QDate date1 = QDate::currentDate();
|
||||
for(int i=0;i<8;i++)
|
||||
{
|
||||
fileName=QString("/home/plg/loginfo/")+date1.addDays(-i-5).toString("yyyyMMdd")+".txt";
|
||||
file.setFileName(fileName);
|
||||
if(file.exists()){
|
||||
file.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::on_pushButtonExportFile_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将所有日志文件导出到U盘?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
//QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_LOGINFO_ROOT_DIR;
|
||||
QDir dir(UDISK_LOGINFO_ROOT_DIR);
|
||||
if(!dir.exists()){
|
||||
// system((char *)((QString("mkdir -p %1").arg(UDISK_LOGINFO_ROOT_DIR)).toLatin1().data()));
|
||||
system((char *)((QString("mkdir -p %1").arg(UDISK_LOGINFO_ROOT_DIR)).toLocal8Bit().data()));
|
||||
}
|
||||
|
||||
if(!dir.exists()){
|
||||
QMessageBox::warning(this,tr("警告"),tr("U盘目录创建失败"));
|
||||
return;
|
||||
}
|
||||
|
||||
//插入U盘后,检测目录usb-storage是否存在
|
||||
if(!QDir("/proc/scsi/usb-storage/").exists()){
|
||||
QMessageBox::warning(this,tr("警告"),tr("U盘未插入或无法识别,请重新插拔设备!"));
|
||||
return;
|
||||
}
|
||||
|
||||
// system((char *)((QString("cp -r %1/* %2/").arg(LOGINFOFILE_DIR).arg(UDISK_LOGINFO_ROOT_DIR)).toLatin1().data()));
|
||||
system((char *)((QString("cp -r %1/* %2/").arg(LOGINFOFILE_DIR).arg(UDISK_LOGINFO_ROOT_DIR)).toLocal8Bit().data()));
|
||||
//refreshListView(EditMode);
|
||||
system("sync");
|
||||
QMessageBox::information(this, tr("提示"),
|
||||
tr("成功导出到U盘"));
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonPreState_pressed(){
|
||||
|
||||
if(textEdit2){
|
||||
//setRunStatusText(QString("FILE COUNT:%1").arg( QDir("/home/plg/loginfo/").count()));
|
||||
//if( QDir("/home/plg/loginfo/").count()<4) {
|
||||
if( QDir("/home/plg/loginfo/").count()<1) {
|
||||
//QMessageBox::warning(this,"警告","没有历史文件");
|
||||
textEdit2->setText( "目前历史文件数量为0个");
|
||||
return;
|
||||
}
|
||||
//qDebug()<<"3333";
|
||||
if(openFileName.isEmpty()) textEdit2->clear();
|
||||
if(fileDialog) {
|
||||
// qDebug()<<"4444";
|
||||
disconnect(fileDialog,SIGNAL(accepted()),0,0); // 如果对话框已经定义,则删除控件关联,否则也会出错。
|
||||
|
||||
}
|
||||
else {//qDebug()<<"5";
|
||||
fileDialog = new MyFileDialog(0);
|
||||
//qDebug()<<"6";
|
||||
}
|
||||
//qDebug()<<"7";
|
||||
QDir dir1;
|
||||
dir1.setPath(QString ("/home/plg/loginfo/"));
|
||||
|
||||
if(!dir1.exists())
|
||||
{dir1.mkdir(QString ("/home/plg/loginfo/"));}
|
||||
if(fileDialog){
|
||||
//qDebug()<<"8";
|
||||
|
||||
if(fileDialog->directory().path()!="/home/plg/loginfo")
|
||||
fileDialog->setDirectory(QDir("/home/plg/loginfo"));
|
||||
|
||||
|
||||
//qDebug()<<"read File 3.2"<<dir1.Name;
|
||||
connect(fileDialog,SIGNAL(accepted()),this,SLOT(onOpen_FIle_select()));
|
||||
//qDebug()<<"read File 4"<<dir1.Name;
|
||||
fileDialog->showFileDialog(tr("选择历史记录文件"),dir1.path(),QString("Txt files (*.txt)"));
|
||||
//qDebug()<<"read File 5";
|
||||
}
|
||||
//else qDebug()<<"read File:"<<dir1.path();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::onOpen_FIle_select()
|
||||
{
|
||||
QStringList files = fileDialog->selectedFiles();
|
||||
if( files.size()==0) return ;
|
||||
if(QString(files[0]).isNull()) return;
|
||||
QString fileName = files[0];
|
||||
|
||||
if(!fileName.endsWith(".txt",Qt::CaseInsensitive)) return ;
|
||||
|
||||
openFileName = fileName;
|
||||
QFile file(openFileName);
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + openFileName);
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEdit2->setText(in.readAll());
|
||||
textEdit2->moveCursor(QTextCursor::End);//滚动条移到底部
|
||||
//ui->labelPreFileName->setText(file.fileName());
|
||||
QFileInfo info(file.fileName());
|
||||
ui->labelPreFileName->setText(info.fileName());
|
||||
}
|
||||
file.close();
|
||||
ui->tabWidget->setCurrentWidget(ui->tab_2);
|
||||
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonClear_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将历史日志文件全部清除?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
QDir dir1(QString("/home/plg/loginfo/"));
|
||||
if(dir1.exists())
|
||||
{
|
||||
system((char *)QString("rm /home/plg/loginfo/*.*").toLatin1().data());
|
||||
deleteDirectoryContents(dir1);
|
||||
}
|
||||
else system((char *)QString("mkdir /home/plg/loginfo/").toLatin1().data());
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::on_pushButtonClearCurrent_pressed()
|
||||
{
|
||||
QMessageBox::StandardButton ret;
|
||||
ret = QMessageBox::information(this, tr("提示"),
|
||||
tr("是否确认将当前日志全部清除?"),
|
||||
QMessageBox::Ok | QMessageBox::Cancel);
|
||||
if (ret == QMessageBox::Ok){
|
||||
textEdit->clear();
|
||||
runStatusTextIndex=0;
|
||||
}
|
||||
}
|
||||
void LogInfo::onReadRecorder()
|
||||
{
|
||||
QString fileName = ":/buge_record.txt";
|
||||
|
||||
if(!fileName.endsWith(".txt",Qt::CaseInsensitive)) return ;
|
||||
QFile file(fileName);
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::warning(this,tr("警告"),tr("无法打开文件") + openFileName);
|
||||
}
|
||||
else{
|
||||
QTextStream in (&file);
|
||||
textEditMachInfo->setText(in.readAll());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LogInfo::deleteDirectoryContents(const QDir &directory) {
|
||||
QList<QFileInfo> entries = directory.entryInfoList(
|
||||
QDir::NoDotAndDotDot | QDir::AllEntries);
|
||||
QString today = QDate::currentDate().toString(Qt::ISODate);
|
||||
// 使用传统的 for 循环遍历所有条目
|
||||
for (int i = 0; i < entries.size(); ++i) {
|
||||
QFileInfo entryInfo = entries.at(i);
|
||||
QString dirName=entryInfo.baseName();
|
||||
if(today==dirName)
|
||||
continue;
|
||||
QString fullPath = entryInfo.absoluteFilePath();
|
||||
|
||||
if (entryInfo.isFile() || entryInfo.isSymLink()) {
|
||||
// 如果是文件或符号链接,尝试删除
|
||||
if (!QFile::remove(fullPath)) {
|
||||
qDebug() << "Failed to remove file or symlink:" << fullPath;
|
||||
}
|
||||
} else if (entryInfo.isDir()) {
|
||||
// 如果是目录,递归调用以删除其内容
|
||||
QDir subDir(fullPath);
|
||||
deleteDirectoryContents(subDir);
|
||||
|
||||
// 目录现在应该是空的,尝试删除它
|
||||
if (!QDir().rmdir(fullPath)) {
|
||||
qDebug() << "Failed to remove directory:" << fullPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除指定文件夹超过七天的文件
|
||||
void LogInfo::deleteOldLog(const QString folderPath,int days){
|
||||
QDir dir(folderPath); // 创建一个QDir对象用于操作传入的文件夹路径
|
||||
if (!dir.exists()) { // 判断传入文件夹是否存在
|
||||
qDebug() << "文件夹不存在";
|
||||
return;
|
||||
}
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime(); // 获取当前时间
|
||||
QDateTime sevenDaysAgo = currentDateTime.addDays(-days); // 计算days天前的日期时间
|
||||
// 目录下的文件和子目录的信息列表,列出所有文件和子目录并按照最后修改时间排序
|
||||
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
|
||||
foreach (QFileInfo fileInfo ,fileInfoList) {
|
||||
if (!fileInfo.isDir()){
|
||||
QString fileExtension = fileInfo.suffix().toLower(); // 获取文件的扩展名并确保它是小写的
|
||||
// 选择删除那种图片类型
|
||||
if (fileExtension == "txt") {
|
||||
if (fileInfo.lastModified() < sevenDaysAgo && fileInfo.fileName().indexOf('_')!=-1) { // 获取最后一个文件夹修改时间和days传入的时间最对比
|
||||
if (QFile::remove(fileInfo.absoluteFilePath())) { // 删除满足条件的文件的绝对路径
|
||||
qDebug() << "删除成功" << fileInfo.absoluteFilePath();
|
||||
} else {
|
||||
qDebug() << "文件删除失败" << fileInfo.absoluteFilePath();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::deleteOldLog()
|
||||
{
|
||||
qDebug()<<"延时60秒删除过期日志";
|
||||
deleteOldLog(LOGINFOFILE_DIR,7);//删除一周前的日志
|
||||
timerLogdel->stop();
|
||||
}
|
||||
|
||||
void LogInfo::truncatelog()
|
||||
{
|
||||
const QString dts=QDate::currentDate().toString("yyyyMMdd") ;
|
||||
const QString fileNameL=QString(LOGINFOFILE_DIR)+dts +"_1.txt";
|
||||
QFile file(fileNameL);
|
||||
|
||||
if(file.size()>204800)//文件大小
|
||||
{
|
||||
QSettings settings("MySoft", "MyApp");
|
||||
int filenum=settings.value(dts,2).toInt();
|
||||
if(filenum==2)
|
||||
settings.clear();
|
||||
settings.setValue(dts,filenum+1);
|
||||
|
||||
qDebug()<<"rename="<<fileNameL << QString(LOGINFOFILE_DIR)+dts +QString("_%1.txt").arg(filenum);
|
||||
file.rename(QString(LOGINFOFILE_DIR)+dts +QString("_%1.txt").arg(filenum));
|
||||
}
|
||||
}
|
||||
|
||||
void LogInfo::setScrollBar(){
|
||||
QString styleSheet = "\n\
|
||||
QTextEdit QScrollBar:vertical {\n\
|
||||
width: 50px; /* 设置垂直滚动条的宽度 */\n\
|
||||
background: rgba(0, 0, 0, 0); /* 滚动条背景透明 */\n\
|
||||
margin: 20px 0 20px 0; /* 为按钮留出空间 */\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::handle:vertical {\n\
|
||||
background: rgb(125, 170, 177); /* 滑块颜色 */\n\
|
||||
border-radius: 5px; /* 滑块圆角 */\n\
|
||||
min-height: 40px; /* 滑块最小高度 */\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::add-line:vertical {\n\
|
||||
height: 40px; /* 设置向下按钮的高度 */\n\
|
||||
background: rgb(255, 170, 127); /* 按钮颜色 */\n\
|
||||
subcontrol-position: bottom;\n\
|
||||
subcontrol-origin: margin;\n\
|
||||
}\n\
|
||||
QTextEdit QScrollBar::sub-line:vertical {\n\
|
||||
height: 40px; /* 设置向上按钮的高度 */\n\
|
||||
background: rgb(255, 170, 127); /* 按钮颜色 */\n\
|
||||
subcontrol-position: top;\n\
|
||||
subcontrol-origin: margin;\n\
|
||||
}";
|
||||
ui->textEditRunStatus->setStyleSheet(styleSheet);
|
||||
ui->textEditRunStatus_2->setStyleSheet(styleSheet);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user