[Orxonox-commit 3841] r8517 - in code/branches/unity_build/src/libraries: core util

rgrieder at orxonox.net rgrieder at orxonox.net
Fri May 20 05:18:09 CEST 2011


Author: rgrieder
Date: 2011-05-20 05:18:08 +0200 (Fri, 20 May 2011)
New Revision: 8517

Modified:
   code/branches/unity_build/src/libraries/core/Core.cc
   code/branches/unity_build/src/libraries/util/OutputHandler.cc
   code/branches/unity_build/src/libraries/util/OutputHandler.h
Log:
Improved logging to a file by rewriting the log according to the right debug level. Also, failure to open the file is now being handled.

Modified: code/branches/unity_build/src/libraries/core/Core.cc
===================================================================
--- code/branches/unity_build/src/libraries/core/Core.cc	2011-05-20 01:58:29 UTC (rev 8516)
+++ code/branches/unity_build/src/libraries/core/Core.cc	2011-05-20 03:18:08 UTC (rev 8517)
@@ -167,6 +167,8 @@
         // possibility to configure everything below here
         RegisterRootObject(Core);
         this->setConfigValues();
+        // Rewrite the log file with the correct log levels
+        OutputHandler::getInstance().rewriteLogFile();
 
 #if !defined(ORXONOX_PLATFORM_APPLE) && !defined(ORXONOX_USE_WINMAIN)
         // Create persistent IO console
@@ -239,7 +241,7 @@
 #endif
         SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile)
             .description("The maximum level of debug output shown in the log file");
-        OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
+        OutputHandler::getInstance().setSoftDebugLevel("LogFile", this->softDebugLevelLogFile_);
 
         SetConfigValue(bDevMode_, PathConfig::buildDirectoryRun())
             .description("Developer mode. If not set, hides some things from the user to not confuse him.");

Modified: code/branches/unity_build/src/libraries/util/OutputHandler.cc
===================================================================
--- code/branches/unity_build/src/libraries/util/OutputHandler.cc	2011-05-20 01:58:29 UTC (rev 8516)
+++ code/branches/unity_build/src/libraries/util/OutputHandler.cc	2011-05-20 03:18:08 UTC (rev 8517)
@@ -68,7 +68,7 @@
             Gets temporary log path and starts the log file
         */
         LogFileWriter()
-            : OutputListener(OutputHandler::logFileOutputListenerName_s)
+            : OutputListener("LogFile")
         {
             // Get path for a temporary file
 #ifdef ORXONOX_PLATFORM_WINDOWS
@@ -84,35 +84,59 @@
             time(&rawtime);
             timeinfo = localtime(&rawtime);
 
-            this->logFile_.open(this->logFilename_.c_str(), std::fstream::out);
-            this->logFile_ << "Started log on " << asctime(timeinfo) << std::endl;
-            this->logFile_.flush();
-
-            this->outputStream_ = &this->logFile_;
+            this->openFile();
+            if (this->logFile_.is_open())
+            {
+                this->logFile_ << "Started log on " << asctime(timeinfo) << std::endl;
+                this->logFile_.flush();
+            }
         }
 
         //! Closes the log file
         ~LogFileWriter()
         {
-            this->logFile_ << "Closed log" << std::endl;
-            this->logFile_.close();
+            if (this->logFile_.is_open())
+            {
+                this->logFile_ << "Closed log" << std::endl;
+                this->logFile_.close();
+            }
         }
 
         //! Changes the log path
         void setLogPath(const std::string& path)
         {
-            this->logFile_.close();
-            // Read old file into a buffer
-            std::ifstream old(this->logFilename_.c_str());
+            if (this->logFile_.is_open())
+                this->logFile_.close();
+
+            // Open the new file
             this->logFilename_ = path + logFileBaseName_g;
-            // Open the new file and feed it the content of the old one
+            this->openFile();
+        }
+
+        //! Erases the log file
+        void clearFile()
+        {
+            if (this->logFile_.is_open())
+            {
+                this->logFile_.close();
+                this->openFile();
+            }
+        }
+
+    private:
+        void openFile()
+        {
             this->logFile_.open(this->logFilename_.c_str(), std::fstream::out);
-            this->logFile_ << old.rdbuf();
-            this->logFile_.flush();
-            old.close();
+
+            if (this->logFile_.is_open())
+                this->outputStream_ = &this->logFile_;
+            else
+            {
+                COUT(2) << "Warning: Failed to open log file. File logging disabled." << std::endl;
+                this->outputStream_ = NULL;
+            }
         }
 
-    private:
         std::ofstream logFile_;     //!< File handle for the log file
         std::string   logFilename_; //!< Filename of the log file
     };
@@ -184,8 +208,7 @@
     /////////////////////////
     ///// OutputHandler /////
     /////////////////////////
-    const std::string OutputHandler::logFileOutputListenerName_s = "logFile";
-          int         OutputHandler::softDebugLevel_s = hardDebugLevel;
+    int OutputHandler::softDebugLevel_s = hardDebugLevel;
 
     //! Creates the LogFileWriter and the MemoryLogWriter
     OutputHandler::OutputHandler()
@@ -258,8 +281,24 @@
     void OutputHandler::setLogPath(const std::string& path)
     {
         this->logFile_->setLogPath(path);
+        this->rewriteLogFile();
     }
 
+    void OutputHandler::rewriteLogFile()
+    {
+        logFile_->clearFile();
+
+        if (logFile_->outputStream_ == NULL)
+            return;
+
+        for (OutputVector::const_iterator it = this->getOutput().begin(); it != this->getOutput().end(); ++it)
+        {
+            if (it->first <= logFile_->softDebugLevel_)
+                (*logFile_->outputStream_) << it->second;
+        }
+        logFile_->outputStream_->flush();
+    }
+
     void OutputHandler::disableCout()
     {
         this->unregisterOutputListener(this->consoleWriter_);

Modified: code/branches/unity_build/src/libraries/util/OutputHandler.h
===================================================================
--- code/branches/unity_build/src/libraries/util/OutputHandler.h	2011-05-20 01:58:29 UTC (rev 8516)
+++ code/branches/unity_build/src/libraries/util/OutputHandler.h	2011-05-20 03:18:08 UTC (rev 8517)
@@ -132,6 +132,11 @@
 
             //! Set the log path once the program has been properly initialised
             void setLogPath(const std::string& path);
+            /** Rewrites the log file (completely respects the current debug level).
+                Once disableMemoryLog() has been called, this function will do nothing.
+            */
+            void rewriteLogFile();
+
             //! Disables the std::cout stream for output
             void disableCout();
             //! Enables the std::cout stream for output (startup behaviour)
@@ -211,9 +216,6 @@
             //! Dummy operator required by Debug.h for the ternary operator
             inline operator int() const { return 0; }
 
-            //! Name of the OutputListener that writes to the log file
-            static const std::string logFileOutputListenerName_s;
-
         private:
             OutputHandler();
             ~OutputHandler();




More information about the Orxonox-commit mailing list