[Orxonox-commit 4869] r9538 - in code/branches/testing: src/libraries/core src/libraries/util/output test/util/output

landauf at orxonox.net landauf at orxonox.net
Fri Mar 8 23:16:57 CET 2013


Author: landauf
Date: 2013-03-08 23:16:56 +0100 (Fri, 08 Mar 2013)
New Revision: 9538

Modified:
   code/branches/testing/src/libraries/core/Core.cc
   code/branches/testing/src/libraries/util/output/LogWriter.cc
   code/branches/testing/src/libraries/util/output/LogWriter.h
   code/branches/testing/test/util/output/ConsoleWriterTest.cc
   code/branches/testing/test/util/output/LogWriterTest.cc
Log:
small refactoring in LogWriter and tests added

Modified: code/branches/testing/src/libraries/core/Core.cc
===================================================================
--- code/branches/testing/src/libraries/core/Core.cc	2013-03-04 22:23:57 UTC (rev 9537)
+++ code/branches/testing/src/libraries/core/Core.cc	2013-03-08 22:16:56 UTC (rev 9538)
@@ -180,7 +180,7 @@
         this->setConfigValues();
 
         // Set the correct log path and rewrite the log file with the correct log levels
-        OutputManager::getInstance().getLogWriter().setLogPath(PathConfig::getLogPathString());
+        OutputManager::getInstance().getLogWriter().setLogDirectory(PathConfig::getLogPathString());
 
 #if !defined(ORXONOX_PLATFORM_APPLE) && !defined(ORXONOX_USE_WINMAIN)
         // Create persistent IO console

Modified: code/branches/testing/src/libraries/util/output/LogWriter.cc
===================================================================
--- code/branches/testing/src/libraries/util/output/LogWriter.cc	2013-03-04 22:23:57 UTC (rev 9537)
+++ code/branches/testing/src/libraries/util/output/LogWriter.cc	2013-03-08 22:16:56 UTC (rev 9538)
@@ -57,12 +57,14 @@
 
         // 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();
     }
 
@@ -79,15 +81,8 @@
     */
     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);
-
         // 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
         if (this->file_.is_open())
@@ -111,15 +106,14 @@
     /**
         @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

Modified: code/branches/testing/src/libraries/util/output/LogWriter.h
===================================================================
--- code/branches/testing/src/libraries/util/output/LogWriter.h	2013-03-04 22:23:57 UTC (rev 9537)
+++ code/branches/testing/src/libraries/util/output/LogWriter.h	2013-03-08 22:16:56 UTC (rev 9538)
@@ -60,8 +60,15 @@
             LogWriter(const LogWriter&);
             virtual ~LogWriter();
 
-            void setLogPath(const std::string& path);
+            void setLogDirectory(const std::string& directory);
 
+            /** @brief Returns the path to the logfile. */
+            inline std::string getPath() const
+                { return this->directory_ + '/' + this->filename_; }
+            /** @brief Returns the open file stream. */
+            inline const std::ofstream& getFile() const
+                { return this->file_; }
+
         protected:
             virtual void printLine(const std::string& line, OutputLevel level);
 
@@ -69,10 +76,8 @@
             void openFile();
             void closeFile();
 
-            std::string filename_;  ///< The name of the log-file (without directories)
-            std::string path_;      ///< The path of the log-file (without file-name)
-            bool bDefaultPath_;     ///< If true, the log-file resides at the default path (which is usually a temporary directory)
-
+            std::string filename_;  ///< The name of the log-file (without directory)
+            std::string directory_; ///< The directory where the log-file resided (without file-name)
             std::ofstream file_;    ///< The output file stream.
     };
 }

Modified: code/branches/testing/test/util/output/ConsoleWriterTest.cc
===================================================================
--- code/branches/testing/test/util/output/ConsoleWriterTest.cc	2013-03-04 22:23:57 UTC (rev 9537)
+++ code/branches/testing/test/util/output/ConsoleWriterTest.cc	2013-03-08 22:16:56 UTC (rev 9538)
@@ -2,11 +2,15 @@
 #include "util/Output.h"
 #include "util/output/ConsoleWriter.h"
 #include "util/output/OutputManager.h"
