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 | #include "OutputHandler.h" |
---|
29 | #include "DebugLevel.h" |
---|
30 | |
---|
31 | namespace orxonox |
---|
32 | { |
---|
33 | /** |
---|
34 | @brief Constructor: Opens the logfile and writes the first line. |
---|
35 | @param logfilename The name of the logfile |
---|
36 | */ |
---|
37 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
38 | { |
---|
39 | this->logfilename_ = logfilename; |
---|
40 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
41 | this->logfile_ << "Started log" << std::endl; |
---|
42 | this->logfile_.flush(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | @brief Destructor: Writes the last line to the logfile and closes it. |
---|
47 | */ |
---|
48 | OutputHandler::~OutputHandler() |
---|
49 | { |
---|
50 | this->logfile_ << "Closed log" << std::endl; |
---|
51 | this->logfile_.close(); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | @returns a reference to the only existing instance of the OutputHandler class. |
---|
56 | */ |
---|
57 | OutputHandler& OutputHandler::getOutStream() |
---|
58 | { |
---|
59 | static OutputHandler orxout("orxonox.log"); |
---|
60 | return orxout; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | @brief Returns the soft debug level for a given output device. |
---|
65 | @param device The output device |
---|
66 | @return The debug level |
---|
67 | */ |
---|
68 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
69 | { |
---|
70 | return DebugLevel::getSoftDebugLevel(device); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
75 | @param sb The streambuffer that should be shown in the console |
---|
76 | @return A reference to the OutputHandler itself |
---|
77 | */ |
---|
78 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
---|
79 | { |
---|
80 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
81 | std::cout << sb; |
---|
82 | |
---|
83 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
84 | { |
---|
85 | this->logfile_ << sb; |
---|
86 | this->logfile_.flush(); |
---|
87 | } |
---|
88 | |
---|
89 | return *this; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
94 | @param manipulator A function, manipulating the outstream. |
---|
95 | @return A reference to the OutputHandler itself |
---|
96 | */ |
---|
97 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
98 | { |
---|
99 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
100 | manipulator(std::cout); |
---|
101 | |
---|
102 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
103 | { |
---|
104 | manipulator(this->logfile_); |
---|
105 | this->logfile_.flush(); |
---|
106 | } |
---|
107 | |
---|
108 | return *this; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
113 | @param manipulator A function, manipulating the outstream. |
---|
114 | @return A reference to the OutputHandler itself |
---|
115 | */ |
---|
116 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
117 | { |
---|
118 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
119 | manipulator(std::cout); |
---|
120 | |
---|
121 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
122 | { |
---|
123 | manipulator(this->logfile_); |
---|
124 | this->logfile_.flush(); |
---|
125 | } |
---|
126 | |
---|
127 | return *this; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
132 | @param manipulator A function, manipulating the outstream. |
---|
133 | @return A reference to the OutputHandler itself |
---|
134 | */ |
---|
135 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
136 | { |
---|
137 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
138 | manipulator(std::cout); |
---|
139 | |
---|
140 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
141 | { |
---|
142 | manipulator(this->logfile_); |
---|
143 | this->logfile_.flush(); |
---|
144 | } |
---|
145 | |
---|
146 | return *this; |
---|
147 | } |
---|
148 | } |
---|