File: loggingwindow.cpp

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (252 lines) | stat: -rw-r--r-- 7,613 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
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
#include "loggingwindow.h"
#include "ui_loggingwindow.h"

loggingWindow::loggingWindow(QString logFilename, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loggingWindow),
    logFilename(logFilename)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Log"));
    ui->logTextDisplay->setReadOnly(true);
    ui->userAnnotationText->setFocus();
    ui->annotateBtn->setDefault(true);
    ui->logTextDisplay->setFocusPolicy(Qt::NoFocus);
    ui->annotateBtn->setFocusPolicy(Qt::NoFocus);
    
    QDir d = QFileInfo(logFilename).absoluteDir();
    logDirectory = d.absolutePath();

    QFont font = QFont(QFontDatabase::systemFont(QFontDatabase::FixedFont).family());
    font.setStyleHint(QFont::TypeWriter);
    ui->logTextDisplay->setFont(font);
    ui->userAnnotationText->setFont(font);

    clipboard = QApplication::clipboard();
    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(connected()), this, SLOT(connectedToHost()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnectedFromHost()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(handleDataFromLoggingHost()));
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(handleLoggingHostError(QAbstractSocket::SocketError)));
#else
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleLoggingHostError(QAbstractSocket::SocketError)));
#endif
    vertLogScroll = ui->logTextDisplay->verticalScrollBar();
    horizLogScroll = ui->logTextDisplay->horizontalScrollBar();

    vertLogScroll->setValue(vertLogScroll->maximum());
    horizLogScroll->setValue(horizLogScroll->minimum());
}

loggingWindow::~loggingWindow()
{
    QMutexLocker lock(&textMutex);
    delete ui;
}

void loggingWindow::showEvent(QShowEvent *event)
{
    (void)event;
    on_toBottomBtn_clicked();
}

void loggingWindow::setInitialDebugState(bool debugModeEnabled)
{
    ui->debugBtn->blockSignals(true);
    ui->debugBtn->setChecked(debugModeEnabled);
    ui->debugBtn->blockSignals(false);
}

void loggingWindow::ingestSettings(preferences prefs) {
    this->prefs = prefs;
    havePrefs = true;
}

void loggingWindow::acceptLogText(QPair<QtMsgType,QString> text)
{
    QMutexLocker lock(&textMutex);
    QString colour = "white";
    if (text.first == QtDebugMsg)
    {
        colour = "#ffbc11"; // orange
    } else if (text.first == QtWarningMsg)
    {
        colour = "yellow";
    } else if (text.first == QtCriticalMsg || text.first == QtFatalMsg)
    {
        colour = "red";
    } else if (text.first == QtInfoMsg) {
        colour = "white";
    }
    else
    {
        colour = "green";
    }
    QString sanitized = text.second.replace(" ", "&nbsp;");
    ui->logTextDisplay->appendHtml(QString("<p><span style='color:%0'>%1</span></p>").arg(colour,sanitized));
    if(vertLogScroll->value() == vertLogScroll->maximum())
    {
        horizLogScroll->setValue(horizLogScroll->minimum());
    }
}

void loggingWindow::sendLogToHost()
{
    if(prefs.pastebinHost.isEmpty() || prefs.pastebinPort == 0 || prefs.pastebinPort > 65535) {
        qInfo(logLogger()) << tr("Pastbin host and/or port is not configured correctly. Check settings.");
        return;
    }

    qInfo(logLogger()).noquote().nospace() << tr("Sending data to ") << prefs.pastebinHost << ":" << prefs.pastebinPort << tr(", Standby.");
    socket->connectToHost(prefs.pastebinHost, prefs.pastebinPort);
    ui->sendToPasteBtn->setDisabled(true);
}

