소스 검색

refactor log file

- Append to existing log file when the file size does not exceed the limit;
- Put log file beside the vnote.ini configuration file;
Le Tan 8 년 전
부모
커밋
d739fa5de8
3개의 변경된 파일30개의 추가작업 그리고 36개의 파일을 삭제
  1. 26 22
      src/main.cpp
  2. 2 12
      src/vconfigmanager.cpp
  3. 2 2
      src/vconfigmanager.h

+ 26 - 22
src/main.cpp

@@ -16,7 +16,20 @@
 VConfigManager *g_config;
 VConfigManager *g_config;
 
 
 #if defined(QT_NO_DEBUG)
 #if defined(QT_NO_DEBUG)
+// 5MB log size.
+#define MAX_LOG_SIZE 5 * 1024 * 1024
+
 QFile g_logFile;
 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
 #endif
 
 
 void VLogger(QtMsgType type, const QMessageLogContext &context, const QString &msg)
 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);
     QTextStream stream(&g_logFile);
 
 
-#if defined(Q_OS_WIN)
-    stream << header << localMsg << "\r\n";
-#else
     stream << header << localMsg << "\n";
     stream << header << localMsg << "\n";
-#endif
 
 
     if (type == QtFatalMsg) {
     if (type == QtFatalMsg) {
         g_logFile.close();
         g_logFile.close();
@@ -97,17 +106,6 @@ int main(int argc, char *argv[])
     VSingleInstanceGuard guard;
     VSingleInstanceGuard guard;
     bool canRun = guard.tryRun();
     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");
     QTextCodec *codec = QTextCodec::codecForName("UTF8");
     if (codec) {
     if (codec) {
         QTextCodec::setCodecForLocale(codec);
         QTextCodec::setCodecForLocale(codec);
@@ -115,16 +113,9 @@ int main(int argc, char *argv[])
 
 
     QApplication app(argc, argv);
     QApplication app(argc, argv);
 
 
-    // Check the openSSL.
-    qDebug() << "openSSL" << QSslSocket::sslLibraryBuildVersionString()
-             << QSslSocket::sslLibraryVersionNumber();
-
     // The file path passed via command line arguments.
     // The file path passed via command line arguments.
     QStringList filePaths = VUtils::filterFilePathsToOpen(app.arguments().mid(1));
     QStringList filePaths = VUtils::filterFilePathsToOpen(app.arguments().mid(1));
 
 
-    qDebug() << "command line arguments" << app.arguments();
-    qDebug() << "files to open from arguments" << filePaths;
-
     if (!canRun) {
     if (!canRun) {
         // Ask another instance to open files passed in.
         // Ask another instance to open files passed in.
         if (!filePaths.isEmpty()) {
         if (!filePaths.isEmpty()) {
@@ -140,12 +131,25 @@ int main(int argc, char *argv[])
     vconfig.initialize();
     vconfig.initialize();
     g_config = &vconfig;
     g_config = &vconfig;
 
 
+#if defined(QT_NO_DEBUG)
+    initLogFile(vconfig.getLogFilePath());
+#endif
+
+    qInstallMessageHandler(VLogger);
+
     QString locale = VUtils::getLocale();
     QString locale = VUtils::getLocale();
     // Set default locale.
     // Set default locale.
     if (locale == "zh_CN") {
     if (locale == "zh_CN") {
         QLocale::setDefault(QLocale(QLocale::Chinese, QLocale::China));
         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
     // load translation for Qt
     QTranslator qtTranslator;
     QTranslator qtTranslator;
     if (!qtTranslator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
     if (!qtTranslator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {

+ 2 - 12
src/vconfigmanager.cpp

@@ -535,19 +535,9 @@ bool VConfigManager::deleteDirectoryConfig(const QString &path)
     return true;
     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()
 void VConfigManager::updateMarkdownEditStyle()

+ 2 - 2
src/vconfigmanager.h

@@ -71,8 +71,6 @@ public:
     static bool directoryConfigExist(const QString &path);
     static bool directoryConfigExist(const QString &path);
     static bool deleteDirectoryConfig(const QString &path);
     static bool deleteDirectoryConfig(const QString &path);
 
 
-    static QString getLogFilePath();
-
     // Get the path of the folder used to store default notebook.
     // Get the path of the folder used to store default notebook.
     static QString getVnoteNotebookFolderPath();
     static QString getVnoteNotebookFolderPath();
 
 
@@ -100,6 +98,8 @@ public:
 
 
     QString getWelcomePagePath() const;
     QString getWelcomePagePath() const;
 
 
+    QString getLogFilePath() const;
+
     QString getTemplateCssUrl();
     QString getTemplateCssUrl();
 
 
     QString getTemplateCodeBlockCssUrl();
     QString getTemplateCodeBlockCssUrl();