| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file OutputHandler.cc |
|---|
| 31 | @brief Implementation of the OutputHandler class. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "OutputHandler.h" |
|---|
| 35 | #include "CoreSettings.h" |
|---|
| 36 | #include "ConsoleCommand.h" |
|---|
| 37 | |
|---|
| 38 | namespace orxonox |
|---|
| 39 | { |
|---|
| 40 | ConsoleCommandShortcutGeneric(log, createExecutor(createFunctor(&OutputHandler::log), "log", AccessLevel::None)); |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | @brief Constructor: Opens the logfile and writes the first line. |
|---|
| 44 | @param logfilename The name of the logfile |
|---|
| 45 | */ |
|---|
| 46 | OutputHandler::OutputHandler(const std::string& logfilename) |
|---|
| 47 | { |
|---|
| 48 | this->logfilename_ = logfilename; |
|---|
| 49 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
|---|
| 50 | this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl; |
|---|
| 51 | this->logfile_.flush(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | @brief Destructor: Writes the last line to the logfile and closes it. |
|---|
| 56 | */ |
|---|
| 57 | OutputHandler::~OutputHandler() |
|---|
| 58 | { |
|---|
| 59 | this->logfile_ << "Closed log" << std::endl; |
|---|
| 60 | this->logfile_.close(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | @brief Returns a reference to the only existing instance of the OutputHandler class. |
|---|
| 65 | @return The instance |
|---|
| 66 | */ |
|---|
| 67 | OutputHandler& OutputHandler::getOutStream() |
|---|
| 68 | { |
|---|
| 69 | static OutputHandler orxout("orxonox.log"); |
|---|
| 70 | return orxout; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | @brief Returns the soft debug level for a given output device. |
|---|
| 75 | @param device The output device |
|---|
| 76 | @return The debug level |
|---|
| 77 | */ |
|---|
| 78 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
|---|
| 79 | { |
|---|
| 80 | return CoreSettings::getSoftDebugLevel(device); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
|---|
| 85 | @param sb The streambuffer that should be shown in the console |
|---|
| 86 | @return A reference to the OutputHandler itself |
|---|
| 87 | */ |
|---|
| 88 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
|---|
| 89 | { |
|---|
| 90 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
|---|
| 91 | std::cout << sb; |
|---|
| 92 | |
|---|
| 93 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
|---|
| 94 | { |
|---|
| 95 | this->logfile_ << sb; |
|---|
| 96 | this->logfile_.flush(); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return *this; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
|---|
| 104 | @param manipulator A function, manipulating the outstream. |
|---|
| 105 | @return A reference to the OutputHandler itself |
|---|
| 106 | */ |
|---|
| 107 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
|---|
| 108 | { |
|---|
| 109 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
|---|
| 110 | manipulator(std::cout); |
|---|
| 111 | |
|---|
| 112 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
|---|
| 113 | { |
|---|
| 114 | manipulator(this->logfile_); |
|---|
| 115 | this->logfile_.flush(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | return *this; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
|---|
| 123 | @param manipulator A function, manipulating the outstream. |
|---|
| 124 | @return A reference to the OutputHandler itself |
|---|
| 125 | */ |
|---|
| 126 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
|---|
| 127 | { |
|---|
| 128 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
|---|
| 129 | manipulator(std::cout); |
|---|
| 130 | |
|---|
| 131 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
|---|
| 132 | { |
|---|
| 133 | manipulator(this->logfile_); |
|---|
| 134 | this->logfile_.flush(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return *this; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
|---|
| 142 | @param manipulator A function, manipulating the outstream. |
|---|
| 143 | @return A reference to the OutputHandler itself |
|---|
| 144 | */ |
|---|
| 145 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
|---|
| 146 | { |
|---|
| 147 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
|---|
| 148 | manipulator(std::cout); |
|---|
| 149 | |
|---|
| 150 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
|---|
| 151 | { |
|---|
| 152 | manipulator(this->logfile_); |
|---|
| 153 | this->logfile_.flush(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | return *this; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|