Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9538


Ignore:
Timestamp:
Mar 8, 2013, 11:16:56 PM (12 years ago)
Author:
landauf
Message:

small refactoring in LogWriter and tests added

Location:
code/branches/testing
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/testing/src/libraries/core/Core.cc

    r9536 r9538  
    181181
    182182        // Set the correct log path and rewrite the log file with the correct log levels
    183         OutputManager::getInstance().getLogWriter().setLogPath(PathConfig::getLogPathString());
     183        OutputManager::getInstance().getLogWriter().setLogDirectory(PathConfig::getLogPathString());
    184184
    185185#if !defined(ORXONOX_PLATFORM_APPLE) && !defined(ORXONOX_USE_WINMAIN)
  • code/branches/testing/src/libraries/util/output/LogWriter.cc

    r9536 r9538  
    5858        // get the path for a temporary file, depending on the system
    5959#ifdef ORXONOX_PLATFORM_WINDOWS
    60         this->path_ = getenv("TEMP");
     60        this->directory_ = getenv("TEMP");
    6161#else
    62         this->path_ = "/tmp";
     62        this->directory_ = "/tmp";
    6363#endif
    64         this->bDefaultPath_ = true;
     64
     65        // send a message to the user so that he can find the file in the case of a crash.
     66        OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath());
    6567
    6668        this->openFile();
     
    8082    void LogWriter::openFile()
    8183    {
    82         // get the full file-name
    83         std::string name = this->path_ + '/' + this->filename_;
    84 
    85         // 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.
    86         if (this->bDefaultPath_)
    87             OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name);
    88 
    8984        // open the file
    90         this->file_.open(name.c_str(), std::fstream::out);
     85        this->file_.open(this->getPath().c_str(), std::fstream::out);
    9186
    9287        // check if it worked and print some output
     
    112107        @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter.
    113108    */
    114     void LogWriter::setLogPath(const std::string& path)
     109    void LogWriter::setLogDirectory(const std::string& directory)
    115110    {
    116111        // notify about the change of the log-file (because the old file will no longer be updated)
    117         OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path);
     112        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory);
    118113
    119114        // close the old file, update the path and open the new file
    120115        this->closeFile();
    121         this->path_ = path;
    122         this->bDefaultPath_ = false;
     116        this->directory_ = directory;
    123117        this->openFile();
    124118
  • code/branches/testing/src/libraries/util/output/LogWriter.h

    r9536 r9538  
    6161            virtual ~LogWriter();
    6262
    63             void setLogPath(const std::string& path);
     63            void setLogDirectory(const std::string& directory);
     64
     65            /** @brief Returns the path to the logfile. */
     66            inline std::string getPath() const
     67                { return this->directory_ + '/' + this->filename_; }
     68            /** @brief Returns the open file stream. */
     69            inline const std::ofstream& getFile() const
     70                { return this->file_; }
    6471
    6572        protected:
     
    7077            void closeFile();
    7178
    72             std::string filename_;  ///< The name of the log-file (without directories)
    73             std::string path_;      ///< The path of the log-file (without file-name)
    74             bool bDefaultPath_;     ///< If true, the log-file resides at the default path (which is usually a temporary directory)
    75 
     79            std::string filename_;  ///< The name of the log-file (without directory)
     80            std::string directory_; ///< The directory where the log-file resided (without file-name)
    7681            std::ofstream file_;    ///< The output file stream.
    7782    };
  • code/branches/testing/test/util/output/ConsoleWriterTest.cc

    r9537 r9538  
    33#include "util/output/ConsoleWriter.h"
    44#include "util/output/OutputManager.h"
     5#include "util/SharedPtr.h"
    56
    67namespace orxonox
     
    89    TEST(ConsoleWriterTest, Disable)
    910    {
     11        // reset output manager
     12        OutputManager::Testing::getInstancePointer() = new OutputManager();
     13
    1014        std::ostream stream(NULL);
    1115        EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size());
     
    1822    TEST(ConsoleWriterTest, Enable)
    1923    {
     24        // reset output manager
     25        OutputManager::Testing::getInstancePointer() = new OutputManager();
     26
    2027        std::ostream stream(NULL);
    2128        ConsoleWriter writer(stream);
  • code/branches/testing/test/util/output/LogWriterTest.cc

    r9529 r9538  
    11#include <gtest/gtest.h>
    22#include "util/Output.h"
     3#include "util/output/LogWriter.h"
    34
    45namespace orxonox
    56{
     7    // test constructor opens file
     8    TEST(LogWriterTest, ConstructorOpensFile)
     9    {
     10        LogWriter logWriter;
     11        EXPECT_TRUE(logWriter.getFile().is_open());
     12    }
     13
     14    // setLogDirectory changes file, opens correct file
     15    bool fileExists(const std::string& path)
     16    {
     17        std::ifstream stream(path.c_str(), std::fstream::in);
     18        bool exists = stream.is_open();
     19        stream.close();
     20        return exists;
     21    }
     22
     23    bool fileSuccessfullyDeleted(const std::string& path)
     24    {
     25        return std::remove(path.c_str()) == 0;
     26    }
     27
     28    TEST(LogWriterTest, SetLogDirectoryOpensNewFile)
     29    {
     30        std::string path = "./orxonox.log";
     31
     32        // cleanup before test
     33        fileSuccessfullyDeleted(path);
     34
     35        {
     36            LogWriter logWriter;
     37            EXPECT_FALSE(fileExists(path));
     38            logWriter.setLogDirectory(".");
     39            EXPECT_TRUE(fileExists(path));
     40        }
     41
     42        // cleanup after test
     43        EXPECT_TRUE(fileSuccessfullyDeleted(path));
     44    }
     45
     46    // prints output to logfile
     47    TEST(LogWriterTest, PrintsOutputToLogfile)
     48    {
     49        std::string path;
     50
     51        {
     52            // write lines to log file
     53            std::vector<std::string> lines;
     54            lines.push_back("mytestoutput");
     55
     56            LogWriter logWriter;
     57            logWriter.setLogDirectory(".");
     58            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
     59
     60            path = logWriter.getPath();
     61        }
     62
     63        {
     64            std::ifstream file(path.c_str(), std::fstream::in);
     65            ASSERT_TRUE(file.is_open());
     66            ASSERT_TRUE(file.good());
     67            ASSERT_FALSE(file.eof());
     68
     69            while (!file.eof())
     70            {
     71                std::string line;
     72                std::getline(file, line);
     73
     74                // see if we find the output and return
     75                if (line.find("mytestoutput") != std::string::npos)
     76                    return;
     77            }
     78
     79            // output not found - failure!
     80            FAIL();
     81        }
     82    }
     83
     84    // prints time to logfile
     85    TEST(LogWriterTest, PrintsTimestampToLogfile)
     86    {
     87        std::string path;
     88
     89        {
     90            // write lines to log file
     91            std::vector<std::string> lines;
     92            lines.push_back("myothertestoutput");
     93
     94            LogWriter logWriter;
     95            logWriter.setLogDirectory(".");
     96            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
     97
     98            path = logWriter.getPath();
     99        }
     100
     101        {
     102            std::ifstream file(path.c_str(), std::fstream::in);
     103            ASSERT_TRUE(file.is_open());
     104            ASSERT_TRUE(file.good());
     105            ASSERT_FALSE(file.eof());
     106
     107            while (!file.eof())
     108            {
     109                std::string line;
     110                std::getline(file, line);
     111
     112                // see if we find the output and return
     113                if (line.find("myothertestoutput") != std::string::npos)
     114                {
     115                    EXPECT_TRUE(std::isdigit(line[0]));
     116                    EXPECT_TRUE(std::isdigit(line[1]));
     117                    EXPECT_EQ(':', line[2]);
     118                    EXPECT_TRUE(std::isdigit(line[3]));
     119                    EXPECT_TRUE(std::isdigit(line[4]));
     120                    EXPECT_EQ(':', line[5]);
     121                    EXPECT_TRUE(std::isdigit(line[6]));
     122                    EXPECT_TRUE(std::isdigit(line[7]));
     123                    EXPECT_EQ(' ', line[8]);
     124                    return;
     125                }
     126            }
     127
     128            // output not found - failure!
     129            FAIL();
     130        }
     131    }
    6132}
Note: See TracChangeset for help on using the changeset viewer.