Logs functions fixes

This commit is contained in:
pokamest 2022-01-30 17:35:57 +03:00
parent 95fe09489c
commit d24f6ae064
15 changed files with 240 additions and 39 deletions

View file

@ -6,13 +6,15 @@
#include <iostream> #include <iostream>
#include <core/ipcclient.h>
#include "debug.h" #include "debug.h"
#include "defines.h" #include "defines.h"
#include "utils.h" #include "utils.h"
QFile Debug::m_file; QFile Debug::m_file;
QTextStream Debug::m_textStream; QTextStream Debug::m_textStream;
QString Debug::m_logFileName; QString Debug::m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{ {
@ -40,11 +42,8 @@ bool Debug::init()
return false; return false;
} }
m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
m_file.setFileName(appDir.filePath(m_logFileName)); m_file.setFileName(appDir.filePath(m_logFileName));
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { if (!m_file.open(QIODevice::Append)) {
qWarning() << "Cannot open log file:" << m_logFileName; qWarning() << "Cannot open log file:" << m_logFileName;
return false; return false;
} }
@ -88,3 +87,52 @@ QString Debug::appLogFileNamePath()
{ {
return m_file.fileName(); return m_file.fileName();
} }
void Debug::clearLogs()
{
bool isLogActive = m_file.isOpen();
m_file.close();
QString path = userLogsDir();
QDir appDir(path);
QFile file;
file.setFileName(appDir.filePath(m_logFileName));
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.resize(0);
file.close();
if (isLogActive) {
init();
}
}
void Debug::clearServiceLogs()
{
IpcClient *m_IpcClient = new IpcClient;
if (!m_IpcClient->isSocketConnected()) {
if (!IpcClient::init(m_IpcClient)) {
qWarning() << "Error occured when init IPC client";
return;
}
}
if (m_IpcClient->Interface()) {
m_IpcClient->Interface()->setLogsEnabled(false);
m_IpcClient->Interface()->cleanUp();
}
else {
qWarning() << "Error occured cleaning up service logs";
}
}
void Debug::cleanUp()
{
clearLogs();
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
dir.removeRecursively();
clearServiceLogs();
}

View file

@ -14,6 +14,9 @@ public:
static bool openLogsFolder(); static bool openLogsFolder();
static bool openServiceLogsFolder(); static bool openServiceLogsFolder();
static QString appLogFileNamePath(); static QString appLogFileNamePath();
static void clearLogs();
static void clearServiceLogs();
static void cleanUp();
private: private:
static QString userLogsDir(); static QString userLogsDir();

View file

@ -86,15 +86,15 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
#endif #endif
#if defined(Q_OS_ANDROID)
NativeHelpers::registerApplicationInstance(&app);
#endif
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
AllowSetForegroundWindow(0); AllowSetForegroundWindow(0);
#endif #endif
#if defined(Q_OS_ANDROID)
NativeHelpers::registerApplicationInstance(&app);
#endif
loadTranslator(); loadTranslator();
QFontDatabase::addApplicationFont(":/fonts/Lato-Black.ttf"); QFontDatabase::addApplicationFont(":/fonts/Lato-Black.ttf");
@ -120,10 +120,26 @@ int main(int argc, char *argv[])
QCommandLineOption c_autostart {{"a", "autostart"}, "System autostart"}; QCommandLineOption c_autostart {{"a", "autostart"}, "System autostart"};
parser.addOption(c_autostart); parser.addOption(c_autostart);
QCommandLineOption c_cleanup {{"c", "cleanup"}, "Cleanup logs"};
parser.addOption(c_cleanup);
parser.process(app); parser.process(app);
if (!Debug::init()) { if (parser.isSet(c_cleanup)) {
qWarning() << "Initialization of debug subsystem failed"; Debug::cleanUp();
QTimer::singleShot(100,[&app]{
app.quit();
});
app.exec();
return 0;
}
Settings settings;
if (settings.isSaveLogs()) {
if (!Debug::init()) {
qWarning() << "Initialization of debug subsystem failed";
}
} }
app.setQuitOnLastWindowClosed(false); app.setQuitOnLastWindowClosed(false);
@ -203,18 +219,16 @@ int main(int argc, char *argv[])
uiLogic->showOnStartup(); uiLogic->showOnStartup();
#endif #endif
// TODO - fix
// TODO - fix #if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
//#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) if (app.isPrimary()) {
// if (app.isPrimary()) { QObject::connect(&app, &SingleApplication::instanceStarted, uiLogic, [&](){
// QObject::connect(&app, &SingleApplication::instanceStarted, &mainWindow, [&](){ qDebug() << "Secondary instance started, showing this window instead";
// qDebug() << "Secondary instance started, showing this window instead"; emit uiLogic->show();
// mainWindow.show(); emit uiLogic->raise();
// mainWindow.showNormal(); });
// mainWindow.raise(); }
// }); #endif
// }
//#endif
return app.exec(); return app.exec();
} }

