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 export specify app log failed problem #210

Merged
merged 1 commit into from
Oct 26, 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
8 changes: 8 additions & 0 deletions application/logapplicationhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ AppLogConfig LogApplicationHelper::appLogConfig(const QString &app)
return AppLogConfig();
}

bool LogApplicationHelper::isValidAppName(const QString &appName)
{
if (m_en_log_map.find(appName) != m_en_log_map.end())
return true;

return false;
}

//从应用包名转换为应用显示文本
QString LogApplicationHelper::transName(const QString &str)
{
Expand Down
3 changes: 3 additions & 0 deletions application/logapplicationhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class LogApplicationHelper : public QObject

AppLogConfig appLogConfig(const QString& app);

// 验证是否为有效的应用名
bool isValidAppName(const QString& appName);

private:
explicit LogApplicationHelper(QObject *parent = nullptr);

Expand Down
149 changes: 98 additions & 51 deletions application/logbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ void LogBackend::exportAllLogs(const QString &outDir)
Utils::resetToNormalAuth(m_outPath);
}

void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)
int LogBackend::exportTypeLogs(const QString &outDir, const QString &type)
{
// 输出目录有效性验证
if(!getOutDirPath(outDir))
return;
return -1;

// 日志种类有效性验证
QString error;
m_flag = type2Flag(type, error);
if (NONE == m_flag) {
qCInfo(logBackend) << error;
return;
qCWarning(logBackend) << error;
return -1;
}

QString categoryOutPath = QString("%1/%2/").arg(m_outPath).arg(type);

qCInfo(logBackend) << "exporting ... type:" << type;
bool bSuccess = true;
switch (m_flag) {
case JOURNAL: {
resetCategoryOutputPath(categoryOutPath);
Expand All @@ -139,7 +141,8 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)
for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else if (logPaths.size() == 0) {
qCInfo(logBackend) << "/var/log has not kern.log";
qCWarning(logBackend) << "/var/log has not kern.log";
bSuccess = false;
}
}
break;
Expand All @@ -157,7 +160,8 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)
for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else {
qCInfo(logBackend) << "/var/log has not boot.log";
qCWarning(logBackend) << "/var/log has not boot.log";
bSuccess = false;
}
}
break;
Expand All @@ -168,8 +172,10 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)

for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else
qCInfo(logBackend) << "/var/log has not dpkg.log";
} else {
qCWarning(logBackend) << "/var/log has not dpkg.log";
bSuccess = false;
}
}
break;
case Dnf: {
Expand All @@ -179,8 +185,10 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)

for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else
qCInfo(logBackend) << "/var/log has not dnf.log";
} else {
qCWarning(logBackend) << "/var/log has not dnf.log";
bSuccess = false;
}
}
break;
case Kwin: {
Expand All @@ -196,8 +204,10 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)

for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else
qCInfo(logBackend) << "/var/log has not Xorg.log";
} else {
qCWarning(logBackend) << "/var/log has not Xorg.log";
bSuccess = false;
}
}
break;
case APP: {
Expand Down Expand Up @@ -241,14 +251,18 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)

for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else
qCInfo(logBackend) << "/var/log has no coredump logs";
} else {
qCWarning(logBackend) << "/var/log has no coredump logs";
bSuccess = false;
}
}
break;
case Normal: {
QFile file("/var/log/wtmp");
if (!file.exists())
qCInfo(logBackend) << "/var/log has no boot shutdown event log";
if (!file.exists()) {
qCWarning(logBackend) << "/var/log has no boot shutdown event log";
bSuccess = false;
}

resetCategoryOutputPath(categoryOutPath);

Expand Down Expand Up @@ -290,7 +304,8 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
}
} else {
qCInfo(logBackend) << "no custom logs";
qCWarning(logBackend) << "no custom logs";
bSuccess = false;
}
}
break;
Expand All @@ -301,15 +316,24 @@ void LogBackend::exportTypeLogs(const QString &outDir, const QString &type)

for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else
qCInfo(logBackend) << "/var/log has no audit logs";
} else {
qCWarning(logBackend) << "/var/log has no audit logs";
bSuccess = false;
}
}
break;
default:
break;
}

