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 | |
---|
28 | /** |
---|
29 | @file OutputHandler.cc |
---|
30 | @brief Implementation of the OutputHandler class. |
---|
31 | */ |
---|
32 | |
---|
33 | #include "CoreSettings.h" |
---|
34 | #include "OutputHandler.h" |
---|
35 | #include "ConsoleCommand.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | ConsoleCommandShortcutGeneric(log, createExecutor(createFunctor(&OutputHandler::log), "log", AccessLevel::None)); |
---|
40 | |
---|
41 | /** |
---|
42 | @brief Constructor: Opens the logfile and writes the first line. |
---|
43 | @param logfilename The name of the logfile |
---|
44 | */ |
---|
45 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
46 | { |
---|
47 | this->logfilename_ = logfilename; |
---|
48 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
49 | this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl; |
---|
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 | /** |
---|
63 | @brief Returns a reference to the only existing instance of the OutputHandler class. |
---|
64 | @return The instance |
---|
65 | */ |
---|
66 | OutputHandler& OutputHandler::getOutStream() |
---|
67 | { |
---|
68 | static OutputHandler orxout("orxonox.log"); |
---|
69 | return orxout; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
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 | { |
---|
79 | return CoreSettings::getSoftDebugLevel(device); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
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 | { |
---|
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 | |
---|
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 | { |
---|
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 | |
---|
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 | { |
---|
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 | |
---|
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 | { |
---|
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 | |
---|
155 | return *this; |
---|
156 | } |
---|
157 | } |
---|