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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
// SPDX-FileCopyrightText: 2019 - 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "logsegementexportthread.h"
#include "utils.h"
#include "xlsxwriter.h"
#include "WordProcessingMerger.h"
#include "WordProcessingCompiler.h"
#include "dbusproxy/dldbushandler.h"
#include <DApplication>
#include <QDebug>
#include <QFile>
// #include <QTextCodec>
#include <QTextStream>
#include <QTextDocument>
#include <QTextDocumentWriter>
#include <QElapsedTimer>
#include <QStandardPaths>
#include <QProcess>
#include <QLoggingCategory>
#include <malloc.h>
DWIDGET_USE_NAMESPACE
#ifdef QT_DEBUG
Q_LOGGING_CATEGORY(logSegementExport, "org.deepin.log.viewer.segement.export.work")
#else
Q_LOGGING_CATEGORY(logSegementExport, "org.deepin.log.viewer.segement.export.work", QtInfoMsg)
#endif
/**
* @brief LogSegementExportThread::LogSegementExportThread 导出日志线程类构造函数
* @param parent 父对象
*/
LogSegementExportThread::LogSegementExportThread(QObject *parent)
: QObject(parent),
QRunnable()
{
setAutoDelete(true);
}
/**
* @brief LogSegementExportThread::~LogSegementExportThread 析构函数
*/
LogSegementExportThread::~LogSegementExportThread()
{
qCDebug(logSegementExport) << "LogSegementExportThread destoryed.";
//释放空闲内存
malloc_trim(0);
}
void LogSegementExportThread::setParameter(const QString &fileName, const QList<QString> &jList, const QStringList &lables, LOG_FLAG flag)
{
QMutexLocker locker(&mutex);
m_fileName = fileName;
m_logDataList = jList;
m_flag = flag;
m_labels = lables;
m_bForceStop = false;
if (m_fileName.endsWith(".txt")) {
m_runMode = Txt;
} else if (fileName.endsWith(".html")) {
m_runMode = Html;
} else if (fileName.endsWith(".doc")) {
m_runMode = Doc;
if (!m_pDocMerger)
initDoc();
} else if (fileName.endsWith(".xls")) {
m_runMode = Xls;
if (!m_pWorkbook)
initXls();
}
condition.wakeOne();
}
void LogSegementExportThread::initDoc()
{
QString tempdir = getDocTemplatePath();
if (tempdir.isEmpty())
return;
m_pDocMerger = &DocxFactory:: WordProcessingMerger::getInstance();
m_pDocMerger->load(tempdir.toStdString());
//往表头中添加表头描述,表头为第一行,数据则在下面
for (int col = 0; col < m_labels.count(); ++col) {
m_pDocMerger->setClipboardValue("tableRow", QString("column%1").arg(col + 1).toStdString(), m_labels.at(col).toStdString());
}
m_pDocMerger->paste("tableRow");
}
void LogSegementExportThread::initXls()
{
m_currentXlsRow = 0;
m_pWorkbook = workbook_new(m_fileName.toStdString().c_str());
m_pWorksheet = workbook_add_worksheet(m_pWorkbook, nullptr);
lxw_format *format = workbook_add_format(m_pWorkbook);
format_set_bold(format);
for (int col = 0; col < m_labels.count(); ++col) {
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col), m_labels.at(col).toStdString().c_str(), format);
}
++m_currentXlsRow;
}
QString LogSegementExportThread::getDocTemplatePath()
{
QString tempdir("");
if (m_flag == KERN) {
tempdir = "/usr/share/deepin-log-viewer/DocxTemplate/4column.dfw";
} else if (m_flag == Kwin) {
tempdir = "/usr/share/deepin-log-viewer/DocxTemplate/1column.dfw";
} else {
qCWarning(logSegementExport) << "exportToDoc type is Wrong!";
return "";
}
if (!QFile(tempdir).exists()) {
qCWarning(logSegementExport) << "export docx template is not exisits";
return "";
}
return tempdir;
}
/**
* @brief LogSegementExportThread::isProcessing 返回当前线程获取数据逻辑启动停止控制的变量
* @return 当前线程获取数据逻辑启动停止控制的变量
*/
bool LogSegementExportThread::isProcessing()
{
return !m_bForceStop;
}
/**
* @brief LogSegementExportThread::stopImmediately 强制停止前线程获取数据逻辑
*/
void LogSegementExportThread::stopImmediately()
{
QMutexLocker locker(&mutex);
m_bForceStop = true;
condition.wakeOne();
}
/**
* @brief LogSegementExportThread::stop 停止前线程获取数据逻辑
*/
void LogSegementExportThread::stop()
{
QMutexLocker locker(&mutex);
m_bStop = true;
condition.wakeOne();
}
/**
* @brief LogSegementExportThread::run 线程的run函数,通过配置类型执行相应的导出逻辑
*/
void LogSegementExportThread::run()
{
qCDebug(logSegementExport) << "threadrun";
QMutexLocker locker(&mutex);
while(!m_bForceStop && !m_bStop) {
if (m_logDataList.isEmpty()) {
condition.wait(&mutex);
} else {
try {
switch (m_runMode) {
case Txt: {
exportTxt();
break;
}
case Html:{
exportHtml();
break;
}
case Doc: {
exportToDoc();
break;
}
case Xls: {
exportToXls();
break;
}
default:
break;
}
emit sigProgress(++m_nCurProcess, m_nTotalProcess);
m_logDataList.clear();
} catch (const QString &ErrorStr) {
// 捕获到异常,导出失败,发出失败信号
qCWarning(logSegementExport) << "Export Stop" << ErrorStr;
emit sigResult(false);
if (ErrorStr != m_forceStopStr) {
emit sigError(QString("export error: %1").arg(ErrorStr));
}
}
}
}
if (!m_bForceStop) {
// 保存数据
switch (m_runMode) {
case Doc:
saveDoc();
break;
case Xls:
closeXls();
break;
default:
break;
}
// 进度100%
emit sigProgress(m_nTotalProcess, m_nTotalProcess);
// 延时200ms再发送导出成功信号,关闭导出进度框,让100%的进度有时间显示
Utils::sleep(200);
}
emit sigResult(!m_bForceStop);
if (m_bForceStop) {
Utils::checkAndDeleteDir(m_fileName);
}
m_bForceStop = false;
}
bool LogSegementExportThread::exportTxt()
{
//判断文件路径是否存在,不存在就返回错误
QFile fi(m_fileName);
if (!fi.open(m_bAppendWrite ? (QIODevice::Append | QIODevice::WriteOnly) : QIODevice::WriteOnly)) {
emit sigError(m_openErroStr);
return false;
}
QTextStream out(&fi);
for (int i = 0; i < m_logDataList.count(); i++) {
//导出逻辑启动停止控制,外部把m_forceStopStr置true时停止运行,抛出异常处理
if (m_bForceStop) {
fi.close();
throw QString(m_forceStopStr);
}
int col = 0;
LOG_MSG_BASE jMsg;
jMsg.fromJson(m_logDataList[i]);
if (m_flag == KERN) {
out << m_labels.value(col++, "") << ":" << jMsg.dateTime << " ";
out << m_labels.value(col++, "") << ":" << jMsg.hostName << " ";
out << m_labels.value(col++, "") << ":" << jMsg.daemonName << " ";
out << m_labels.value(col++, "") << ":" << jMsg.msg << " ";
out << "\n";
} else if (m_flag == Kwin) {
out << m_labels.value(col++, "") << ":" << jMsg.msg << " ";
out << "\n";
}
}
//设置文件编码为utf8
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
out.setCodec(QTextCodec::codecForName("utf-8"));
#else
out.setEncoding(QStringConverter::Utf8);
#endif
fi.close();
return true;
}
bool LogSegementExportThread::exportHtml()
{
QFile html(m_fileName);
//判断文件路径是否存在,不存在就返回错误
if (!html.open(m_bAppendWrite ? (QIODevice::Append | QIODevice::WriteOnly) : QIODevice::WriteOnly)) {
emit sigError(m_openErroStr);
return false;
}
//写网页头
html.write("<!DOCTYPE html>\n");
html.write("<html>\n");
html.write("<body>\n");
//写入表格标签
html.write("<table border=\"1\">\n");
//写入表头
html.write("<tr>");
for (int i = 0; i < m_labels.count(); ++i) {
QString labelInfo = QString("<td>%1</td>").arg(m_labels.value(i));
html.write(labelInfo.toUtf8().data());
}
//写入内容
//根据字段拼出每行的网页内容
html.write("</tr>");
for (int row = 0; row < m_logDataList.count(); ++row) {
if (m_bForceStop) {
html.close();
throw QString(m_forceStopStr);
}
LOG_MSG_BASE jMsg;
jMsg.fromJson(m_logDataList.at(row));
htmlEscapeCovert(jMsg.msg);
html.write("<tr>");
if (m_flag == KERN) {
QString info = QString("<td>%1</td>").arg(jMsg.dateTime);
html.write(info.toUtf8().data());
info = QString("<td>%1</td>").arg(jMsg.hostName);
html.write(info.toUtf8().data());
info = QString("<td>%1</td>").arg(jMsg.daemonName);
html.write(info.toUtf8().data());
info = QString("<td>%1</td>").arg(jMsg.msg);
html.write(info.toUtf8().data());
} else if (m_flag == Kwin) {
QString info = QString("<td>%1</td>").arg(jMsg.msg);
html.write(info.toUtf8().data());
}
html.write("</tr>");
}
//写入结尾
html.write("</table>\n");
html.write("</body>\n");
html.write("</html>\n");
html.close();
return true;
}
bool LogSegementExportThread::exportToDoc()
{
if (!m_pDocMerger)
return false;
for (int row = 0; row < m_logDataList.count(); ++row) {
//导出逻辑启动停止控制,外部把m_forceStopStr置true时停止运行,抛出异常处理
if (m_bForceStop) {
throw QString(m_forceStopStr);
}
LOG_MSG_BASE message;
message.fromJson(m_logDataList.at(row));
//把数据填入表格单元格中
if (m_flag == KERN) {
m_pDocMerger->setClipboardValue("tableRow", QString("column1").toStdString(), message.dateTime.toStdString());
m_pDocMerger->setClipboardValue("tableRow", QString("column2").toStdString(), message.hostName.toStdString());
m_pDocMerger->setClipboardValue("tableRow", QString("column3").toStdString(), message.daemonName.toStdString());
m_pDocMerger->setClipboardValue("tableRow", QString("column4").toStdString(), message.msg.toStdString());
} else if (m_flag == Kwin) {
m_pDocMerger->setClipboardValue("tableRow", QString("column1").toStdString(), message.msg.toStdString());
}
m_pDocMerger->paste("tableRow");
}
return true;
}
bool LogSegementExportThread::exportToXls()
{
if (!m_pWorksheet)
return false;
for (int row = 0; row < m_logDataList.count() ; ++row) {
if (m_bForceStop) {
throw QString(m_forceStopStr);
}
LOG_MSG_BASE message;
message.fromJson(m_logDataList.at(row));
int col = 0;
if (m_flag == KERN) {
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col++), message.dateTime.toStdString().c_str(), nullptr);
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col++), message.hostName.toStdString().c_str(), nullptr);
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col++), message.daemonName.toStdString().c_str(), nullptr);
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col++), message.msg.toStdString().c_str(), nullptr);
} else if (m_flag == Kwin) {
worksheet_write_string(m_pWorksheet, static_cast<lxw_row_t>(m_currentXlsRow), static_cast<lxw_col_t>(col++), message.msg.toStdString().c_str(), nullptr);
}
++m_currentXlsRow;
}
return true;
}
void LogSegementExportThread::htmlEscapeCovert(QString &htmlMsg)
{
//无法对所有转义字符进行转换,对常用转义字符转换
htmlMsg.replace("<", "<", Qt::CaseInsensitive);
htmlMsg.replace(">", ">", Qt::CaseInsensitive);
htmlMsg.replace("?", "¡", Qt::CaseInsensitive);
htmlMsg.replace("¥", "¥", Qt::CaseInsensitive);
htmlMsg.replace("|", "¦", Qt::CaseInsensitive);
}
void LogSegementExportThread::saveDoc()
{
//保存,把拼好的xml写入文件中
QString fileNamex = m_fileName + "x";
QFile rsNameFile(m_fileName) ;
if (rsNameFile.exists()) {
rsNameFile.remove();
}
m_pDocMerger->save(fileNamex.toStdString());
QFile(fileNamex).rename(m_fileName);
}
void LogSegementExportThread::closeXls()
{
workbook_close(m_pWorkbook);
malloc_trim(0);
}
|