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 | ConsoleCommandShortcutGeneric(error, createExecutor(createFunctor(&OutputHandler::error), "error", AccessLevel::None)); |
---|
42 | ConsoleCommandShortcutGeneric(warning, createExecutor(createFunctor(&OutputHandler::warning), "warning", AccessLevel::None)); |
---|
43 | ConsoleCommandShortcutGeneric(info, createExecutor(createFunctor(&OutputHandler::info), "info", AccessLevel::None)); |
---|
44 | ConsoleCommandShortcutGeneric(debug, createExecutor(createFunctor(&OutputHandler::debug), "debug", AccessLevel::None)); |
---|
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 | return *this; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
108 | @param manipulator A function, manipulating the outstream. |
---|
109 | @return A reference to the OutputHandler itself |
---|
110 | */ |
---|
111 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
112 | { |
---|
113 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
114 | manipulator(std::cout); |
---|
115 | |
---|
116 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
117 | { |
---|
118 | manipulator(this->logfile_); |
---|
119 | this->logfile_.flush(); |
---|
120 | } |
---|
121 | |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
127 | @param manipulator A function, manipulating the outstream. |
---|
128 | @return A reference to the OutputHandler itself |
---|
129 | */ |
---|
130 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
131 | { |
---|
132 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
133 | manipulator(std::cout); |
---|
134 | |
---|
135 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
136 | { |
---|
137 | manipulator(this->logfile_); |
---|
138 | this->logfile_.flush(); |
---|
139 | } |
---|
140 | |
---|
141 | return *this; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
146 | @param manipulator A function, manipulating the outstream. |
---|
147 | @return A reference to the OutputHandler itself |
---|
148 | */ |
---|
149 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
150 | { |
---|
151 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
152 | manipulator(std::cout); |
---|
153 | |
---|
154 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
155 | { |
---|
156 | manipulator(this->logfile_); |
---|
157 | this->logfile_.flush(); |
---|
158 | } |
---|
159 | |
---|
160 | return *this; |
---|
161 | } |
---|
162 | } |
---|