Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the issue of missing 'apps' directory in command line export #187

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion application/assets/deepin-logger
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
para=$*
CMDPATTERN="^-"

# 添加当前路径作为命令行最后一个参数,用来解决身份验证提权后,程序获取不到终端当前运行路径的问题。
para+=" "
para+=$(pwd)

# 添加当前用户名,解决身份验证提权后,homePath为/root的问题。
para+=" "
para+=$USER

if [[ "$para" =~ $CMDPATTERN ]]; then
pkexec deepin-log-viewer ${para}
else
deepin-log-viewer -h
fi
fi
4 changes: 2 additions & 2 deletions application/logallexportthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void LogAllExportThread::run()
int tolProcess = nCount + 10;
int currentProcess = 1;
emit updateTolProcess(tolProcess);
QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp/";
QString tmpPath = Utils::getAppDataPath() + "/tmp/";
QDir dir(tmpPath);
//鍒犻櫎涓存椂鐩綍
dir.removeRecursively();
Expand Down Expand Up @@ -182,7 +182,7 @@ void LogAllExportThread::run()
QProcess procss;
procss.setWorkingDirectory(tmpPath);
QStringList arg = {"-c"};
arg.append(QString("zip -r tmp.zip ./*;mv tmp.zip '%1'").arg(m_outfile));
arg.append(QString("zip -r tmp.zip ./*;mv tmp.zip '%1';chmod 666 '%2'").arg(m_outfile).arg(m_outfile));
procss.start("/bin/bash", arg);
procss.waitForFinished(-1);
currentProcess += 9;
Expand Down
8 changes: 4 additions & 4 deletions application/logapplicationhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void LogApplicationHelper::initOtherLog()
for (QStringList iter : m_other_log_list_temp) {
QString path = iter.at(1);
if (path.startsWith("~/"))
path.replace(0, 1, QDir::homePath());
path.replace(0, 1, Utils::homePath);
m_other_log_list.append(QStringList() << iter.at(0) << path);
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ void LogApplicationHelper::initCustomLog()
for (QString iter : sList) {
QString path = iter;
if (path.startsWith("~/"))
path.replace(0, 1, QDir::homePath());
path.replace(0, 1, Utils::homePath);
//蹇界暐闈炴枃鏈枃浠跺拰涓嶅瓨鍦ㄧ殑鏂囦欢
if (!QFile::exists(path) || !Utils::isTextFileType(path)) {
continue;
Expand Down Expand Up @@ -189,7 +189,7 @@ void LogApplicationHelper::initCustomLog()
for (QString iter : sList2) {
QString path = iter;
if (path.startsWith("~/"))
path.replace(0, 1, QDir::homePath());
path.replace(0, 1, Utils::homePath);
//蹇界暐闈炴枃鏈枃浠跺拰涓嶅瓨鍦ㄧ殑鏂囦欢
if (!QFile::exists(path) || !Utils::isTextFileType(path)) {
continue;
Expand Down Expand Up @@ -304,7 +304,7 @@ void LogApplicationHelper::createDesktopFiles()
*/
void LogApplicationHelper::createLogFiles()
{
QString homePath = QDir::homePath();
QString homePath = Utils::homePath;
if (homePath.isEmpty()) {
return;
}
Expand Down
10 changes: 8 additions & 2 deletions application/logbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ LogBackend::LogBackend(QObject *parent) : QObject(parent)
getLogTypes();
}

void LogBackend::setCmdWorkDir(const QString &dir)
{
m_cmdWorkDir = dir;
}

void LogBackend::exportAllLogs(const QString &outDir)
{
PERF_PRINT_BEGIN("POINT-05", "export all logs");
Expand All @@ -53,7 +58,7 @@ void LogBackend::exportAllLogs(const QString &outDir)

QString outPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
if (!outDir.isEmpty())
outPath = outDir;
outPath = QDir::isRelativePath(outDir) ? (m_cmdWorkDir + "/" + outDir) : outDir;

// 鑻ユ寚瀹氱洰褰曚笉瀛樺湪锛屽厛鍒涘缓鐩綍
QDir odir(outPath);
Expand All @@ -62,8 +67,9 @@ void LogBackend::exportAllLogs(const QString &outDir)
m_newDir = odir.exists();
if (m_newDir)
outPath = odir.absolutePath();
} else
} else {
outPath = odir.absolutePath();
}

m_outPath = outPath;

Expand Down
4 changes: 4 additions & 0 deletions application/logbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class LogBackend : public QObject
~LogBackend();
explicit LogBackend(QObject *parent = nullptr);

// 璁剧疆鍛戒护琛屽綋鍓嶅伐浣滅洰褰�
void setCmdWorkDir(const QString &dir);

// 瀵煎嚭鍏ㄩ儴鏃ュ織鍒版寚瀹氱洰褰�
void exportAllLogs(const QString& outDir = "");

Expand All @@ -28,6 +31,7 @@ class LogBackend : public QObject
QStringList m_logTypes;
bool m_newDir {false};
QString m_outPath {""};
QString m_cmdWorkDir {""};
};

#endif // LOGBACKEND_H
2 changes: 1 addition & 1 deletion application/logexportthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3351,7 +3351,7 @@ bool LogExportThread::exportToXls(const QString &fileName, const QList<LOG_MSG_A

bool LogExportThread::exportToZip(const QString &fileName, const QList<LOG_MSG_COREDUMP> &jList)
{
QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp/";
QString tmpPath = Utils::getAppDataPath() + "/tmp/";
QDir dir(tmpPath);
//鍒犻櫎涓存椂鐩綍
dir.removeRecursively();
Expand Down
3 changes: 2 additions & 1 deletion application/loglistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DebugTimeManager.h"
#include "logapplicationhelper.h"
#include "dbusproxy/dldbushandler.h"
#include "utils.h"

#include <DDesktopServices>
#include <DDialog>
Expand Down Expand Up @@ -449,7 +450,7 @@ void LogListView::showRightMenu(const QPoint &pos, bool isUsePoint)
g_openForder->setEnabled(false);
}

QString dirPath = QDir::homePath();
QString dirPath = Utils::homePath;
QString _path_ = g_path; //get app path
QString path = "";

Expand Down
5 changes: 3 additions & 2 deletions application/logsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "logsettings.h"
#include "utils.h"

#include <QStandardPaths>
#include <QApplication>
Expand All @@ -27,9 +28,9 @@ LogSettings::LogSettings(QObject *parent)
m_configPath(""),
m_logDirPath("")
{
QDir infoPath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
QDir infoPath(Utils::getConfigPath());
if (!infoPath.exists()) {
infoPath.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
infoPath.mkpath(Utils::getConfigPath());
}

m_configPath = infoPath.filePath("wininfo-config.conf");
Expand Down
8 changes: 7 additions & 1 deletion application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ int main(int argc, char *argv[])
// 鍛戒护鍙傛暟澶т簬1锛岃繘琛屽懡浠よ澶勭悊
if (argc > 1) {
QCoreApplication a(argc, argv);
a.setOrganizationName("deepin");
a.setApplicationName("deepin-log-viewer");

QCommandLineOption exportOption(QStringList() << "e" << "export", DApplication::translate("main", "export logs"));

Expand All @@ -62,11 +64,15 @@ int main(int argc, char *argv[])
}

QString outDir = "";

// 鑻ユ寚瀹氭湁瀵煎嚭鐩綍锛屾寜鎸囧畾鐩綍瀵煎嚭锛屽惁鍒欐寜榛樿璺緞瀵煎嚭
if (!args.isEmpty())
outDir = args.first();

// 璁剧疆鍛戒护琛屽伐浣滅洰褰�
LogBackend::instance(&a)->setCmdWorkDir(argv[argc - 2]);
// 鏍规嵁褰撳墠鐢ㄦ埛鍚嶈幏鍙栨纭鐩綍璺緞
Utils::homePath = Utils::getHomePath(argv[argc - 1]);

// 鍏ㄩ儴瀵煎嚭鏃ュ織
LogBackend::instance(&a)->exportAllLogs(outDir);

Expand Down
4 changes: 2 additions & 2 deletions application/structdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QString>
#include <QDir>
#include <QMap>

#include "utils.h"
#define DPKG_TABLE_DATA "dpkgItemData"
#define XORG_TABLE_DATA "XorgItemData"
#define BOOT_TABLE_DATA "bootItemData"
Expand All @@ -27,7 +27,7 @@
#define BOOT_KLU_TREE_DATA "bootklu"
#define DPKG_TREE_DATA "/var/log/dpkg.log"
#define XORG_TREE_DATA "/var/log/Xorg.0.log"
#define KWIN_TREE_DATA QDir::homePath() + "/.kwin.log"
#define KWIN_TREE_DATA Utils::homePath + "/.kwin.log"
#define BOOT_TREE_DATA "/var/log/boot.log"
#define KERN_TREE_DATA "/var/log/kern.log"
#define APP_TREE_DATA "application"
Expand Down
37 changes: 36 additions & 1 deletion application/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ QHash<QString, QPixmap> Utils::m_imgCacheHash;
QHash<QString, QString> Utils::m_fontNameCache;
QMap<QString, QStringList> Utils::m_mapAuditType2EventType;
int Utils::specialComType = -1;
QString Utils::homePath = QDir::homePath();

Utils::Utils(QObject *parent)
: QObject(parent)
Expand All @@ -55,7 +56,15 @@ QString Utils::getQssContent(const QString &filePath)

QString Utils::getConfigPath()
{
QDir dir(QDir(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first())
QDir dir(QDir(Utils::homePath + "/.config")
.filePath(qApp->organizationName()));

return dir.filePath(qApp->applicationName());
}

QString Utils::getAppDataPath()
{
QDir dir(QDir(Utils::homePath + "/.local/share")
.filePath(qApp->organizationName()));

return dir.filePath(qApp->applicationName());
Expand Down Expand Up @@ -320,6 +329,13 @@ QString Utils::getUserNamebyUID(uint uid)
return pwd->pw_name;
}

QString Utils::getCurrentUserName()
{
// 鑾峰彇褰撳墠绯荤粺鐢ㄦ埛鍚�
struct passwd* pwd = getpwuid(getuid());
return pwd->pw_name;
}

bool Utils::isCoredumpctlExist()
{
bool isCoredumpctlExist = false;
Expand All @@ -333,3 +349,22 @@ bool Utils::isCoredumpctlExist()
}
return isCoredumpctlExist;
}

QString Utils::getHomePath(const QString &userName)
{
QString uName("");
if (!userName.isEmpty())
uName = userName;
else
uName = getCurrentUserName();


QProcess *unlock = new QProcess;
unlock->start("sh", QStringList() << "-c" << QString("cat /etc/passwd | grep %1").arg(uName));
unlock->waitForFinished();
auto output = unlock->readAllStandardOutput();
auto str = QString::fromUtf8(output);
str = str.mid(str.indexOf("::") + 2).split(":").first();
qInfo() << "userName: " << uName << "homePath:" << str;
return str;
}
4 changes: 4 additions & 0 deletions application/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Utils : public QObject

static QString getQssContent(const QString &filePath);
static QString getConfigPath();
static QString getAppDataPath();
static bool isFontMimeType(const QString &filePath);
static bool isTextFileType(const QString &filePath);
static QString suffixList();
Expand All @@ -53,12 +54,15 @@ class Utils : public QObject
static QString auditType(const QString& eventType);
static double convertToMB(quint64 cap, const int size = 1024);
static QString getUserNamebyUID(uint uid); //鏍规嵁uid鑾峰彇鐢ㄦ埛鍚�
static QString getCurrentUserName();
static bool isCoredumpctlExist(); // is coredumpctl installed
static QString getHomePath(const QString &userName = "");
/**
* @brief specialComType 鏄惁鏄壒娈婃満鍨嬶紝like huawei
* 鍙栧�兼湁3绉嶏紙-1,0,>0锛夛紝榛樿涓�-1锛堟湭鐭ワ級锛�0锛堜笉鏄壒娈婃満鍨嬶級,>0锛堢壒娈婃満鍨嬶級
*/
static int specialComType;
static QString homePath;
};

#endif
2 changes: 1 addition & 1 deletion tests/src/ut_displaycontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ TEST(DisplayContent_slot_appLogs_UT, DisplayContent_slot_appLogs_UT_001)
DisplayContent *p = new DisplayContent(nullptr);
EXPECT_NE(p, nullptr);
stub.set(ADDR(DisplayContent, generateAppFile), DisplayContent_slot_BtnSelected_UT_generateAppFile);
p->slot_appLogs(1, QDir::homePath() + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log");
p->slot_appLogs(1, Utils::homePath + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log");
p->deleteLater();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/ut_filtercontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ TEST_F(FilterContent_UT, slot_logCatelogueClicked_UT)
m_model.setData(m_model.index(7, 0), QString("bootklu"), Qt::UserRole + 66);
m_model.setData(m_model.index(8, 0), QString("/var/log/dnf.log"), Qt::UserRole + 66);
m_model.setData(m_model.index(9, 0), QString("dmesg"), Qt::UserRole + 66);
m_model.setData(m_model.index(10, 0), QString(QDir::homePath() + "/.kwin.log"), Qt::UserRole + 66);
m_model.setData(m_model.index(10, 0), QString(Utils::homePath + "/.kwin.log"), Qt::UserRole + 66);
for (int i = 0; i < m_model.rowCount(); i++) {
QModelIndex modelindex = m_model.index(i, 0, QModelIndex());
m_filter->slot_logCatelogueClicked(modelindex);
Expand Down
2 changes: 1 addition & 1 deletion tests/ut_logviewerplugin/ut_logViewerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_F(LogViewerPlugin_UT, generateAppFile_UT001)
stub.set(ADDR(QThread, start), QThread_start);

LogViewerPlugin *p = new LogViewerPlugin;
p->generateAppFile(QDir::homePath() + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log", ALL, INF, "");
p->generateAppFile(Utils::homePath + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log", ALL, INF, "");
connect(p, &LogViewerPlugin::sigAppData, this, [=](int index, QList<LOG_MSG_APPLICATOIN> iList) {
qDebug() << "index:" << index << endl;
qDebug() << "appDatas:" << endl;
Expand Down
Loading