View file

@ -159,8 +159,8 @@ ErrorCode OpenVpnProtocol::start()
return lastError(); return lastError();
} }
QString vpnLogFileNamePath = Utils::systemLogPath() + "/openvpn.log"; // QString vpnLogFileNamePath = Utils::systemLogPath() + "/openvpn.log";
Utils::createEmptyFile(vpnLogFileNamePath); // Utils::createEmptyFile(vpnLogFileNamePath);
if (!m_managementServer.start(m_managementHost, m_managementPort)) { if (!m_managementServer.start(m_managementHost, m_managementPort)) {
setLastError(ErrorCode::OpenVpnManagementServerError); setLastError(ErrorCode::OpenVpnManagementServerError);
@ -186,8 +186,7 @@ ErrorCode OpenVpnProtocol::start()
m_openVpnProcess->setProgram(openVpnExecPath()); m_openVpnProcess->setProgram(openVpnExecPath());
QStringList arguments({"--config" , configPath(), QStringList arguments({"--config" , configPath(),
"--management", m_managementHost, QString::number(m_managementPort), "--management", m_managementHost, QString::number(m_managementPort),
"--management-client", "--management-client"/*, "--log", vpnLogFileNamePath */
"--log", vpnLogFileNamePath
}); });
m_openVpnProcess->setArguments(arguments); m_openVpnProcess->setArguments(arguments);

View file

@ -67,6 +67,9 @@ public:
bool isStartMinimized() const { return m_settings.value("Conf/startMinimized", false).toBool(); } bool isStartMinimized() const { return m_settings.value("Conf/startMinimized", false).toBool(); }
void setStartMinimized(bool enabled) { m_settings.setValue("Conf/startMinimized", enabled); } void setStartMinimized(bool enabled) { m_settings.setValue("Conf/startMinimized", enabled); }
bool isSaveLogs() const { return m_settings.value("Conf/saveLogs", false).toBool(); }
void setSaveLogs(bool enabled) { m_settings.setValue("Conf/saveLogs", enabled); }
enum RouteMode { enum RouteMode {
VpnAllSites, VpnAllSites,
VpnOnlyForwardSites, VpnOnlyForwardSites,

View file

@ -11,7 +11,8 @@ AppSettingsLogic::AppSettingsLogic(UiLogic *logic, QObject *parent):
PageLogicBase(logic, parent), PageLogicBase(logic, parent),
m_checkBoxAutostartChecked{false}, m_checkBoxAutostartChecked{false},
m_checkBoxAutoConnectChecked{false}, m_checkBoxAutoConnectChecked{false},
m_checkBoxStartMinimizedChecked{false} m_checkBoxStartMinimizedChecked{false},
m_checkBoxSaveLogsChecked{false}
{ {
} }
@ -21,6 +22,7 @@ void AppSettingsLogic::onUpdatePage()
set_checkBoxAutostartChecked(Autostart::isAutostart()); set_checkBoxAutostartChecked(Autostart::isAutostart());
set_checkBoxAutoConnectChecked(m_settings.isAutoConnect()); set_checkBoxAutoConnectChecked(m_settings.isAutoConnect());
set_checkBoxStartMinimizedChecked(m_settings.isStartMinimized()); set_checkBoxStartMinimizedChecked(m_settings.isStartMinimized());
set_checkBoxSaveLogsChecked(m_settings.isSaveLogs());
QString ver = QString("%1: %2 (%3)") QString ver = QString("%1: %2 (%3)")
.arg(tr("Software version")) .arg(tr("Software version"))
@ -47,7 +49,23 @@ void AppSettingsLogic::onCheckBoxStartMinimizedToggled(bool checked)
m_settings.setStartMinimized(checked); m_settings.setStartMinimized(checked);
} }
void AppSettingsLogic::onCheckBoxSaveLogsCheckedToggled(bool checked)
{
m_settings.setSaveLogs(checked);
}
void AppSettingsLogic::onPushButtonOpenLogsClicked() void AppSettingsLogic::onPushButtonOpenLogsClicked()
{ {
Debug::openLogsFolder(); Debug::openLogsFolder();
} }
void AppSettingsLogic::onPushButtonExportLogsClicked()
{
}
void AppSettingsLogic::onPushButtonClearLogsClicked()
{
Debug::clearLogs();
Debug::clearServiceLogs();
}

View file

@ -11,6 +11,7 @@ class AppSettingsLogic : public PageLogicBase
AUTO_PROPERTY(bool, checkBoxAutostartChecked) AUTO_PROPERTY(bool, checkBoxAutostartChecked)
AUTO_PROPERTY(bool, checkBoxAutoConnectChecked) AUTO_PROPERTY(bool, checkBoxAutoConnectChecked)
AUTO_PROPERTY(bool, checkBoxStartMinimizedChecked) AUTO_PROPERTY(bool, checkBoxStartMinimizedChecked)
AUTO_PROPERTY(bool, checkBoxSaveLogsChecked)
AUTO_PROPERTY(QString, labelVersionText) AUTO_PROPERTY(QString, labelVersionText)
public: public:
@ -19,7 +20,10 @@ public:
Q_INVOKABLE void onCheckBoxAutostartToggled(bool checked); Q_INVOKABLE void onCheckBoxAutostartToggled(bool checked);
Q_INVOKABLE void onCheckBoxAutoconnectToggled(bool checked); Q_INVOKABLE void onCheckBoxAutoconnectToggled(bool checked);
Q_INVOKABLE void onCheckBoxStartMinimizedToggled(bool checked); Q_INVOKABLE void onCheckBoxStartMinimizedToggled(bool checked);
Q_INVOKABLE void onCheckBoxSaveLogsCheckedToggled(bool checked);
Q_INVOKABLE void onPushButtonOpenLogsClicked(); Q_INVOKABLE void onPushButtonOpenLogsClicked();
Q_INVOKABLE void onPushButtonExportLogsClicked();
Q_INVOKABLE void onPushButtonClearLogsClicked();
public: public:
explicit AppSettingsLogic(UiLogic *uiLogic, QObject *parent = nullptr); explicit AppSettingsLogic(UiLogic *uiLogic, QObject *parent = nullptr);

View file

@ -18,7 +18,9 @@ using namespace amnezia;
using namespace PageEnumNS; using namespace PageEnumNS;
OtherProtocolsLogic::OtherProtocolsLogic(UiLogic *logic, QObject *parent): OtherProtocolsLogic::OtherProtocolsLogic(UiLogic *logic, QObject *parent):
PageProtocolLogicBase(logic, parent) PageProtocolLogicBase(logic, parent),
m_checkBoxSftpRestoreChecked{false}
{ {
} }

View file

@ -81,9 +81,19 @@ PageBase {
Qt.openUrlExternally("https://github.com/amnezia-vpn/desktop-client/releases/latest") Qt.openUrlExternally("https://github.com/amnezia-vpn/desktop-client/releases/latest")
} }
} }
BlueButtonType {
CheckBoxType {
Layout.fillWidth: true Layout.fillWidth: true
Layout.topMargin: 15 Layout.topMargin: 15
text: qsTr("Keep logs")
checked: AppSettingsLogic.checkBoxSaveLogsChecked
onCheckedChanged: {
AppSettingsLogic.checkBoxSaveLogsChecked = checked
AppSettingsLogic.onCheckBoxSaveLogsCheckedToggled(checked)
}
}
BlueButtonType {
Layout.fillWidth: true
Layout.preferredHeight: 41 Layout.preferredHeight: 41
text: qsTr("Open logs folder") text: qsTr("Open logs folder")
onClicked: { onClicked: {
@ -97,10 +107,30 @@ PageBase {
Layout.preferredHeight: 41 Layout.preferredHeight: 41
text: qsTr("Export logs") text: qsTr("Export logs")
onClicked: { onClicked: {
AppSettingsLogic.onPushButtonOpenLogsClicked() AppSettingsLogic.onPushButtonExportLogsClicked()
} }
} }
BlueButtonType {
Layout.fillWidth: true
Layout.topMargin: 15
Layout.preferredHeight: 41
property string start_text: qsTr("Clear logs")
property string end_text: qsTr("Cleared")
text: start_text
Timer {
id: timer
interval: 1000; running: false; repeat: false
onTriggered: parent.text = parent.start_text
}
onClicked: {
text = end_text
timer.running = true
AppSettingsLogic.onPushButtonClearLogsClicked()
}
}
} }
} }

