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