Files
DefaultVPN/client/debug.cpp

78 lines
1.8 KiB
C++
Raw Normal View History

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>
2020-12-16 06:02:22 +03:00
#include "debug.h"
#include "defines.h"
2020-11-23 16:20:25 +03:00
2020-12-16 06:02:22 +03:00
QFile Debug::m_file;
QTextStream Debug::m_textStream;
QString Debug::m_logFileName;
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;
}
// Temporally disabled
if (msg.startsWith("Unknown property") || msg.startsWith("Could not create pixmap")) {
return;
}
2020-11-23 16:20:25 +03:00
2020-12-16 06:02:22 +03:00
Debug::m_textStream << qFormatLogMessage(type, context, msg) << endl << 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
}
bool Debug::init()
{
2020-12-16 06:02:22 +03:00
QString path = logsDir();
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_logFileName = QString("%1.log").arg(APPLICATION_NAME);
2020-11-23 16:20:25 +03:00
2020-12-26 15:03:51 +03:00
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss} %{type} %{message}");
2020-11-23 16:20:25 +03:00
2020-12-16 06:02:22 +03:00
m_file.setFileName(appDir.filePath(m_logFileName));
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
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);
2020-11-23 16:20:25 +03:00
qInstallMessageHandler(debugMessageHandler);
return true;
}
2020-12-16 06:02:22 +03:00
QString Debug::logsDir()
2020-11-23 16:20:25 +03:00
{
2020-12-16 06:02:22 +03:00
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/logs";
2020-11-23 16:20:25 +03:00
}
2020-12-16 06:02:22 +03:00
bool Debug::openLogsFolder()
2020-11-23 16:20:25 +03:00
{
2020-12-16 06:02:22 +03:00
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;
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
QString Debug::appLogFileNamePath()
{
return m_file.fileName();
}