Files
amnezia-client/common/logger/logger.cpp
T

331 lines
8.0 KiB
C++
Raw Normal View History

2022-12-28 13:41:45 +03:00
#include "logger.h"
2022-02-22 02:08:57 +03:00
#include <QDateTime>
2020-12-16 06:02:22 +03:00
#include <QDebug>
#include <QDesktopServices>
2020-11-23 16:20:25 +03:00
#include <QDir>
2023-07-15 14:19:48 -07:00
#include <QJsonDocument>
2024-09-09 20:53:44 +04:00
#include <QMetaEnum>
2020-11-23 16:20:25 +03:00
#include <QStandardPaths>
2020-12-16 06:02:22 +03:00
#include <QUrl>
2020-11-23 16:20:25 +03:00
2020-12-26 15:03:51 +03:00
#include <iostream>
#include "utilities.h"
2024-09-09 20:53:44 +04:00
#include "version.h"
2020-11-23 16:20:25 +03:00
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2024-09-09 20:53:44 +04:00
#include <core/ipcclient.h>
2022-02-22 02:08:57 +03:00
#endif
2024-02-10 19:44:55 +03:00
#ifdef Q_OS_IOS
#include <AmneziaVPN-Swift.h>
#endif
2022-12-28 13:41:45 +03:00
QFile Logger::m_file;
QTextStream Logger::m_textStream;
QString Logger::m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
2024-09-09 20:53:44 +04:00
QString Logger::m_serviceLogFileName = QString("%1.log").arg(SERVICE_NAME);
2020-11-23 16:20:25 +03:00
2024-09-09 20:53:44 +04:00
void debugMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
2020-11-23 16:20:25 +03:00
{
2020-12-16 06:02:22 +03:00
if (msg.simplified().isEmpty()) {
return;
}
2020-12-26 23:17:20 +03:00
// Skip annoying messages from Qt
if (msg.contains("OpenType support missing for")) {
return;
}
2024-09-09 20:53:44 +04:00
if (msg.startsWith("Unknown property") || msg.startsWith("Could not create pixmap") || msg.startsWith("Populating font")
|| msg.startsWith("stale focus object")) {
2020-12-16 06:02:22 +03:00
return;
}
2020-11-23 16:20:25 +03:00
2022-12-28 13:41:45 +03:00
Logger::m_textStream << qFormatLogMessage(type, context, msg) << Qt::endl << Qt::flush;
2020-12-26 15:03:51 +03:00
std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl << std::flush;
2020-11-23 16:20:25 +03:00
}
2022-12-28 13:41:45 +03:00
Logger &Logger::Instance()
2022-02-04 17:49:48 +03:00
{
2022-12-28 13:41:45 +03:00
static Logger s;
2022-02-04 17:49:48 +03:00
return s;
}
2024-09-09 20:53:44 +04:00
bool Logger::init(bool isServiceLogger)
2020-11-23 16:20:25 +03:00
{
2024-09-09 20:53:44 +04:00
QString path = isServiceLogger ? systemLogDir() : userLogsDir();
QString logFileName = isServiceLogger ? m_serviceLogFileName : m_logFileName ;
2020-11-23 16:20:25 +03:00
QDir appDir(path);
2020-12-16 06:02:22 +03:00
if (!appDir.mkpath(path)) {
2020-11-23 16:20:25 +03:00
return false;
}
2024-09-09 20:53:44 +04:00
m_file.setFileName(appDir.filePath(logFileName));
2022-01-30 17:35:57 +03:00
if (!m_file.open(QIODevice::Append)) {
2024-09-09 20:53:44 +04:00
qWarning() << "Cannot open log file:" << logFileName;
2020-11-23 16:20:25 +03:00
return false;
}
2024-09-09 20:53:44 +04:00
2020-12-16 06:02:22 +03:00
m_file.setTextModeEnabled(true);
m_textStream.setDevice(&m_file);
2024-09-09 20:53:44 +04:00
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}");
2021-10-17 07:00:00 -07:00
2023-08-27 10:46:41 -07:00
#if !defined(QT_DEBUG) || defined(Q_OS_IOS)
2020-11-23 16:20:25 +03:00
qInstallMessageHandler(debugMessageHandler);
2021-10-04 21:13:07 +03:00
#endif
2020-11-23 16:20:25 +03:00
return true;
}
2022-12-28 13:41:45 +03:00
void Logger::deInit()
{
qInstallMessageHandler(nullptr);
qSetMessagePattern("%{message}");
m_textStream.setDevice(nullptr);
m_file.close();
}
2024-09-09 20:53:44 +04:00
bool Logger::setServiceLogsEnabled(bool enabled)
{
#ifdef AMNEZIA_DESKTOP
IpcClient *m_IpcClient = new IpcClient;
if (!m_IpcClient->isSocketConnected()) {
if (!IpcClient::init(m_IpcClient)) {
qWarning() << "Error occurred when init IPC client";
return false;
}
}
if (m_IpcClient->Interface()) {
m_IpcClient->Interface()->setLogsEnabled(enabled);
2024-09-09 20:53:44 +04:00
} else {
qWarning() << "Error occurred setting up service logs";
return false;
}
#endif
return true;
}
2022-12-28 13:41:45 +03:00
QString Logger::userLogsDir()
2020-11-23 16:20:25 +03:00
{
2020-12-26 23:17:20 +03:00
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/log";
2020-11-23 16:20:25 +03:00
}
2024-09-09 20:53:44 +04:00
QString Logger::systemLogDir()
{
#ifdef Q_OS_WIN
QStringList locationList = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
QString primaryLocation = "ProgramData";
foreach (const QString &location, locationList) {
if (location.contains(primaryLocation)) {
return QString("%1/%2/log").arg(location).arg(APPLICATION_NAME);
}
}
return QString();
#else
return QString("/var/log/%1").arg(APPLICATION_NAME);
#endif
}
2022-12-28 13:41:45 +03:00
QString Logger::userLogsFilePath()
2022-02-01 19:48:59 +03:00
{
return userLogsDir() + QDir::separator() + m_logFileName;
}
2024-09-09 20:53:44 +04:00
QString Logger::serviceLogsFilePath()
{
return systemLogDir() + QDir::separator() + m_serviceLogFileName;
}
2022-12-28 13:41:45 +03:00
QString Logger::getLogFile()
2022-02-01 19:48:59 +03:00
{
m_file.flush();
QFile file(userLogsFilePath());
file.open(QIODevice::ReadOnly);
2024-02-10 19:44:55 +03:00
QString qtLog = file.readAll();
2024-09-09 20:53:44 +04:00
2024-02-10 19:44:55 +03:00
#ifdef Q_OS_IOS
return QString().fromStdString(AmneziaVPN::swiftUpdateLogData(qtLog.toStdString()));
#else
return qtLog;
#endif
2024-09-09 20:53:44 +04:00
}
2024-02-10 19:44:55 +03:00
2024-09-09 20:53:44 +04:00
QString Logger::getServiceLogFile()
{
m_file.flush();
QFile file(serviceLogsFilePath());
file.open(QIODevice::ReadOnly);
QString qtLog = file.readAll();
#ifdef Q_OS_IOS
return QString().fromStdString(AmneziaVPN::swiftUpdateLogData(qtLog.toStdString()));
#else
return qtLog;
#endif
2022-02-01 19:48:59 +03:00
}
2024-09-09 20:53:44 +04:00
bool Logger::openLogsFolder(bool isServiceLogger)
2020-11-23 16:20:25 +03:00
{
2024-09-09 20:53:44 +04:00
QString path = isServiceLogger ? systemLogDir() : userLogsDir();
2020-12-16 06:02:22 +03:00
#ifdef Q_OS_WIN
path = "file:///" + path;
#endif
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(path))) {
qWarning() << "Can't open url:" << path;
return false;
2020-11-23 16:20:25 +03:00
}
2020-12-16 06:02:22 +03:00
return true;
2020-11-23 16:20:25 +03:00
}
2020-12-26 15:03:51 +03:00
2024-09-09 20:53:44 +04:00
void Logger::clearLogs(bool isServiceLogger)
2022-01-30 17:35:57 +03:00
{
bool isLogActive = m_file.isOpen();
m_file.close();
2024-09-09 20:53:44 +04:00
QFile file(isServiceLogger ? serviceLogsFilePath() : userLogsFilePath());
2022-01-30 17:35:57 +03:00
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.resize(0);
file.close();
2024-09-09 20:53:44 +04:00
2024-02-10 19:44:55 +03:00
#ifdef Q_OS_IOS
AmneziaVPN::swiftDeleteLog();
#endif
2024-09-09 20:53:44 +04:00
2022-01-30 17:35:57 +03:00
if (isLogActive) {
2024-09-09 20:53:44 +04:00
init(isServiceLogger);
2022-01-30 17:35:57 +03:00
}
}
2022-12-28 13:41:45 +03:00
void Logger::clearServiceLogs()
2022-01-30 17:35:57 +03:00
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2022-01-30 17:35:57 +03:00
IpcClient *m_IpcClient = new IpcClient;
if (!m_IpcClient->isSocketConnected()) {
if (!IpcClient::init(m_IpcClient)) {
2023-04-11 09:50:44 -04:00
qWarning() << "Error occurred when init IPC client";
2022-01-30 17:35:57 +03:00
return;
}
}
if (m_IpcClient->Interface()) {
m_IpcClient->Interface()->clearLogs();
2024-09-09 20:53:44 +04:00
} else {
2023-04-11 09:50:44 -04:00
qWarning() << "Error occurred cleaning up service logs";
2022-01-30 17:35:57 +03:00
}
2022-02-22 02:08:57 +03:00
#endif
2022-01-30 17:35:57 +03:00
}
2022-12-28 13:41:45 +03:00
void Logger::cleanUp()
2022-01-30 17:35:57 +03:00
{
2024-09-09 20:53:44 +04:00
clearLogs(false);
2022-01-30 17:35:57 +03:00
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
dir.removeRecursively();
2024-09-09 20:53:44 +04:00
clearLogs(true);
2022-01-30 17:35:57 +03:00
}
2023-07-15 14:19:48 -07:00
2024-09-09 20:53:44 +04:00
Logger::Log::Log(Logger *logger, LogLevel logLevel) : m_logger(logger), m_logLevel(logLevel), m_data(new Data())
{
}
2023-07-15 14:19:48 -07:00
2024-09-09 20:53:44 +04:00
Logger::Log::~Log()
{
2023-07-15 14:19:48 -07:00
qDebug() << "Amnezia" << m_logger->className() << m_data->m_buffer.trimmed();
delete m_data;
}
2024-09-09 20:53:44 +04:00
Logger::Log Logger::error()
{
return Log(this, LogLevel::Error);
}
Logger::Log Logger::warning()
{
return Log(this, LogLevel::Warning);
}
Logger::Log Logger::info()
{
return Log(this, LogLevel::Info);
}
Logger::Log Logger::debug()
{
return Log(this, LogLevel::Debug);
}
QString Logger::sensitive(const QString &input)
{
2023-07-15 14:19:48 -07:00
#ifdef Q_DEBUG
return input;
#else
Q_UNUSED(input);
return QString(8, 'X');
#endif
}
2024-09-09 20:53:44 +04:00
#define CREATE_LOG_OP_REF(x) \
Logger::Log &Logger::Log::operator<<(x t) \
{ \
m_data->m_ts << t << ' '; \
return *this; \
}
2023-07-15 14:19:48 -07:00
CREATE_LOG_OP_REF(uint64_t);
2024-09-09 20:53:44 +04:00
CREATE_LOG_OP_REF(const char *);
CREATE_LOG_OP_REF(const QString &);
CREATE_LOG_OP_REF(const QByteArray &);
CREATE_LOG_OP_REF(const void *);
2023-07-15 14:19:48 -07:00
#undef CREATE_LOG_OP_REF
2024-09-09 20:53:44 +04:00
Logger::Log &Logger::Log::operator<<(const QStringList &t)
{
2023-07-15 14:19:48 -07:00
m_data->m_ts << '[' << t.join(",") << ']' << ' ';
return *this;
}
2024-09-09 20:53:44 +04:00
Logger::Log &Logger::Log::operator<<(const QJsonObject &t)
{
2023-07-15 14:19:48 -07:00
m_data->m_ts << QJsonDocument(t).toJson(QJsonDocument::Indented) << ' ';
return *this;
}
2024-09-09 20:53:44 +04:00
Logger::Log &Logger::Log::operator<<(QTextStreamFunction t)
{
2023-07-15 14:19:48 -07:00
m_data->m_ts << t;
return *this;
}
2024-09-09 20:53:44 +04:00
void Logger::Log::addMetaEnum(quint64 value, const QMetaObject *meta, const char *name)
{
2023-07-15 14:19:48 -07:00
QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
QString out;
QTextStream ts(&out);
2024-09-09 20:53:44 +04:00
if (const char *scope = me.scope()) {
2023-07-15 14:19:48 -07:00
ts << scope << "::";
}
2024-09-09 20:53:44 +04:00
const char *key = me.valueToKey(static_cast<int>(value));
2023-07-15 14:19:48 -07:00
const bool scoped = me.isScoped();
if (scoped || !key) {
ts << me.enumName() << (!key ? "(" : "::");
}
if (key) {
ts << key;
} else {
ts << value << ")";
}
m_data->m_ts << out;
}