diff --git a/src/main.cpp b/src/main.cpp index c1f1add5..f2734602 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,20 @@ VConfigManager *g_config; #if defined(QT_NO_DEBUG) +// 5MB log size. +#define MAX_LOG_SIZE 5 * 1024 * 1024 + QFile g_logFile; + +static void initLogFile(const QString &p_file) +{ + g_logFile.setFileName(p_file); + if (g_logFile.size() >= MAX_LOG_SIZE) { + g_logFile.open(QIODevice::WriteOnly | QIODevice::Text); + } else { + g_logFile.open(QIODevice::Append | QIODevice::Text); + } +} #endif void VLogger(QtMsgType type, const QMessageLogContext &context, const QString &msg) @@ -50,11 +63,7 @@ void VLogger(QtMsgType type, const QMessageLogContext &context, const QString &m QTextStream stream(&g_logFile); -#if defined(Q_OS_WIN) - stream << header << localMsg << "\r\n"; -#else stream << header << localMsg << "\n"; -#endif if (type == QtFatalMsg) { g_logFile.close(); @@ -97,17 +106,6 @@ int main(int argc, char *argv[]) VSingleInstanceGuard guard; bool canRun = guard.tryRun(); -#if defined(QT_NO_DEBUG) - if (canRun) { - g_logFile.setFileName(VConfigManager::getLogFilePath()); - g_logFile.open(QIODevice::WriteOnly); - } -#endif - - if (canRun) { - qInstallMessageHandler(VLogger); - } - QTextCodec *codec = QTextCodec::codecForName("UTF8"); if (codec) { QTextCodec::setCodecForLocale(codec); @@ -115,16 +113,9 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); - // Check the openSSL. - qDebug() << "openSSL" << QSslSocket::sslLibraryBuildVersionString() - << QSslSocket::sslLibraryVersionNumber(); - // The file path passed via command line arguments. QStringList filePaths = VUtils::filterFilePathsToOpen(app.arguments().mid(1)); - qDebug() << "command line arguments" << app.arguments(); - qDebug() << "files to open from arguments" << filePaths; - if (!canRun) { // Ask another instance to open files passed in. if (!filePaths.isEmpty()) { @@ -140,12 +131,25 @@ int main(int argc, char *argv[]) vconfig.initialize(); g_config = &vconfig; +#if defined(QT_NO_DEBUG) + initLogFile(vconfig.getLogFilePath()); +#endif + + qInstallMessageHandler(VLogger); + QString locale = VUtils::getLocale(); // Set default locale. if (locale == "zh_CN") { QLocale::setDefault(QLocale(QLocale::Chinese, QLocale::China)); } + qDebug() << "command line arguments" << app.arguments(); + qDebug() << "files to open from arguments" << filePaths; + + // Check the openSSL. + qDebug() << "openSSL" << QSslSocket::sslLibraryBuildVersionString() + << QSslSocket::sslLibraryVersionNumber(); + // load translation for Qt QTranslator qtTranslator; if (!qtTranslator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 7143892b..cbe27894 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -535,19 +535,9 @@ bool VConfigManager::deleteDirectoryConfig(const QString &path) return true; } -QString VConfigManager::getLogFilePath() +QString VConfigManager::getLogFilePath() const { - 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; + return QDir(getConfigFolder()).filePath("vnote.log"); } void VConfigManager::updateMarkdownEditStyle() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 6f32ff45..bf291299 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -71,8 +71,6 @@ public: static bool directoryConfigExist(const QString &path); static bool deleteDirectoryConfig(const QString &path); - static QString getLogFilePath(); - // Get the path of the folder used to store default notebook. static QString getVnoteNotebookFolderPath(); @@ -100,6 +98,8 @@ public: QString getWelcomePagePath() const; + QString getLogFilePath() const; + QString getTemplateCssUrl(); QString getTemplateCodeBlockCssUrl();