Index: code/trunk/src/libraries/util/output/LogWriter.cc
===================================================================
--- code/trunk/src/libraries/util/output/LogWriter.cc	(revision 8858)
+++ code/trunk/src/libraries/util/output/LogWriter.cc	(revision 9550)
@@ -39,7 +39,10 @@
 #include "OutputManager.h"
 #include "MemoryWriter.h"
+#include "util/Convert.h"
 
 namespace orxonox
 {
+    static const int MAX_ARCHIVED_FILES = 9;
+
     /**
         @brief Constructor, initializes the desired output levels and the name and path of the log-file, and opens the log-file.
@@ -58,9 +61,11 @@
         // get the path for a temporary file, depending on the system
 #ifdef ORXONOX_PLATFORM_WINDOWS
-        this->path_ = getenv("TEMP");
+        this->directory_ = getenv("TEMP");
 #else
-        this->path_ = "/tmp";
+        this->directory_ = "/tmp";
 #endif
-        this->bDefaultPath_ = true;
+
+        // send a message to the user so that he can find the file in the case of a crash.
+        OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath());
 
         this->openFile();
@@ -76,26 +81,13 @@
 
     /**
-        @brief Returns the only existing instance of this class.
-    */
-    /*static*/ LogWriter& LogWriter::getInstance()
-    {
-        static LogWriter instance;
-        return instance;
-    }
-
-    /**
         @brief Opens the log-file in order to write output to it.
     */
     void LogWriter::openFile()
     {
-        // get the full file-name
-        std::string name = this->path_ + '/' + this->filename_;
-
-        // if we open the log file in the default directory, send a message to the user so that he can find the file in the case of a crash.
-        if (this->bDefaultPath_)
-            OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name);
+        // archive the old log file
+        this->archive();
 
         // open the file
-        this->file_.open(name.c_str(), std::fstream::out);
+        this->file_.open(this->getPath().c_str(), std::fstream::out);
 
         // check if it worked and print some output
@@ -119,19 +111,63 @@
 
     /**
+     * @brief Archives old copies of the log file by adding increasing numbers to the filename.
+     */
+    void LogWriter::archive(int index)
+    {
+        std::string oldPath = this->getArchivedPath(index);
+
+        // see if the file already exists, otherwise return
+        std::ifstream stream(oldPath.c_str());
+        bool exists = stream.is_open();
+        stream.close();
+
+        if (!exists)
+            return;
+
+        if (index < MAX_ARCHIVED_FILES)
+        {
+            // increment the index and archive the file with the next higher index
+            this->archive(++index);
+
+            // create the new path based on the incremented index
+            std::string newPath = this->getArchivedPath(index);
+
+            // move the file
+            std::rename(oldPath.c_str(), newPath.c_str());
+        }
+        else
+        {
+            // delete the file
+            std::remove(oldPath.c_str());
+        }
+    }
+    
+    /**
+     * @brief Returns the path for archived copies of the logfile (based on the archive index)
+     */
+    std::string LogWriter::getArchivedPath(int index) const
+    {
+        std::string path = this->getPath();
+        if (index > 0)
+            path += '.' + multi_cast<std::string>(index);
+        return path;
+    }
+
+    /**
         @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter.
     */
-    void LogWriter::setLogPath(const std::string& path)
+    void LogWriter::setLogDirectory(const std::string& directory)
     {
         // notify about the change of the log-file (because the old file will no longer be updated)
-        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path);
+        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory);
 
         // close the old file, update the path and open the new file
         this->closeFile();
-        this->path_ = path;
-        this->bDefaultPath_ = false;
+        this->directory_ = directory;
         this->openFile();
 
         // request old output from MemoryWriter
-        MemoryWriter::getInstance().resendOutput(this);
+        if (OutputManager::getInstance().getMemoryWriter())
+            OutputManager::getInstance().getMemoryWriter()->resendOutput(this);
     }
 
