File: logallexportthread.cpp

package info (click to toggle)
deepin-log-viewer 5.9.7%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,656 kB
  • sloc: cpp: 51,541; ansic: 1,732; sh: 51; xml: 25; makefile: 13
file content (138 lines) | stat: -rw-r--r-- 5,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
*
* Author:     liuyanga <liuyanga@uniontech.com>
*
* Maintainer: liuyanga <liuyanga@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "logallexportthread.h"
#include "dbusproxy/dldbushandler.h"
#include "logapplicationhelper.h"
#include "utils.h"
LogAllExportThread::LogAllExportThread(const QStringList &types, const QString &outfile, QObject *parent)
    : QObject(parent)
    , m_types(types)
    , m_outfile(outfile)
{
}

void LogAllExportThread::run()
{
    //判断权限
    QFileInfo info(m_outfile);
    if (!QFileInfo(info.path()).isWritable()) {
        emit exportFinsh(false);
        return;
    }

    QStringList files;
    QStringList commands;

    if (files.isEmpty() && commands.isEmpty()) {
        //获取所有文件
        for (auto &it : m_types) {
            if (it.contains(JOUR_TREE_DATA, Qt::CaseInsensitive)) {
                commands.push_back("journalctl_system");
            } else if (it.contains(BOOT_KLU_TREE_DATA, Qt::CaseInsensitive)) {
                commands.push_back("journalctl_boot");
            } else if (it.contains(DMESG_TREE_DATA, Qt::CaseInsensitive)) {
                commands.push_back("dmesg");
            } else if (it.contains(LAST_TREE_DATA, Qt::CaseInsensitive)) {
                commands.push_back("last");
            } else if (it.contains(DPKG_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(DLDBusHandler::instance(nullptr)->getFileInfo("dpkg", false));
            } else if (it.contains(KERN_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(DLDBusHandler::instance(nullptr)->getFileInfo("kern", false));
            } else if (it.contains(XORG_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(DLDBusHandler::instance(nullptr)->getFileInfo("Xorg", false));
            } else if (it.contains(DNF_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(DLDBusHandler::instance(nullptr)->getFileInfo("dnf", false));
            } else if (it.contains(BOOT_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(DLDBusHandler::instance(nullptr)->getFileInfo("boot", false));
            } else if (it.contains(KWIN_TREE_DATA, Qt::CaseInsensitive)) {
                files.append(KWIN_TREE_DATA);
            } else if (it.contains(APP_TREE_DATA, Qt::CaseInsensitive)) {
                QMap<QString, QString> appData = LogApplicationHelper::instance()->getMap();
                for (auto &it : appData.toStdMap()) {
                    files.append(it.second);
                }
            }
            //取消导出直接返回
            if (m_cancel) {
                files.clear();
                commands.clear();
                emit exportFinsh(false);
                return;
            }
        }
    }

    if (files.isEmpty() && commands.isEmpty()) {
        emit exportFinsh(false);
        return;
    }

    //删除原文件
    if (info.exists() && !QFile::remove(m_outfile)) {
        emit exportFinsh(false);
        return;
    }

    int tolProcess = files.size() + commands.size() + 10;
    int currentProcess = 1;
    emit updateTolProcess(tolProcess);
    QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp/";
    QDir dir(tmpPath);
    //删除临时目录
    dir.removeRecursively();
    //创建临时目录
    Utils::mkMutiDir(tmpPath);
    //复制文件
    for (auto &it : files) {
        DLDBusHandler::instance(this)->exportLog(tmpPath, it, true);
        emit updatecurrentProcess(currentProcess++);
        if (m_cancel) {
            break;
        }
    }
    if (false == m_cancel) {
        for (auto &it : commands) {
            DLDBusHandler::instance(this)->exportLog(tmpPath, it, false);
            emit updatecurrentProcess(currentProcess++);
            if (m_cancel) {
                break;
            }
        }
    }
    if (false == m_cancel) {
        //打包日志文件
        QProcess procss;
        procss.setWorkingDirectory(tmpPath);
        QStringList arg = {"-c"};
        arg.append(QString("zip tmp.zip *.*;mv tmp.zip '%1'").arg(m_outfile));
        procss.start("/bin/bash", arg);
        procss.waitForFinished(-1);
        currentProcess += 9;
        emit updatecurrentProcess(currentProcess);
    }
    //删除临时目录
    dir.removeRecursively();
    //取消导出删除输出文件
    if (m_cancel) {
        QFile::remove(m_outfile);
    }
    emit exportFinsh(!m_cancel && QFileInfo(m_outfile).exists());
}