mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
support log file in release mode
This commit is contained in:
parent
d1395b9ac2
commit
db9903b906
62
src/main.cpp
62
src/main.cpp
@ -5,37 +5,87 @@
|
||||
#include <QLibraryInfo>
|
||||
#include <QFile>
|
||||
#include <QTextCodec>
|
||||
#include <QFileInfo>
|
||||
#include "utils/vutils.h"
|
||||
#include "vsingleinstanceguard.h"
|
||||
#include "vconfigmanager.h"
|
||||
|
||||
VConfigManager vconfig;
|
||||
static QFile g_logFile;
|
||||
|
||||
void VLogger(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
QByteArray localMsg = msg.toUtf8();
|
||||
QString header;
|
||||
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
fprintf(stderr, "Debug:%s (%s:%u)\n", localMsg.constData(), context.file, context.line);
|
||||
header = "Debug:";
|
||||
break;
|
||||
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "Info:%s (%s:%u)\n", localMsg.constData(), context.file, context.line);
|
||||
header = "Info:";
|
||||
break;
|
||||
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "Warning:%s (%s:%u)\n", localMsg.constData(), context.file, context.line);
|
||||
header = "Warning:";
|
||||
break;
|
||||
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "Critical:%s (%s:%u)\n", localMsg.constData(), context.file, context.line);
|
||||
header = "Critical:";
|
||||
break;
|
||||
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "Fatal:%s (%s:%u)\n", localMsg.constData(), context.file, context.line);
|
||||
header = "Fatal:";
|
||||
}
|
||||
|
||||
#if defined(QT_NO_DEBUG)
|
||||
QTextStream stream(&g_logFile);
|
||||
stream << header << localMsg << "\n";
|
||||
g_logFile.flush();
|
||||
|
||||
if (type == QtFatalMsg) {
|
||||
g_logFile.close();
|
||||
abort();
|
||||
}
|
||||
|
||||
#else
|
||||
std::string fileStr = QFileInfo(context.file).fileName().toStdString();
|
||||
const char *file = fileStr.c_str();
|
||||
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
fprintf(stderr, "%s(%s:%u) %s\n",
|
||||
header.toStdString().c_str(), file, context.line, localMsg.constData());
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "%s(%s:%u) %s\n",
|
||||
header.toStdString().c_str(), file, context.line, localMsg.constData());
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "%s(%s:%u) %s\n",
|
||||
header.toStdString().c_str(), file, context.line, localMsg.constData());
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "%s(%s:%u) %s\n",
|
||||
header.toStdString().c_str(), file, context.line, localMsg.constData());
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "%s(%s:%u) %s\n",
|
||||
header.toStdString().c_str(), file, context.line, localMsg.constData());
|
||||
abort();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//qInstallMessageHandler(VLogger);
|
||||
#if defined(QT_NO_DEBUG)
|
||||
g_logFile.setFileName(VConfigManager::getLogFilePath());
|
||||
g_logFile.open(QIODevice::WriteOnly);
|
||||
#endif
|
||||
|
||||
qInstallMessageHandler(VLogger);
|
||||
|
||||
VSingleInstanceGuard guard;
|
||||
if (!guard.tryRun()) {
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QtDebug>
|
||||
#include <QTextEdit>
|
||||
#include <QStandardPaths>
|
||||
#include "utils/vutils.h"
|
||||
#include "vstyleparser.h"
|
||||
|
||||
@ -276,6 +277,21 @@ bool VConfigManager::deleteDirectoryConfig(const QString &path)
|
||||
return true;
|
||||
}
|
||||
|
||||
QString VConfigManager::getLogFilePath()
|
||||
{
|
||||
static QString logPath;
|
||||
|
||||
if (logPath.isEmpty()) {
|
||||
QString location = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
V_ASSERT(!location.isEmpty());
|
||||
QDir dir(location);
|
||||
dir.mkdir("VNote");
|
||||
logPath = dir.filePath("VNote/vnote.log");
|
||||
}
|
||||
|
||||
return logPath;
|
||||
}
|
||||
|
||||
void VConfigManager::updateMarkdownEditStyle()
|
||||
{
|
||||
static const QString defaultCurrentLineBackground = "#C5CAE9";
|
||||
|
@ -39,6 +39,8 @@ public:
|
||||
static bool directoryConfigExist(const QString &path);
|
||||
static bool deleteDirectoryConfig(const QString &path);
|
||||
|
||||
static QString getLogFilePath();
|
||||
|
||||
// Constants
|
||||
static const QString orgName;
|
||||
static const QString appName;
|
||||
|
Loading…
x
Reference in New Issue
Block a user