View file

@ -15,5 +15,8 @@ class IpcInterface
SLOT( bool checkAndInstallDriver() ); SLOT( bool checkAndInstallDriver() );
SLOT( QStringList getTapList() ); SLOT( QStringList getTapList() );
SLOT( void cleanUp() );
SLOT( void setLogsEnabled(bool enabled) );
}; };

View file

@ -5,6 +5,8 @@
#include <QLocalSocket> #include <QLocalSocket>
#include "router.h" #include "router.h"
#include "log.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include "tapcontroller_win.h" #include "tapcontroller_win.h"
#endif #endif
@ -33,7 +35,7 @@ int IpcServer::createPrivilegedProcess()
// Make sure any connections are handed to QtRO // Make sure any connections are handed to QtRO
QObject::connect(pd.localServer.data(), &QLocalServer::newConnection, this, [pd]() { QObject::connect(pd.localServer.data(), &QLocalServer::newConnection, this, [pd]() {
qDebug() << "LocalServer new connection"; qDebug() << "IpcServer new connection";
if (pd.serverNode) { if (pd.serverNode) {
pd.serverNode->addHostSideConnection(pd.localServer->nextPendingConnection()); pd.serverNode->addHostSideConnection(pd.localServer->nextPendingConnection());
pd.serverNode->enableRemoting(pd.ipcProcess.data()); pd.serverNode->enableRemoting(pd.ipcProcess.data());
@ -105,3 +107,20 @@ QStringList IpcServer::getTapList()
return QStringList(); return QStringList();
#endif #endif
} }
void IpcServer::cleanUp()
{
qDebug() << "IpcServer::cleanUp";
Log::deinit();
Log::cleanUp();
}
void IpcServer::setLogsEnabled(bool enabled)
{
if (enabled) {
Log::init();
}
else {
Log::deinit();
}
}

View file

@ -23,6 +23,8 @@ public:
virtual void resetIpStack() override; virtual void resetIpStack() override;
virtual bool checkAndInstallDriver() override; virtual bool checkAndInstallDriver() override;
virtual QStringList getTapList() override; virtual QStringList getTapList() override;
virtual void cleanUp() override;
virtual void setLogsEnabled(bool enabled) override;
private: private:
int m_localpid = 0; int m_localpid = 0;

View file

@ -9,7 +9,7 @@
QFile Log::m_file; QFile Log::m_file;
QTextStream Log::m_textStream; QTextStream Log::m_textStream;
QString Log::m_logFileName; QString Log::m_logFileName = QString("%1.log").arg(SERVICE_NAME);
void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{ {
@ -22,20 +22,20 @@ void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons
std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl << std::flush; std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl << std::flush;
} }
bool Log::initialize() bool Log::init()
{ {
if (m_file.isOpen()) return true;
QString path = Utils::systemLogPath(); QString path = Utils::systemLogPath();
QDir appDir(path); QDir appDir(path);
if (!appDir.mkpath(path)) { if (!appDir.mkpath(path)) {
return false; return false;
} }
m_logFileName = QString("%1.log").arg(SERVICE_NAME);
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}"); qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}");
m_file.setFileName(appDir.filePath(m_logFileName)); m_file.setFileName(appDir.filePath(m_logFileName));
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { if (!m_file.open(QIODevice::Append)) {
qWarning() << "Cannot open log file:" << m_logFileName; qWarning() << "Cannot open log file:" << m_logFileName;
return false; return false;
} }
@ -46,8 +46,59 @@ bool Log::initialize()
return true; return true;
} }
void Log::deinit()
{
m_file.close();
m_textStream.setDevice(nullptr);
qInstallMessageHandler(nullptr);
}
QString Log::serviceLogFileNamePath() QString Log::serviceLogFileNamePath()
{ {
return m_file.fileName(); return m_file.fileName();
} }
void Log::clearLogs()
{
bool isLogActive = m_file.isOpen();
m_file.close();
QString path = Utils::systemLogPath();
QDir appDir(path);
QFile file;
file.setFileName(appDir.filePath(m_logFileName));
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.resize(0);
file.close();
if (isLogActive) {
init();
}
}
void Log::cleanUp()
{
clearLogs();
deinit();
QString path = Utils::systemLogPath();
QDir appDir(path);
{
QFile file;
file.setFileName(appDir.filePath(m_logFileName));
file.remove();
}
{
QFile file;
file.setFileName(appDir.filePath("openvpn.log"));
file.remove();
}
#ifdef Q_OS_WINDOWS
QDir dir(Utils::systemLogPath());
dir.removeRecursively();
#endif
}

View file

@ -9,9 +9,14 @@
class Log class Log
{ {
public: public:
static bool initialize(); static bool init();
static void deinit();
static QString serviceLogFileNamePath(); static QString serviceLogFileNamePath();
static void clearLogs();
static void cleanUp();
private: private:
friend void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg); friend void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg);

View file

@ -20,7 +20,7 @@ int main(int argc, char **argv)
{ {
Utils::initializePath(Utils::systemLogPath()); Utils::initializePath(Utils::systemLogPath());
Log::initialize(); Log::init();
if (argc == 2) { if (argc == 2) {
qInfo() << "Started as console application"; qInfo() << "Started as console application";