void loggingWindow::handleDataFromLoggingHost()
{
    qInfo(logLogger()) << tr("Receiving data from logging host.");
    QString URL;
    QByteArray data = socket->readAll();
    if(data.length() < 256)
    {
        URL = QString(data).trimmed();
        if(!URL.isEmpty())
        {
            clipboard->setText(URL);
            qInfo(logLogger()) << tr("Sent log to URL: ") << URL;
            qInfo(logLogger()) << tr("This address already copied to the clipboard. Please paste this URL in to your support questions.");
            URLmsgBox.setText(tr("Your log has been posted, and the URL has been copied to the clipboard."));
            URLmsgBox.setInformativeText("<b>" + URL + "</b>");
            URLmsgBox.exec();
            // For whatever reason, showing the message box hides this window.
            this->show();
            this->raise();
            this->activateWindow();
        }
    } else {
        qDebug(logLogger()) << tr("Error, return from logging host too large. Received ") << data.length() << tr(" bytes.");
    }
}

void loggingWindow::disconnectedFromHost()
{
    qInfo(logLogger()) << tr("Disconnected from logging host");
    ui->sendToPasteBtn->setDisabled(false);
}

void loggingWindow::connectedToHost()
{
    qInfo(logLogger()) << tr("Connected to logging host");
    QMutexLocker lock(&textMutex);
    QTextStream outText(socket);
    outText << ui->logTextDisplay->toPlainText();
    outText << "\n----------\nSent from wfview version ";
    outText << WFVIEW_VERSION << "\n----------\n";
    outText.flush();
}

void loggingWindow::handleLoggingHostError(QAbstractSocket::SocketError error)
{
    switch(error)
    {
    case QAbstractSocket::RemoteHostClosedError:
        //qInfo(logLogger()) << "Disconnected from logging host.";
        break;

    default:
        qWarning(logLogger()) << tr("Error connecting to logging host. Check internet connection. Error code: ") << error;
        ui->sendToPasteBtn->setDisabled(false);
        break;
    }
}

void loggingWindow::on_clearDisplayBtn_clicked()
{
    QMutexLocker lock(&textMutex);
    // Only clears the displayed text, not the log file.
    ui->logTextDisplay->clear();
}

void loggingWindow::on_openDirBtn_clicked()
{
    QString cmd;
    bool rtn = false;
    QStringList arg;
    const QFileInfo dir(logDirectory);

#ifdef Q_OS_LINUX
    cmd = "xdg-open";
#elif defined(Q_OS_WIN)
    cmd = QStandardPaths::findExecutable("explorer.exe");
    if (!dir.isDir())
        arg += QLatin1String("/select,");
#else
    cmd = "open";
#endif
    arg += QDir::toNativeSeparators(dir.canonicalFilePath());;
    rtn = QProcess::startDetached(cmd, arg);
    if(!rtn)
        qInfo(logLogger()) << "Error, open log directory" << logDirectory << "command failed";
}

void loggingWindow::on_openLogFileBtn_clicked()
{
    QString cmd;
    bool rtn = false;
#ifdef Q_OS_LINUX
    cmd = "xdg-open";
#elif defined(Q_OS_WIN)
    cmd = QStandardPaths::findExecutable("notepad.exe");
#else
    cmd = "open";
#endif
    rtn = QProcess::startDetached(cmd, { logFilename });
    if(!rtn)
        qInfo(logLogger()) << "Error, open log file command failed";
}

void loggingWindow::on_sendToPasteBtn_clicked()
{
    sendLogToHost();
}

void loggingWindow::on_annotateBtn_clicked()
{
    QMutexLocker lock(&textMutex);
    if(ui->userAnnotationText->text().isEmpty())
        return;
    qInfo(logUser()) << ui->userAnnotationText->text();
    ui->userAnnotationText->clear();
}

void loggingWindow::on_userAnnotationText_returnPressed()
{
    on_annotateBtn_clicked();
}

void loggingWindow::on_copyPathBtn_clicked()
{
    clipboard->setText(logFilename);
}

void loggingWindow::on_debugBtn_clicked(bool checked)
{
    emit setDebugMode(checked);
}

void loggingWindow::on_commDebugChk_clicked(bool checked)
{
    emit setInsaneLoggingMode(checked);
}

void loggingWindow::on_rigctlDebugChk_clicked(bool checked)
{
    emit setRigctlLoggingMode(checked);
}

void loggingWindow::on_toBottomBtn_clicked()
{
    vertLogScroll->setValue(vertLogScroll->maximum());
    horizLogScroll->setValue(horizLogScroll->minimum());
}