2024-08-07 14:05:18 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2024-08-07 14:05:18 +00:00
|
|
|
* Copyright (C) 2024 Symless Ltd.
|
|
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
|
|
|
|
*
|
|
|
|
|
* This package 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 "Logger.h"
|
|
|
|
|
|
|
|
|
|
#include "string_utils.h"
|
|
|
|
|
|
|
|
|
|
#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{
|
2024-10-01 13:30:24 +00:00
|
|
|
"No functional TLS backend was found", "No TLS backend is available",
|
2024-10-17 18:04:35 +00:00
|
|
|
"QSslSocket::connectToHostEncrypted: TLS initialization failed", "Retrying to obtain clipboard.",
|
|
|
|
|
"Unable to obtain clipboard."
|
|
|
|
|
};
|
2024-08-09 13:03:48 +00:00
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
Logger Logger::s_instance;
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
QString printLine(FILE *out, const QString &type, const QString &message, const QString &fileLine = "")
|
|
|
|
|
{
|
2024-08-09 13:03:48 +00:00
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
auto datetime = QDateTime::currentDateTime().toString("yyyy-MM-ddTHH:mm:ss");
|
2024-10-21 03:36:10 +00:00
|
|
|
auto logLine = QString("[%1] %2: %3").arg(datetime, type, message);
|
2024-08-07 14:05:18 +00:00
|
|
|
|
2024-08-09 13:03:48 +00:00
|
|
|
QTextStream stream(&logLine);
|
2024-08-09 18:20:10 +00:00
|
|
|
stream << Qt::endl;
|
|
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
if (!fileLine.isEmpty()) {
|
2024-08-09 18:20:10 +00:00
|
|
|
stream << "\t" + fileLine << Qt::endl;
|
2024-08-07 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-09 18:20:10 +00:00
|
|
|
auto logLineBytes = logLine.toUtf8();
|
|
|
|
|
auto logLine_c = logLineBytes.constData();
|
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.
|
2024-08-09 18:20:10 +00:00
|
|
|
OutputDebugStringA(logLine_c);
|
2024-08-09 13:03:48 +00:00
|
|
|
#else
|
2024-08-09 18:20:10 +00:00
|
|
|
fprintf(out, "%s", logLine_c);
|
2024-08-07 14:05:18 +00:00
|
|
|
fflush(out);
|
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::loadEnvVars()
|
|
|
|
|
{
|
2024-09-17 19:00:25 +00:00
|
|
|
const auto debugEnvVar = qEnvironmentVariable("DESKFLOW_GUI_DEBUG");
|
2024-08-07 14:05:18 +00:00
|
|
|
if (!debugEnvVar.isEmpty()) {
|
|
|
|
|
m_debug = strToTrue(debugEnvVar);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
const auto verboseEnvVar = qEnvironmentVariable("DESKFLOW_GUI_VERBOSE");
|
2024-08-07 14:05:18 +00:00
|
|
|
if (!verboseEnvVar.isEmpty()) {
|
|
|
|
|
m_verbose = strToTrue(verboseEnvVar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Logger::logVerbose(const QString &message) const
|
|
|
|
|
{
|
2024-08-07 14:05:18 +00:00
|
|
|
if (m_verbose) {
|
|
|
|
|
printLine(stdout, "VERBOSE", message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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:
|
|
|
|
|
typeString = "DEBUG";
|
|
|
|
|
if (!m_debug) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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-08-07 14:05:18 +00:00
|
|
|
emit newLine(logLine);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
} // namespace deskflow::gui
|