Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

added archive feature to LogWriter which archives up to nine old log-files (orxonox.log.1 to orxonox.log.9)

Location:
code/branches/testing/src/libraries/util/output
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/testing/src/libraries/util/output/LogWriter.cc

    r9540 r9544  
    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.
     
    8285    void LogWriter::openFile()
    8386    {
     87        // archive the old log file
     88        this->archive();
     89
    8490        // open the file
    8591        this->file_.open(this->getPath().c_str(), std::fstream::out);
     
    102108            this->file_.close();
    103109        }
     110    }
     111
     112    /**
     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;
    104154    }
    105155
  • code/branches/testing/src/libraries/util/output/LogWriter.h

    r9542 r9544  
    7676            void closeFile();
    7777
     78            void archive(int index = 0);
     79            std::string getArchivedPath(int index) const;
     80
    7881            std::string filename_;  ///< The name of the log-file (without directory)
    7982            std::string directory_; ///< The directory where the log-file resided (without file-name)
Note: See TracChangeset for help on using the changeset viewer.