+#include "util/SharedPtr.h"
 
 namespace orxonox
 {
     TEST(ConsoleWriterTest, Disable)
     {
+        // reset output manager
+        OutputManager::Testing::getInstancePointer() = new OutputManager();
+
         std::ostream stream(NULL);
         EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size());
         ConsoleWriter writer(stream);
@@ -17,6 +21,9 @@
 
     TEST(ConsoleWriterTest, Enable)
     {
+        // reset output manager
+        OutputManager::Testing::getInstancePointer() = new OutputManager();
+
         std::ostream stream(NULL);
         ConsoleWriter writer(stream);
         writer.disable();

Modified: code/branches/testing/test/util/output/LogWriterTest.cc
===================================================================
--- code/branches/testing/test/util/output/LogWriterTest.cc	2013-03-04 22:23:57 UTC (rev 9537)
+++ code/branches/testing/test/util/output/LogWriterTest.cc	2013-03-08 22:16:56 UTC (rev 9538)
@@ -1,6 +1,132 @@
 #include <gtest/gtest.h>
 #include "util/Output.h"
+#include "util/output/LogWriter.h"
 
 namespace orxonox
 {
+    // test constructor opens file
+    TEST(LogWriterTest, ConstructorOpensFile)
+    {
+        LogWriter logWriter;
+        EXPECT_TRUE(logWriter.getFile().is_open());
+    }
+
+    // setLogDirectory changes file, opens correct file
+    bool fileExists(const std::string& path)
+    {
+        std::ifstream stream(path.c_str(), std::fstream::in);
+        bool exists = stream.is_open();
+        stream.close();
+        return exists;
+    }
+
+    bool fileSuccessfullyDeleted(const std::string& path)
+    {
+        return std::remove(path.c_str()) == 0;
+    }
+
+    TEST(LogWriterTest, SetLogDirectoryOpensNewFile)
+    {
+        std::string path = "./orxonox.log";
+
+        // cleanup before test
+        fileSuccessfullyDeleted(path);
+
+        {
+            LogWriter logWriter;
+            EXPECT_FALSE(fileExists(path));
+            logWriter.setLogDirectory(".");
+            EXPECT_TRUE(fileExists(path));
+        }
+
+        // cleanup after test
+        EXPECT_TRUE(fileSuccessfullyDeleted(path));
+    }
+
+    // prints output to logfile
+    TEST(LogWriterTest, PrintsOutputToLogfile)
+    {
+        std::string path;
+
+        {
+            // write lines to log file
+            std::vector<std::string> lines;
+            lines.push_back("mytestoutput");
+
+            LogWriter logWriter;
+            logWriter.setLogDirectory(".");
+            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
+
+            path = logWriter.getPath();
+        }
+
+        {
+            std::ifstream file(path.c_str(), std::fstream::in);
+            ASSERT_TRUE(file.is_open());
+            ASSERT_TRUE(file.good());
+            ASSERT_FALSE(file.eof());
+
+            while (!file.eof())
+            {
+                std::string line;
+                std::getline(file, line);
+
+                // see if we find the output and return
+                if (line.find("mytestoutput") != std::string::npos)
+                    return;
+            }
+
+            // output not found - failure!
+            FAIL();
+        }
+    }
+
+    // prints time to logfile
+    TEST(LogWriterTest, PrintsTimestampToLogfile)
+    {
+        std::string path;
+
+        {
+            // write lines to log file
+            std::vector<std::string> lines;
+            lines.push_back("myothertestoutput");
+
+            LogWriter logWriter;
+            logWriter.setLogDirectory(".");
+            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
+
+            path = logWriter.getPath();
+        }
+
+        {
+            std::ifstream file(path.c_str(), std::fstream::in);
+            ASSERT_TRUE(file.is_open());
+            ASSERT_TRUE(file.good());
+            ASSERT_FALSE(file.eof());
+
+            while (!file.eof())
+            {
+                std::string line;
+                std::getline(file, line);
+
+                // see if we find the output and return
+                if (line.find("myothertestoutput") != std::string::npos)
+                {
+                    EXPECT_TRUE(std::isdigit(line[0]));
+                    EXPECT_TRUE(std::isdigit(line[1]));
+                    EXPECT_EQ(':', line[2]);
+                    EXPECT_TRUE(std::isdigit(line[3]));
+                    EXPECT_TRUE(std::isdigit(line[4]));
+                    EXPECT_EQ(':', line[5]);
+                    EXPECT_TRUE(std::isdigit(line[6]));
+                    EXPECT_TRUE(std::isdigit(line[7]));
+                    EXPECT_EQ(' ', line[8]);
+                    return;
+                }
+            }
+
+            // output not found - failure!
+            FAIL();
+        }
+    }
 }




More information about the Orxonox-commit mailing list