changeset 703:7bd3689cd743

The logging class stores messages in a message queue if the logging file isn't initialized yet. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@972 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Wed, 01 Jul 2009 15:22:36 +0000
parents 8a8e96d6aad4
children b8bb1258b64e
files src/UiGuiLogger.cpp src/UiGuiLogger.h
diffstat 2 files changed, 26 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/UiGuiLogger.cpp	Mon Jun 29 21:51:03 2009 +0000
+++ b/src/UiGuiLogger.cpp	Wed Jul 01 15:22:36 2009 +0000
@@ -19,6 +19,7 @@
 
 #include "UiGuiLogger.h"
 
+#include <ctime>
 #include <QDateTime>
 #include <QFile>
 #include <QUrl>
@@ -52,6 +53,8 @@
     verboseLevel = QtWarningMsg;
 #endif
 
+    logFileInitState = NOTINITIALZED;
+
     connect( openLogFileFolderToolButton, SIGNAL(clicked()), this, SLOT(openLogFileFolder()) );
 
     // Make the main application not to wait for the logging window to close.
@@ -149,8 +152,10 @@
  */
 void UiGuiLogger::writeToLogFile(const QString message) {
 
-    QString testname = logFile.fileName();
-    if ( logFile.fileName().isEmpty() ) {
+    // If the file where all logging messages should go to isn't initilized yet, do that now.
+    if ( logFileInitState == NOTINITIALZED ) {
+        logFileInitState = INITIALIZING;
+
         // On different systems it may be that "QDir::tempPath()" ends with a "/" or not. So check this.
         // Remove any trailing slashes.
         QString tempPath = QFileInfo( SettingsPaths::getTempPath() ).absolutePath();
@@ -186,12 +191,23 @@
         // Set the tooltip of the open log file folder button to show the unique name of the log file.
         openLogFileFolderToolButton->setToolTip( openLogFileFolderToolButton->toolTip() + " (" + logFileName + ")" );
 
+        logFileInitState = INITIALZED;
     }
 
-    // Write/append the log message to the log file.
-    if ( logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append) ) {
-        QTextStream out(&logFile);
-        out << message << "\n";
-        logFile.close();
+    // Add the message to the message queue.
+    messageQueue << message;
+
+    // If the logging file is initialzed, write all messages contained in the message queue into the file.
+    if ( logFileInitState == INITIALZED ) {
+        // Write/append the log message to the log file.
+        if ( logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append) ) {
+            QTextStream out(&logFile);
+
+            while ( !messageQueue.isEmpty() ) {
+                out << messageQueue.takeFirst() << "\n";
+            }
+
+            logFile.close();
+        }
     }
 }
--- a/src/UiGuiLogger.h	Mon Jun 29 21:51:03 2009 +0000
+++ b/src/UiGuiLogger.h	Wed Jul 01 15:22:36 2009 +0000
@@ -24,6 +24,7 @@
 
 #include <QDialog>
 #include <QFile>
+#include <QStringList>
 
 #include "ui_UiGuiLoggerDialog.h"
 
@@ -50,12 +51,14 @@
     void setVerboseLevel(int level);
 
 private:
+    enum LogFileInitState { NOTINITIALZED, INITIALIZING, INITIALZED } logFileInitState;
     UiGuiLogger();
     void writeToLogFile(const QString message);
 
     static UiGuiLogger* instance;
     QtMsgType verboseLevel;
     QFile logFile;
+    QStringList messageQueue;
 
 private slots:
     void openLogFileFolder();