Utils::resetToNormalAuth(categoryOutPath);

if (bSuccess)
qCInfo(logBackend) << QString("export success.");
else
qCInfo(logBackend) << QString("export failed.");

return bSuccess ? 0 : -1;
}

bool LogBackend::LogBackend::exportTypeLogsByCondition(const QString &outDir, const QString &type, const QString &period, const QString &condition, const QString &keyword)
Expand All @@ -321,10 +345,12 @@ bool LogBackend::LogBackend::exportTypeLogsByCondition(const QString &outDir, co
QString error;
m_flag = type2Flag(type, error);
if (NONE == m_flag) {
qCInfo(logBackend) << error;
qCWarning(logBackend) << error;
return false;
}

qCInfo(logBackend) << "exporting ... type:" << type;

m_currentSearchStr = keyword;

// 解析数据
Expand All @@ -337,29 +363,29 @@ bool LogBackend::LogBackend::exportTypeLogsByCondition(const QString &outDir, co
return true;
}

void LogBackend::exportAppLogs(const QString &outDir, const QString &appName)
int LogBackend::exportAppLogs(const QString &outDir, const QString &appName)
{
if(!getOutDirPath(outDir))
return;
return -1;

if (appName.isEmpty())
return;
return -1;

// 先查找是否有该应用相关日志
QString logPath;
QMap<QString, QString> appData = LogApplicationHelper::instance()->getMap();
for (auto &it2 : appData.toStdMap()) {
if (it2.second.contains(appName)) {
logPath = it2.second;
break;
}
if (!LogApplicationHelper::instance()->isValidAppName(appName)) {
qCWarning(logBackend) << QString("unknown app:%1 is invalid.").arg(appName);
return -1;
}

QString logPath = getApplogPath(appName);
if (logPath.isEmpty()) {
qCInfo(logBackend) << QString("unknown app:%1 is invalid.").arg(appName);
return;
qCWarning(logBackend) << QString("app:%1 log path is null.").arg(appName);
return -1;
}

qCInfo(logBackend) << QString("exporting %1 logs...").arg(appName);

bool bSuccess = true;
AppLogConfig appLogConfig = LogApplicationHelper::instance()->appLogConfig(appName);
// 确定解析方式
QString parseType = "";
Expand All @@ -368,7 +394,6 @@ void LogBackend::exportAppLogs(const QString &outDir, const QString &appName)
else if (appLogConfig.isValid() && appLogConfig.logType == "journal")
parseType = "journal";


QString categoryOutPath = QString("%1/%2").arg(m_outPath).arg(appName);
if (parseType == "file") {
QStringList logPaths = DLDBusHandler::instance(nullptr)->getOtherFileInfo(logPath);
Expand All @@ -380,7 +405,8 @@ void LogBackend::exportAppLogs(const QString &outDir, const QString &appName)
for (auto &file: logPaths)
DLDBusHandler::instance()->exportLog(categoryOutPath, file, true);
} else {
qCInfo(logBackend) << QString("app:%1 not found log files.").arg(appName);
qCWarning(logBackend) << QString("app:%1 not found log files.").arg(appName);
bSuccess = false;
}
} else if (parseType == "journal") {
resetCategoryOutputPath(categoryOutPath);
Expand All @@ -389,6 +415,13 @@ void LogBackend::exportAppLogs(const QString &outDir, const QString &appName)
}

Utils::resetToNormalAuth(categoryOutPath);

if (bSuccess)
qCInfo(logBackend) << QString("export success.");
else
qCInfo(logBackend) << QString("export failed.");

return bSuccess ? 0 : -1;
}

bool LogBackend::exportAppLogsByCondition(const QString &outDir, const QString &appName, const QString &period, const QString &level, const QString &keyword)
Expand All @@ -409,22 +442,19 @@ bool LogBackend::exportAppLogsByCondition(const QString &outDir, const QString &
}
}

qCInfo(logBackend) << "period:" << period << "level:" << level << "keyword:" << keyword;
qCInfo(logBackend) << "appName:" << appName << "period:" << period << "level:" << level << "keyword:" << keyword;

TIME_RANGE timeRange = getTimeRange(periodId);

