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 | SetConsoleCommandShortcutGeneric(log, createConsoleCommand(createFunctor(&OutputHandler::log), "log" )); |
---|
41 | SetConsoleCommandShortcutGeneric(error, createConsoleCommand(createFunctor(&OutputHandler::error), "error" )); |
---|
42 | SetConsoleCommandShortcutGeneric(warning, createConsoleCommand(createFunctor(&OutputHandler::warning), "warning")); |
---|
43 | SetConsoleCommandShortcutGeneric(info, createConsoleCommand(createFunctor(&OutputHandler::info), "info" )); |
---|
44 | SetConsoleCommandShortcutGeneric(debug, createConsoleCommand(createFunctor(&OutputHandler::debug), "debug" )); |
---|
45 | |
---|
46 | /** |
---|
47 | @brief Constructor: Opens the logfile and writes the first line. |
---|
48 | @param logfilename The name of the logfile |
---|
49 | */ |
---|
50 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
51 | { |
---|
52 | this->logfilename_ = logfilename; |
---|
53 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
54 | this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl; |
---|
55 | this->logfile_.flush(); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | @brief Destructor: Writes the last line to the logfile and closes it. |
---|
60 | */ |
---|
61 | OutputHandler::~OutputHandler() |
---|
62 | { |
---|
63 | this->logfile_ << "Closed log" << std::endl; |
---|
64 | this->logfile_.close(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | @brief Returns a reference to the only existing instance of the OutputHandler class. |
---|
69 | @return The instance |
---|
70 | */ |
---|
71 | OutputHandler& OutputHandler::getOutStream() |
---|
72 | { |
---|
73 | static OutputHandler orxout("orxonox.log"); |
---|
74 | return orxout; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | @brief Returns the soft debug level for a given output device. |
---|
79 | @param device The output device |
---|
80 | @return The debug level |
---|
81 | */ |
---|
82 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
83 | { |
---|
84 | return CoreSettings::getSoftDebugLevel(device); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
89 | @param sb The streambuffer that should be shown in the console |
---|
90 | @return A reference to the OutputHandler itself |
---|
91 | */ |
---|
92 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
---|
93 | { |
---|
94 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
95 | std::cout << sb; |
---|
96 | |
---|
97 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
98 | { |
---|
99 | this->logfile_ << sb; |
---|
100 | this->logfile_.flush(); |
---|
101 | } |
---|
102 | |
---|
103 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
104 | Shell::getInstance().getOutputBuffer() << sb; |
---|
105 | |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
111 | @param manipulator A function, manipulating the outstream. |
---|
112 | @return A reference to the OutputHandler itself |
---|
113 | */ |
---|
114 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
115 | { |
---|
116 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
117 | manipulator(std::cout); |
---|
118 | |
---|
119 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
120 | { |
---|
121 | manipulator(this->logfile_); |
---|
122 | this->logfile_.flush(); |
---|
123 | } |
---|
124 | |
---|
125 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
126 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
127 | |
---|
128 | return *this; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
133 | @param manipulator A function, manipulating the outstream. |
---|
134 | @return A reference to the OutputHandler itself |
---|
135 | */ |
---|
136 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
137 | { |
---|
138 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
139 | manipulator(std::cout); |
---|
140 | |
---|
141 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
142 | { |
---|
143 | manipulator(this->logfile_); |
---|
144 | this->logfile_.flush(); |
---|
145 | } |
---|
146 | |
---|
147 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
148 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
149 | |
---|
150 | return *this; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
155 | @param manipulator A function, manipulating the outstream. |
---|
156 | @return A reference to the OutputHandler itself |
---|
157 | */ |
---|
158 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
159 | { |
---|
160 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
161 | manipulator(std::cout); |
---|
162 | |
---|
163 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
164 | { |
---|
165 | manipulator(this->logfile_); |
---|
166 | this->logfile_.flush(); |
---|
167 | } |
---|
168 | |
---|
169 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
170 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
171 | |
---|
172 | return *this; |
---|
173 | } |
---|
174 | } |
---|