Add installer

This commit is contained in:
driftingsun 2020-12-16 06:02:22 +03:00
parent c9bc8aa8c1
commit a2a5cafc5f
73 changed files with 4354 additions and 488 deletions

View file

@ -1,126 +1,68 @@
#include "debug.h"
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include <QCoreApplication>
#include <QDateTime>
#include <QUrl>
#define LOGS_DIR "logs"
#define CLIENT_LOG_SUFFIX "amneziavpn.log"
#define MAX_LOG_FILES 5
#define FORMAT_STRING "yyyy-MM-dd--hh-mm-ss"
#include "debug.h"
#include "defines.h"
QFile Debug::m_clientLog;
QTextStream Debug::m_clientLogTextStream;
QString Debug::m_clientLogName;
QFile Debug::m_file;
QTextStream Debug::m_textStream;
QString Debug::m_logFileName;
void debugMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
// Skip Qt warnings
if (msg.contains("known incorrect sRGB profile")) return;
if (msg.contains("libpng warning")) return;
if (msg.contains("Unknown property ffont")) return;
if (msg.simplified().isEmpty()) {
return;
}
Debug::m_clientLogTextStream << qFormatLogMessage(type, context, msg) << endl << flush;
// Temporally disabled
if (msg.startsWith("Unknown property") || msg.startsWith("Could not create pixmap")) {
return;
}
Debug::m_textStream << qFormatLogMessage(type, context, msg) << endl << flush;
}
bool Debug::init()
{
QString path = qApp->applicationDirPath();
QString path = logsDir();
QDir appDir(path);
// init function is called before exec application, so data location folder may not exist
if (!appDir.exists())
{
qWarning() << "Debug: init: log directory doesn't exist or mkpath command error:" << path;
if (!appDir.mkpath(path)) {
return false;
}
if (!appDir.exists(LOGS_DIR) && !appDir.mkdir(LOGS_DIR))
{
qWarning() << "Debug: init: log directory doesn't exist or mkdir command error:" << path << LOGS_DIR;
return false;
}
m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
if (!appDir.cd(LOGS_DIR))
{
qWarning() << "Debug: init: cd command error:" << path << LOGS_DIR;
return false;
}
//delete older log files
auto clientLogsCount = 0;
QFileInfoList logDirList = appDir.entryInfoList(
QDir::Files | QDir::NoDotAndDotDot,
QDir::Time);
for (auto fileInfo : logDirList)
{
if ((fileInfo.completeSuffix() == CLIENT_LOG_SUFFIX &&
++clientLogsCount > MAX_LOG_FILES))
{
appDir.remove(fileInfo.filePath());
}
}
//prepare log file names
auto currentDateTime = QDateTime::currentDateTime().toString(FORMAT_STRING);
m_clientLogName = QString("%1.%2").arg(currentDateTime).arg(CLIENT_LOG_SUFFIX);
return init(appDir);
}
bool Debug::init(QDir& appDir)
{
Q_UNUSED(appDir)
qSetMessagePattern("[%{time}|%{type}] %{message}");
#ifndef QT_DEBUG
m_clientLog.setFileName(appDir.filePath(m_clientLogName));
if (!m_clientLog.open(QIODevice::WriteOnly | QIODevice::Append)) {
qWarning() << "Debug::init - failed to open m_clientLog file:" << m_clientLogName;
m_file.setFileName(appDir.filePath(m_logFileName));
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning() << "Cannot open log file:" << m_logFileName;
return false;
}
m_clientLog.setTextModeEnabled(true);
m_clientLogTextStream.setDevice(&m_clientLog);
m_file.setTextModeEnabled(true);
m_textStream.setDevice(&m_file);
qInstallMessageHandler(debugMessageHandler);
#else
#ifdef DEBUG_OUTPUT_TWO_DIRECTIONAL
m_clientLog.setFileName(appDir.filePath(m_clientLogName));
if (!m_clientLog.open(QIODevice::WriteOnly | QIODevice::Append))
return false;
m_clientLog.setTextModeEnabled(true);
m_clientLogTextStream.setDevice(&m_clientLog);
defaultMessageHandler = qInstallMessageHandler(debugMessageHandler);
#endif
#endif
return true;
}
QString Debug::getPathToClientLog()
QString Debug::logsDir()
{
QString path = qApp->applicationDirPath();
QDir appDir(path);
if (!appDir.exists(LOGS_DIR) || !appDir.cd(LOGS_DIR))
{
qWarning() << "Debug: log directory doesn't exist or cd command error:" << path;
return "";
}
return appDir.filePath(m_clientLogName);
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/logs";
}
QString Debug::getPathToLogsDir()
bool Debug::openLogsFolder()
{
QString path = qApp->applicationDirPath();
QDir appDir(path);
if (!appDir.exists(LOGS_DIR) || !appDir.cd(LOGS_DIR))
{
qWarning() << "Debug: log directory doesn't exist or cd command error" << path;
return "";
QString path = logsDir();
#ifdef Q_OS_WIN
path = "file:///" + path;
#endif
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(path))) {
qWarning() << "Can't open url:" << path;
return false;
}
return appDir.absolutePath();
return true;
}