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