// 先查找是否有该应用相关日志
QString logPath;
QMap<QString, QString> appData = LogApplicationHelper::instance()->getMap();
for (auto &it2 : appData.toStdMap()) {
if (it2.second.contains(appName)) {
logPath = it2.second;
break;
}
if (!LogApplicationHelper::instance()->isValidAppName(appName)) {
qCWarning(logBackend) << QString("unknown app:%1 is invalid.").arg(appName);
return false;
}

QString logPath = getApplogPath(appName);
if (logPath.isEmpty()) {
qCInfo(logBackend) << QString("unknown app:%1 is invalid.").arg(appName);
qCWarning(logBackend) << QString("app:%1 log path is null.").arg(appName);
return false;
}

Expand Down Expand Up @@ -777,15 +807,16 @@ void LogBackend::onExportProgress(int nCur, int nTotal)

void LogBackend::onExportResult(bool isSuccess)
{
Utils::resetToNormalAuth(m_outPath);

if (isSuccess) {
qCInfo(logBackend) << "export success.";
qApp->exit(0);

} else {
qCWarning(logBackend) << "export failed.";
qApp->exit(-1);
}

Utils::resetToNormalAuth(m_outPath);

qApp->exit(-1);
}

QList<LOG_MSG_BOOT> LogBackend::filterBoot(BOOT_FILTERS ibootFilter, const QList<LOG_MSG_BOOT> &iList)
Expand Down Expand Up @@ -1031,7 +1062,7 @@ bool LogBackend::parseData(const LOG_FLAG &flag, const QString &period, const QS
}
}

qCInfo(logBackend) << "period:" << period << "condition:" << condition << "keyword:" << m_currentSearchStr;
qCInfo(logBackend) << "parsing ..." << "period:" << period << "condition:" << condition << "keyword:" << m_currentSearchStr;

TIME_RANGE timeRange = getTimeRange(periodId);
int lId = level2Id(condition);
Expand Down Expand Up @@ -1410,10 +1441,9 @@ bool LogBackend::getOutDirPath(const QString &path)
m_outPath = tmpPath;
qCInfo(logBackend) << "outPath:" << m_outPath;
return true;
}
else {
} else {
m_outPath = "";
qCInfo(logBackend) << QString("outpath:%1 is not exist.").arg(path);
qCWarning(logBackend) << QString("outpath:%1 is not exist.").arg(path);
}

return false;
Expand Down Expand Up @@ -1627,3 +1657,20 @@ TIME_RANGE LogBackend::getTimeRange(const BUTTONID &periodId)

return tr;
}

QString LogBackend::getApplogPath(const QString &appName)
{
QString logPath;
QMap<QString, QString> appData = LogApplicationHelper::instance()->getMap();
for (auto &it2 : appData.toStdMap()) {
if (it2.second.contains(appName)) {
logPath = it2.second;
break;
}
}

if (logPath.isEmpty())
logPath = LogApplicationHelper::instance()->getPathByAppId(appName);

return logPath;
}
5 changes: 3 additions & 2 deletions application/logbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class LogBackend : public QObject
void exportAllLogs(const QString &outDir = "");

// 按类型导出日志
void exportTypeLogs(const QString &outDir, const QString &type = "");
int exportTypeLogs(const QString &outDir, const QString &type = "");

// 按条件导出日志
// condition 可能为级别、事件类型、状态、审计类型等条件
bool exportTypeLogsByCondition(const QString &outDir, const QString &type, const QString &period, const QString &condition = "", const QString &keyword = "");

// 按应用导出日志
void exportAppLogs(const QString &outDir, const QString &appName = "");
int exportAppLogs(const QString &outDir, const QString &appName = "");

// 按条件导出应用日志
bool exportAppLogsByCondition(const QString &outDir, const QString& appName, const QString &period, const QString &level = "", const QString &keyword = "");
Expand Down Expand Up @@ -108,6 +108,7 @@ private slots:
int normal2eventType(const QString &eventType);
int audit2eventType(const QString &eventType);
TIME_RANGE getTimeRange(const BUTTONID& periodId);
QString getApplogPath(const QString &appName);

private:
QStringList m_logTypes;
Expand Down
Loading