Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 12, 2013, 11:13:03 PM (11 years ago)
Author:
landauf
Message:

merged testing branch back to trunk. unbelievable it took me 13 months to finish this chore…

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/util/output/LogWriter.cc

    r8858 r9550  
    3939#include "OutputManager.h"
    4040#include "MemoryWriter.h"
     41#include "util/Convert.h"
    4142
    4243namespace orxonox
    4344{
     45    static const int MAX_ARCHIVED_FILES = 9;
     46
    4447    /**
    4548        @brief Constructor, initializes the desired output levels and the name and path of the log-file, and opens the log-file.
     
    5861        // get the path for a temporary file, depending on the system
    5962#ifdef ORXONOX_PLATFORM_WINDOWS
    60         this->path_ = getenv("TEMP");
     63        this->directory_ = getenv("TEMP");
    6164#else
    62         this->path_ = "/tmp";
     65        this->directory_ = "/tmp";
    6366#endif
    64         this->bDefaultPath_ = true;
     67
     68        // send a message to the user so that he can find the file in the case of a crash.
     69        OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath());
    6570
    6671        this->openFile();
     
    7681
    7782    /**
    78         @brief Returns the only existing instance of this class.
    79     */
    80     /*static*/ LogWriter& LogWriter::getInstance()
    81     {
    82         static LogWriter instance;
    83         return instance;
    84     }
    85 
    86     /**
    8783        @brief Opens the log-file in order to write output to it.
    8884    */
    8985    void LogWriter::openFile()
    9086    {
    91         // get the full file-name
    92         std::string name = this->path_ + '/' + this->filename_;
    93 
    94         // 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.
    95         if (this->bDefaultPath_)
    96             OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name);
     87        // archive the old log file
     88        this->archive();
    9789
    9890        // open the file
    99         this->file_.open(name.c_str(), std::fstream::out);
     91        this->file_.open(this->getPath().c_str(), std::fstream::out);
    10092
    10193        // check if it worked and print some output
     
    119111
    120112    /**
     113     * @brief Archives old copies of the log file by adding increasing numbers to the filename.
     114     */
     115    void LogWriter::archive(int index)
     116    {
     117        std::string oldPath = this->getArchivedPath(index);
     118
     119        // see if the file already exists, otherwise return
     120        std::ifstream stream(oldPath.c_str());
     121        bool exists = stream.is_open();
     122        stream.close();
     123
     124        if (!exists)
     125            return;
     126
     127        if (index < MAX_ARCHIVED_FILES)
     128        {
     129            // increment the index and archive the file with the next higher index
     130            this->archive(++index);
     131
     132            // create the new path based on the incremented index
     133            std::string newPath = this->getArchivedPath(index);
     134
     135            // move the file
     136            std::rename(oldPath.c_str(), newPath.c_str());
     137        }
     138        else
     139        {
     140            // delete the file
     141            std::remove(oldPath.c_str());
     142        }
     143    }
     144   
     145    /**
     146     * @brief Returns the path for archived copies of the logfile (based on the archive index)
     147     */
     148    std::string LogWriter::getArchivedPath(int index) const
     149    {
     150        std::string path = this->getPath();
     151        if (index > 0)
     152            path += '.' + multi_cast<std::string>(index);
     153        return path;
     154    }
     155
     156    /**
    121157        @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter.
    122158    */
    123     void LogWriter::setLogPath(const std::string& path)
     159    void LogWriter::setLogDirectory(const std::string& directory)
    124160    {
    125161        // notify about the change of the log-file (because the old file will no longer be updated)
    126         OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path);
     162        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory);
    127163
    128164        // close the old file, update the path and open the new file
    129165        this->closeFile();
    130         this->path_ = path;
    131         this->bDefaultPath_ = false;
     166        this->directory_ = directory;
    132167        this->openFile();
    133168
    134169        // request old output from MemoryWriter
    135         MemoryWriter::getInstance().resendOutput(this);
     170        if (OutputManager::getInstance().getMemoryWriter())
     171            OutputManager::getInstance().getMemoryWriter()->resendOutput(this);
    136172    }
    137173
Note: See TracChangeset for help on using the changeset viewer.