2024-08-07 14:05:18 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2024-08-07 14:05:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Logger.h"
|
2025-11-04 16:27:52 +00:00
|
|
|
#include "common/Settings.h"
|
2024-08-07 14:05:18 +00:00
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QTime>
|
|
|
|
|
|
2024-08-09 13:03:48 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
namespace deskflow::gui {
|
2024-08-07 14:05:18 +00:00
|
|
|
|
2024-08-09 13:03:48 +00:00
|
|
|
const auto kForceDebugMessages = QStringList{
|
2025-11-04 01:03:01 +00:00
|
|
|
QStringLiteral("No functional TLS backend was found"), QStringLiteral("No TLS backend is available"),
|
|
|
|
|
QStringLiteral("QSslSocket::connectToHostEncrypted: TLS initialization failed"),
|
|
|
|
|
QStringLiteral("Retrying to obtain clipboard."), QStringLiteral("Unable to obtain clipboard.")
|
2024-10-17 18:04:35 +00:00
|
|
|
};
|
2024-08-09 13:03:48 +00:00
|
|
|
|
2025-11-04 01:38:22 +00:00
|
|
|
QString printLine(FILE *out, const QString &type, const QString &message, const QString &fileLine = {})
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-11-04 02:14:11 +00:00
|
|
|
const auto datetime = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
|
2025-11-04 01:03:01 +00:00
|
|
|
auto logLine = QStringLiteral("[%1] %2: %3").arg(datetime, type, message);
|
2024-08-07 14:05:18 +00:00
|
|
|
|
|
|
|
|
if (!fileLine.isEmpty()) {
|
2025-11-04 01:38:22 +00:00
|
|
|
logLine.append(QStringLiteral("\n\t%1").arg(fileLine));
|
2024-08-07 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 09:13:16 +00:00
|
|
|
// We must return a non-terminated log line, but before returning,
|
|
|
|
|
// stdout/stderr and Windows debug output all expect a terminated line.
|
2025-11-04 01:38:22 +00:00
|
|
|
const auto terminatedLogLine = QStringLiteral("%1\n").arg(logLine);
|
2024-08-09 13:03:48 +00:00
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
// Debug output is viewable using either VS Code, Visual Studio, DebugView, or
|
|
|
|
|
// DbgView++ (only one can be used at once). It's important to send output to
|
|
|
|
|
// the debug output API, because it's difficult to view stdout and stderr from
|
|
|
|
|
// a Windows GUI app.
|
2025-04-14 09:13:16 +00:00
|
|
|
OutputDebugStringA(terminatedLogLine.toLocal8Bit().constData());
|
2024-08-09 13:03:48 +00:00
|
|
|
#else
|
2025-11-04 01:38:22 +00:00
|
|
|
QTextStream(out) << terminatedLogLine;
|
2024-08-09 13:03:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
return logLine;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Logger::handleMessage(const QtMsgType type, const QString &fileLine, const QString &message)
|
|
|
|
|
{
|
2024-08-09 13:03:48 +00:00
|
|
|
auto mutatedType = type;
|
|
|
|
|
if (kForceDebugMessages.contains(message)) {
|
|
|
|
|
mutatedType = QtDebugMsg;
|
|
|
|
|
}
|
2024-08-07 14:05:18 +00:00
|
|
|
|
|
|
|
|
QString typeString;
|
|
|
|
|
auto out = stdout;
|
2024-08-09 13:03:48 +00:00
|
|
|
switch (mutatedType) {
|
2024-08-07 14:05:18 +00:00
|
|
|
case QtDebugMsg:
|
2025-11-04 16:27:52 +00:00
|
|
|
if (!Settings::value(Settings::Log::GuiDebug).toBool())
|
2024-08-07 14:05:18 +00:00
|
|
|
return;
|
2025-11-04 16:27:52 +00:00
|
|
|
typeString = "DEBUG";
|
2024-08-07 14:05:18 +00:00
|
|
|
break;
|
|
|
|
|
case QtInfoMsg:
|
|
|
|
|
typeString = "INFO";
|
|
|
|
|
break;
|
|
|
|
|
case QtWarningMsg:
|
|
|
|
|
typeString = "WARNING";
|
|
|
|
|
out = stderr;
|
|
|
|
|
break;
|
|
|
|
|
case QtCriticalMsg:
|
|
|
|
|
typeString = "CRITICAL";
|
|
|
|
|
out = stderr;
|
|
|
|
|
break;
|
|
|
|
|
case QtFatalMsg:
|
|
|
|
|
typeString = "FATAL";
|
|
|
|
|
out = stderr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 21:39:09 +00:00
|
|
|
const auto logLine = printLine(out, typeString, message, fileLine);
|
2024-10-21 04:08:10 +00:00
|
|
|
Q_EMIT newLine(logLine);
|
2024-08-07 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
} // namespace deskflow::gui
|