392 lines
13 KiB
Plaintext
392 lines
13 KiB
Plaintext
#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>
|
|
|
|
#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();
|
|
//ui->pushButtonClear->hide();
|
|
fileDialog=0; // 先初始化为0 ,否则会死机
|
|
|
|
// LOGINFOFILE_DIR "/home/plg/loginfo/"
|
|
#ifdef DEBUG1
|
|
;
|
|
#else
|
|
QFile file(SYS_ROOT_DIR"UseEn");
|
|
if(file.exists()) //ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tab_3));
|
|
;
|
|
else {
|
|
//ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tab_3));
|
|
onReadRecorder( );
|
|
}
|
|
#endif
|
|
//ui->textEditRunStatus_3->setText(tr("测试变量加粗,可能会影响其它内容"));
|
|
//showMachineInformation(" "," "," "," ");
|
|
}
|
|
|
|
LogInfo::~LogInfo()
|
|
{
|
|
textEdit = 0;
|
|
textEdit2 = 0;
|
|
textEditMachInfo = 0;
|
|
if(fileDialog)
|
|
delete fileDialog;
|
|
delete ui;
|
|
}
|
|
|
|
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 += "Serial Number:" + SerialNum + "\n";
|
|
machInfo += "VernonBach Ver:" + VernonBachVer + "\n";
|
|
machInfo += "FPGA Ver:" + FpgaVer + "\n";
|
|
QFile kernelVer("/proc/version");
|
|
if (kernelVer.open(QFile::ReadOnly | QFile::Text)) {
|
|
QTextStream in(&kernelVer);
|
|
QString kernelVer=in.readLine();
|
|
QStringList list=kernelVer.split(" ");
|
|
QStringList list2=kernelVer.split("#");
|
|
machInfo = machInfo + "Kernel: ";
|
|
machInfo = machInfo + list.at(2);
|
|
machInfo = machInfo + "(" + list2.at(1) + ")" + "\n";
|
|
}
|
|
textEditMachInfo->setText(machInfo);
|
|
}
|
|
void LogInfo::showRemoteControll(QString SuperNode, QString edgeIP)
|
|
{
|
|
QString machInfo = textEditMachInfo->toPlainText();
|
|
machInfo += "SuperNode:" + SuperNode + "\n";
|
|
machInfo += "edgeIP:" + edgeIP + "\n";
|
|
textEditMachInfo->setText(machInfo);
|
|
}
|
|
void LogInfo::setRunStatusText(QString str){
|
|
if(!textEdit){
|
|
return;
|
|
}
|
|
const int maxTextEditHeight = 2000;
|
|
if(textEdit->document()->size().height() > maxTextEditHeight){
|
|
const QString fileNameL=QString(LOGINFOFILE_DIR)+QDate::currentDate().toString("yyyyMMdd") +"_test.txt";
|
|
QFile file(fileNameL);
|
|
if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
|
QTextStream out(&file);
|
|
out<<textEdit->toPlainText()<<endl;
|
|
file.close();
|
|
}
|
|
textEdit->clear(); //显示今天全部信息,加载太多可能会死机
|
|
}
|
|
str.prepend(QString("%1. %2:").arg(QString::number(runStatusTextIndex ++ )).arg(QTime::currentTime().toString("hh:mm:ss")));
|
|
textEdit->append(str);
|
|
saveRunStatusText();
|
|
}
|
|
|
|
void LogInfo::setDebugText(QString str){
|
|
#ifdef DEBUG1
|
|
if(textEdit3){
|
|
QSizeF size = textEdit3->document()->size();
|
|
if(size.height() > 500){
|
|
textEdit3->clear();
|
|
}
|
|
//str.prepend(QString("%1. %2: ").arg(QString::number(runStatusTextIndex ++ )).arg(QTime::currentTime().toString("hh:mm:ss")));
|
|
textEdit3->append(str);
|
|
}
|
|
#else
|
|
str;
|
|
#endif
|
|
}
|
|
QString LogInfo::getRunStatusText(void){
|
|
return "";
|
|
}
|
|
|
|
void LogInfo::saveRunStatusText()
|
|
{
|
|
QString fileName;
|
|
static QString r_fileName="";
|
|
QDir dir1;
|
|
dir1.setPath(LOGINFOFILE_DIR);
|
|
|
|
if(textEdit){
|
|
if(!dir1.exists())
|
|
{
|
|
dir1.mkdir(QString ("/home/plg/loginfo/"));
|
|
}
|
|
//currenDate=QDate::currentDate().toString("yyyyMMdd"); //
|
|
fileName=QString("/home/plg/loginfo/")+QDate::currentDate().toString("yyyyMMdd") +".txt";
|
|
#ifdef DEBUG1
|
|
// qDebug()<<fileName;
|
|
#endif
|
|
QTextDocumentWriter writer(fileName);
|
|
writer.write(textEdit->document());
|
|
if(r_fileName!="" && (r_fileName!=fileName)) textEdit->clear();
|
|
r_fileName=fileName;
|
|
}
|
|
}
|
|
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")+".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)).toAscii().data()));
|
|
}
|
|
|
|
//插入U盘后,检测目录usb-storage是否存在
|
|
if(!QDir("/proc/scsi/usb-storage/").exists()){
|
|
QMessageBox::warning(0,tr("警告"),tr("U盘未插入或无法识别,请重新插拔设备!"));
|
|
return;
|
|
}
|
|
|
|
system((char *)((QString("cp -r %1/* %2/").arg(LOGINFOFILE_DIR).arg(UDISK_LOGINFO_ROOT_DIR)).toAscii().data()));
|
|
//refreshListView(EditMode);
|
|
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/*.*").toAscii().data());
|
|
deleteDirectoryContents(dir1);
|
|
}
|
|
else system((char *)QString("mkdir /home/plg/loginfo/").toAscii().data());
|
|
|
|
saveRunStatusText();
|
|
}
|
|
|
|
}
|
|
|
|
void LogInfo::on_pushButtonClearCurrent_pressed()
|
|
{
|
|
QMessageBox::StandardButton ret;
|
|
ret = QMessageBox::information(this, tr("提示"),
|
|
tr("是否确认将当前日志全部清除?"),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
if (ret == QMessageBox::Ok){
|
|
QString fileNameL=QString("/home/plg/loginfo/")+QDate::currentDate().toString("yyyyMMdd") +"test.txt";
|
|
QFile file(fileNameL);
|
|
if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
|
;
|
|
}
|
|
else{
|
|
QTextStream out(&file);
|
|
out<<textEdit->toPlainText()<<endl;
|
|
fsync(file.handle());
|
|
file.close();
|
|
}
|
|
textEdit->clear();
|
|
runStatusTextIndex=0;
|
|
saveRunStatusText();
|
|
}
|
|
//setRunStatusText(QString("FILE COUNT:%1").arg( QDir("/home/plg/loginfo/").count()));
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|