1 // This file is part of Qt Bitcoin Trader
2 // https://github.com/JulyIGHOR/QtBitcoinTrader
3 // Copyright (C) 2013-2021 July Ighor <julyighor@gmail.com>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // In addition, as a special exception, the copyright holders give
11 // permission to link the code of portions of this program with the
12 // OpenSSL library under certain conditions as described in each
13 // individual source file, and distribute linked combinations including
14 // the two.
15 //
16 // You must obey the GNU General Public License in all respects for all
17 // of the code used other than OpenSSL. If you modify file(s) with this
18 // exception, you may extend this exception to your version of the
19 // file(s), but you are not obligated to do so. If you do not wish to do
20 // so, delete this exception statement from your version. If you delete
21 // this exception statement from all source files in the program, then
22 // also delete it here.
23 //
24 // This program is distributed in the hope that it will be useful,
25 // but WITHOUT ANY WARRANTY; without even the implied warranty of
26 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 // GNU General Public License for more details.
28 //
29 // You should have received a copy of the GNU General Public License
30 // along with this program. If not, see <http://www.gnu.org/licenses/>.
31
32 #include "debugviewer.h"
33 #include <QScrollBar>
34 #include "main.h"
35 #include <QFileDialog>
36 #include <QMessageBox>
37 #include <QSysInfo>
38 #include "timesync.h"
39
DebugViewer()40 DebugViewer::DebugViewer()
41 : QWidget()
42 {
43 savingFile = false;
44 ui.setupUi(this);
45 ui.checkEnabled->setChecked(true);
46
47 setWindowFlags(Qt::Window);
48 setAttribute(Qt::WA_DeleteOnClose, true);
49
50 if (baseValues.logThread_)
51 {
52 delete baseValues.logThread_;
53 baseValues.logThread_ = nullptr;
54 }
55
56 logThread = new LogThread(false);
57 connect(logThread, &LogThread::sendLogSignal, this, &DebugViewer::sendLogSlot, Qt::QueuedConnection);
58 debugLevel = 2;
59 show();
60 }
61
~DebugViewer()62 DebugViewer::~DebugViewer()
63 {
64 debugLevel = 0;
65
66 if (logThread)
67 {
68 delete baseValues.logThread_;
69 baseValues.logThread_ = nullptr;
70 }
71 }
72
on_buttonSaveAs_clicked()73 void DebugViewer::on_buttonSaveAs_clicked()
74 {
75 savingFile = true;
76 QString fileName = QFileDialog::getSaveFileName(this, "Save Debug Information",
77 QDateTime::fromTime_t(TimeSync::getTimeT()).toUTC().toString("yyyy-MM-dd HH.mm.ss") + ".log", "Log file (*.log)");
78
79 if (fileName.isEmpty())
80 {
81 savingFile = false;
82 return;
83 }
84
85 QFile writeLog(fileName);
86
87 if (writeLog.open(QIODevice::WriteOnly))
88 {
89 writeLog.write("Qt Bitcoin Trader " + baseValues.appVerStr + "\r\n");
90
91 QByteArray osLine;
92 #ifdef Q_OS_WIN
93 osLine = "OS: Windows " + QByteArray::number(QSysInfo::windowsVersion()) + "\r\n";
94 #endif
95
96 #ifdef Q_OS_MAC
97 osLine = "OS: Mac OS " + QByteArray::number(QSysInfo::MacintoshVersion) + "\r\n";
98 #endif
99
100 if (osLine.isEmpty())
101 osLine = "OS: Linux\r\n";
102
103 writeLog.write(osLine);
104 writeLog.write(ui.debugText->toPlainText().toLatin1());
105 writeLog.close();
106 }
107 else
108 QMessageBox::critical(this, windowTitle(), "Cannot save log file");
109
110 savingFile = false;
111 }
112
sendLogSlot(QByteArray text)113 void DebugViewer::sendLogSlot(QByteArray text)
114 {
115 QStringList filterData(QString(text).split("\r\n"));
116
117 for (int n = 0; n < filterData.size(); n++)
118 if (filterData.at(n).startsWith("Cookie", Qt::CaseInsensitive))
119 filterData[n] = "Cookie: THERE_WAS_A_COOKIE";
120
121 if (!savingFile && ui.checkEnabled->isChecked())
122 ui.debugText->appendPlainText(filterData.join("\n"));
123 }
124
on_radioDebug_toggled(bool debugEnabled)125 void DebugViewer::on_radioDebug_toggled(bool debugEnabled)
126 {
127 if (debugEnabled)
128 debugLevel = 1;
129 else
130 debugLevel = 2;
131
132 ui.debugText->setPlainText("");
133 }
134