Files
DefaultVPN/client/logger.cpp

183 lines
4.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>
#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 "version.h"
#include "utilities.h"
2020-11-23 16:20:25 +03:00
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
#include <core/ipcclient.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);
2020-11-23 16:20:25 +03:00
2020-12-16 06:02:22 +03: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.startsWith("Unknown property") || msg.startsWith("Could not create pixmap") || msg.startsWith("Populating font")) {
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;
Logger::appendAllLog(qFormatLogMessage(type, context, msg));
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;
}
2022-12-28 13:41:45 +03:00
void Logger::appendSshLog(const QString &log)
2022-02-04 17:49:48 +03:00
{
QString dt = QDateTime::currentDateTime().toString();
Instance().m_sshLog.append(dt + ": " + log + "\n");
emit Instance().sshLogChanged(Instance().sshLog());
}
2022-12-28 13:41:45 +03:00
void Logger::appendAllLog(const QString &log)
2022-02-04 17:49:48 +03:00
{
Instance().m_allLog.append(log + "\n");
emit Instance().allLogChanged(Instance().allLog());
}
2022-12-28 13:41:45 +03:00
bool Logger::init()
2020-11-23 16:20:25 +03:00
{
2021-10-04 21:13:07 +03:00
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}");
2020-12-26 23:17:20 +03:00
QString path = userLogsDir();
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;
}
2020-12-16 06:02:22 +03:00
m_file.setFileName(appDir.filePath(m_logFileName));
2022-01-30 17:35:57 +03:00
if (!m_file.open(QIODevice::Append)) {
2020-12-16 06:02:22 +03:00
qWarning() << "Cannot open log file:" << m_logFileName;
2020-11-23 16:20:25 +03:00
return false;
}
2020-12-16 06:02:22 +03:00
m_file.setTextModeEnabled(true);
m_textStream.setDevice(&m_file);
2021-10-17 07:00:00 -07:00
#ifndef QT_DEBUG
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();
}
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
}
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;
}
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);
return file.readAll();
}
2022-12-28 13:41:45 +03:00
bool Logger::openLogsFolder()
2020-11-23 16:20:25 +03:00
{
2020-12-26 23:17:20 +03:00
QString path = 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
2022-12-28 13:41:45 +03:00
bool Logger::openServiceLogsFolder()
2021-10-07 22:21:04 +03:00
{
QString path = Utils::systemLogPath();
path = "file:///" + path;
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
return true;
}
2022-12-28 13:41:45 +03:00
QString Logger::appLogFileNamePath()
2020-12-26 15:03:51 +03:00
{
return m_file.fileName();
}
2022-01-30 17:35:57 +03:00
2022-12-28 13:41:45 +03:00
void Logger::clearLogs()
2022-01-30 17:35:57 +03:00
{
bool isLogActive = m_file.isOpen();
m_file.close();
2022-02-01 19:48:59 +03:00
QFile file(userLogsFilePath());
2022-01-30 17:35:57 +03:00
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.resize(0);
file.close();
if (isLogActive) {
init();
}
}
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()->setLogsEnabled(false);
m_IpcClient->Interface()->cleanUp();
}
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
{
clearLogs();
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
dir.removeRecursively();
clearServiceLogs();
}