Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8517


Ignore:
Timestamp:
May 20, 2011, 5:18:08 AM (14 years ago)
Author:
rgrieder
Message:

Improved logging to a file by rewriting the log according to the right debug level. Also, failure to open the file is now being handled.

Location:
code/branches/unity_build/src/libraries
Files:
3 edited

Legend:

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

    r8423 r8517  
    168168        RegisterRootObject(Core);
    169169        this->setConfigValues();
     170        // Rewrite the log file with the correct log levels
     171        OutputHandler::getInstance().rewriteLogFile();
    170172
    171173#if !defined(ORXONOX_PLATFORM_APPLE) && !defined(ORXONOX_USE_WINMAIN)
     
    240242        SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile)
    241243            .description("The maximum level of debug output shown in the log file");
    242         OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
     244        OutputHandler::getInstance().setSoftDebugLevel("LogFile", this->softDebugLevelLogFile_);
    243245
    244246        SetConfigValue(bDevMode_, PathConfig::buildDirectoryRun())
  • code/branches/unity_build/src/libraries/util/OutputHandler.cc

    r8516 r8517  
    6969        */
    7070        LogFileWriter()
    71             : OutputListener(OutputHandler::logFileOutputListenerName_s)
     71            : OutputListener("LogFile")
    7272        {
    7373            // Get path for a temporary file
     
    8585            timeinfo = localtime(&rawtime);
    8686
    87             this->logFile_.open(this->logFilename_.c_str(), std::fstream::out);
    88             this->logFile_ << "Started log on " << asctime(timeinfo) << std::endl;
    89             this->logFile_.flush();
    90 
    91             this->outputStream_ = &this->logFile_;
     87            this->openFile();
     88            if (this->logFile_.is_open())
     89            {
     90                this->logFile_ << "Started log on " << asctime(timeinfo) << std::endl;
     91                this->logFile_.flush();
     92            }
    9293        }
    9394
     
    9596        ~LogFileWriter()
    9697        {
    97             this->logFile_ << "Closed log" << std::endl;
    98             this->logFile_.close();
     98            if (this->logFile_.is_open())
     99            {
     100                this->logFile_ << "Closed log" << std::endl;
     101                this->logFile_.close();
     102            }
    99103        }
    100104
     
    102106        void setLogPath(const std::string& path)
    103107        {
    104             this->logFile_.close();
    105             // Read old file into a buffer
    106             std::ifstream old(this->logFilename_.c_str());
     108            if (this->logFile_.is_open())
     109                this->logFile_.close();
     110
     111            // Open the new file
    107112            this->logFilename_ = path + logFileBaseName_g;
    108             // Open the new file and feed it the content of the old one
     113            this->openFile();
     114        }
     115
     116        //! Erases the log file
     117        void clearFile()
     118        {
     119            if (this->logFile_.is_open())
     120            {
     121                this->logFile_.close();
     122                this->openFile();
     123            }
     124        }
     125
     126    private:
     127        void openFile()
     128        {
    109129            this->logFile_.open(this->logFilename_.c_str(), std::fstream::out);
    110             this->logFile_ << old.rdbuf();
    111             this->logFile_.flush();
    112             old.close();
    113         }
    114 
    115     private:
     130
     131            if (this->logFile_.is_open())
     132                this->outputStream_ = &this->logFile_;
     133            else
     134            {
     135                COUT(2) << "Warning: Failed to open log file. File logging disabled." << std::endl;
     136                this->outputStream_ = NULL;
     137            }
     138        }
     139
    116140        std::ofstream logFile_;     //!< File handle for the log file
    117141        std::string   logFilename_; //!< Filename of the log file
     
    185209    ///// OutputHandler /////
    186210    /////////////////////////
    187     const std::string OutputHandler::logFileOutputListenerName_s = "logFile";
    188           int         OutputHandler::softDebugLevel_s = hardDebugLevel;
     211    int OutputHandler::softDebugLevel_s = hardDebugLevel;
    189212
    190213    //! Creates the LogFileWriter and the MemoryLogWriter
     
    259282    {
    260283        this->logFile_->setLogPath(path);
     284        this->rewriteLogFile();
     285    }
     286
     287    void OutputHandler::rewriteLogFile()
     288    {
     289        logFile_->clearFile();
     290
     291        if (logFile_->outputStream_ == NULL)
     292            return;
     293
     294        for (OutputVector::const_iterator it = this->getOutput().begin(); it != this->getOutput().end(); ++it)
     295        {
     296            if (it->first <= logFile_->softDebugLevel_)
     297                (*logFile_->outputStream_) << it->second;
     298        }
     299        logFile_->outputStream_->flush();
    261300    }
    262301
  • code/branches/unity_build/src/libraries/util/OutputHandler.h

    r8516 r8517  
    133133            //! Set the log path once the program has been properly initialised
    134134            void setLogPath(const std::string& path);
     135            /** Rewrites the log file (completely respects the current debug level).
     136                Once disableMemoryLog() has been called, this function will do nothing.
     137            */
     138            void rewriteLogFile();
     139
    135140            //! Disables the std::cout stream for output
    136141            void disableCout();
     
    211216            //! Dummy operator required by Debug.h for the ternary operator
    212217            inline operator int() const { return 0; }
    213 
    214             //! Name of the OutputListener that writes to the log file
    215             static const std::string logFileOutputListenerName_s;
    216218
    217219        private:
Note: See TracChangeset for help on using the changeset viewer.