From 3ad8233566297da11132fed64e996183904788b8 Mon Sep 17 00:00:00 2001 From: ruizhe Date: Thu, 19 Oct 2023 11:18:34 +0800 Subject: [PATCH] chore:after export all log program abort --- debian/changelog | 6 +++++ getcontent.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++--- getcontent.h | 3 ++- logtools.cpp | 22 ++++-------------- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/debian/changelog b/debian/changelog index 9e38f03..023d17f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +nfs-logtools (1.0.0-5) unstable; urgency=medium + + * fix: after export all log program abort. + + -- ruizhe Thu, 19 Oct 2023 11:15:13 +0800 + nfs-logtools (1.0.0-4) unstable; urgency=medium * fix: auto scaling and fix error. diff --git a/getcontent.cpp b/getcontent.cpp index 0e70044..8931bbb 100644 --- a/getcontent.cpp +++ b/getcontent.cpp @@ -11,13 +11,51 @@ QString GetContent::getHomePath() QStringList getUserName; QStringList curHome; QString curUser = ""; + QString ret =""; + QString tmpLine =""; + QStringList contentList; + QStringList tmpLineList; + QString firstLine = ""; QString curUsrPawdInfo = ""; - execCmd->start("who", params); + /* 使用who命令获取当前用户名,如果多用户登灵会产生问题*/ + /* execCmd->start("who", params); execCmd->waitForFinished(); curUser = execCmd->readAllStandardOutput(); getUserName = curUser.split(" "); - curUser = getUserName[0]; + curUser = getUserName[0];*/ + + /*使用last命令获取首行still logged in的用户*/ + execCmd->start("last", params); + execCmd->waitForFinished(); + ret = execCmd->readAllStandardOutput(); + + //将整体返回的字符串按\n为一行,存储到list中 + for (auto ch:ret) { + if ('\n'!= ch) { + tmpLine.append(ch); + } + else { + contentList.append(tmpLine); + tmpLine.clear(); + } + } + + //取第一个非root的用户 + int i= 0; + do { + if (contentList[i].contains("root", Qt::CaseInsensitive)) { + continue; + } + else { + getUserName = contentList[i].split(" "); + curUser = getUserName[0]; + break; + } + i++; + } while (istart("bash", QStringList() << "-c" << "cat /etc/passwd | grep " + curUser); execCmd->waitForFinished(); @@ -70,16 +108,31 @@ bool GetContent::checkDirPathExists(QString &dirPath) } } +int GetContent::getSubStringCount(QString &str) +{ + QString substr = "\x1b"; + QRegExp rx(substr); + int count = 0; + int pos = 0; + while ((pos = rx.indexIn(str, pos)) != -1) { + ++count; + pos += rx.matchedLength(); + } + return count; +} + void GetContent::removeColorString(QString &str) { int colorStringLeft = 0; int colorStringRight = 0; int pos = 0; int count = 0; + int substrCount = getSubStringCount(str); QString substr = "\x1b"; colorStringLeft = str.indexOf(substr, pos); - while (colorStringLeft != -1) { + + for (int i=0; istart("who", params); - execCmd->waitForFinished(); - curUser = execCmd->readAllStandardOutput(); - getUserName = curUser.split(" "); - curUser = getUserName[0]; - this->exportPath = "/home/" + curUser + "/桌面/"; - execCmd->close(); - // qDebug() << this->exportPath; + this->exportPath = GetContent::getHomePath() + "/桌面/"; } void Logtools::logtoolsMessagesBox() @@ -1676,7 +1664,7 @@ void Logtools::logtoolsSyslogSlots() QString crashDir = "/var/crash"; QString dstDir = this->exportPath + "/system-all-log"; GetContent checkDir; - QString messageBoxText = "系统日志将导出到桌面,确认导出吗?"; + QString messageBoxText = "系统日志将导出到" + this->exportPath + ",确认导出吗?"; QMessageBox *exportCupsLog = new QMessageBox; exportCupsLog->setWindowTitle(" "); @@ -1718,7 +1706,7 @@ void Logtools::logtoolsPrintRelatedSlots() QString srcDir = "/var/log/cups"; QString dstDir = this->exportPath + "/cups"; GetContent checkDir; - QString messageBoxText = "打印日志将导出到桌面,确认导出吗?"; + QString messageBoxText = "打印日志将导出到" + this->exportPath + ",确认导出吗?"; if (!checkDir.checkDirPathExists(srcDir)) { return ; @@ -1814,7 +1802,7 @@ void Logtools::on_pushButtonExportCur_clicked() QString currentDate = currentDateTime.toString("yyyy.MM.dd-hh:mm:ss"); exportFile = "log_" + currentDate; //format eg: log_2022.08.16-14:36:05 QString logFile = this->exportPath + exportFile; - QString messageBoxText = "当前日志将导出到桌面,确认导出吗?"; + QString messageBoxText = "当前日志将导出到" + this->exportPath + ",确认导出吗?"; QMessageBox *exportCurLog = new QMessageBox; exportCurLog->setWindowTitle(" "); @@ -1853,7 +1841,7 @@ void Logtools::on_pushButtonExportAll_clicked() QString folderNmae = "export"; QDir dir(QDir::tempPath()); GetContent checkDir; - QString messageBoxText = "系统日志将导出到桌面,确认导出吗?"; + QString messageBoxText = "系统日志将导出到" + this->exportPath + ",确认导出吗?"; QMessageBox *exportLog = new QMessageBox; exportLog->setWindowTitle(" "); exportLog->setText(messageBoxText); -- Gitee