1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef __Log_H__ |
---|
30 | #define __Log_H__ |
---|
31 | |
---|
32 | #include "OgrePrerequisites.h" |
---|
33 | #include "OgreString.h" |
---|
34 | #include "OgreHeaderPrefix.h" |
---|
35 | #include "Threading/OgreThreadHeaders.h" |
---|
36 | |
---|
37 | #if OGRE_PLATFORM == OGRE_PLATFORM_NACL |
---|
38 | namespace pp |
---|
39 | { |
---|
40 | class Instance; |
---|
41 | } |
---|
42 | #endif |
---|
43 | |
---|
44 | namespace Ogre { |
---|
45 | |
---|
46 | /** \addtogroup Core |
---|
47 | * @{ |
---|
48 | */ |
---|
49 | /** \addtogroup General |
---|
50 | * @{ |
---|
51 | */ |
---|
52 | // LogMessageLevel + LoggingLevel > OGRE_LOG_THRESHOLD = message logged |
---|
53 | #define OGRE_LOG_THRESHOLD 4 |
---|
54 | |
---|
55 | /** The level of detail to which the log will go into. |
---|
56 | */ |
---|
57 | enum LoggingLevel |
---|
58 | { |
---|
59 | LL_LOW = 1, |
---|
60 | LL_NORMAL = 2, |
---|
61 | LL_BOREME = 3 |
---|
62 | }; |
---|
63 | |
---|
64 | /** The importance of a logged message. |
---|
65 | */ |
---|
66 | enum LogMessageLevel |
---|
67 | { |
---|
68 | LML_TRIVIAL = 1, |
---|
69 | LML_NORMAL = 2, |
---|
70 | LML_CRITICAL = 3 |
---|
71 | }; |
---|
72 | |
---|
73 | /** @remarks Pure Abstract class, derive this class and register to the Log to listen to log messages */ |
---|
74 | class LogListener |
---|
75 | { |
---|
76 | public: |
---|
77 | virtual ~LogListener() {} |
---|
78 | |
---|
79 | /** |
---|
80 | @remarks |
---|
81 | This is called whenever the log receives a message and is about to write it out |
---|
82 | @param message |
---|
83 | The message to be logged |
---|
84 | @param lml |
---|
85 | The message level the log is using |
---|
86 | @param maskDebug |
---|
87 | If we are printing to the console or not |
---|
88 | @param logName |
---|
89 | The name of this log (so you can have several listeners for different logs, and identify them) |
---|
90 | @param skipThisMessage |
---|
91 | If set to true by the messageLogged() implementation message will not be logged |
---|
92 | */ |
---|
93 | virtual void messageLogged( const String& message, LogMessageLevel lml, bool maskDebug, const String &logName, bool& skipThisMessage ) = 0; |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | @remarks |
---|
99 | Log class for writing debug/log data to files. |
---|
100 | @note |
---|
101 | <br>Should not be used directly, but trough the LogManager class. |
---|
102 | */ |
---|
103 | class _OgreExport Log : public LogAlloc |
---|
104 | { |
---|
105 | protected: |
---|
106 | std::ofstream mLog; |
---|
107 | LoggingLevel mLogLevel; |
---|
108 | bool mDebugOut; |
---|
109 | bool mSuppressFile; |
---|
110 | bool mTimeStamp; |
---|
111 | String mLogName; |
---|
112 | |
---|
113 | typedef vector<LogListener*>::type mtLogListener; |
---|
114 | mtLogListener mListeners; |
---|
115 | public: |
---|
116 | |
---|
117 | class Stream; |
---|
118 | |
---|
119 | OGRE_AUTO_MUTEX; // public to allow external locking |
---|
120 | /** |
---|
121 | @remarks |
---|
122 | Usual constructor - called by LogManager. |
---|
123 | */ |
---|
124 | Log( const String& name, bool debugOutput = true, bool suppressFileOutput = false); |
---|
125 | |
---|
126 | /** |
---|
127 | @remarks |
---|
128 | Default destructor. |
---|
129 | */ |
---|
130 | ~Log(); |
---|
131 | |
---|
132 | /// Return the name of the log |
---|
133 | const String& getName() const { return mLogName; } |
---|
134 | /// Get whether debug output is enabled for this log |
---|
135 | bool isDebugOutputEnabled() const { return mDebugOut; } |
---|
136 | /// Get whether file output is suppressed for this log |
---|
137 | bool isFileOutputSuppressed() const { return mSuppressFile; } |
---|
138 | /// Get whether time stamps are printed for this log |
---|
139 | bool isTimeStampEnabled() const { return mTimeStamp; } |
---|
140 | |
---|
141 | /** Log a message to the debugger and to log file (the default is |
---|
142 | "<code>OGRE.log</code>"), |
---|
143 | */ |
---|
144 | void logMessage( const String& message, LogMessageLevel lml = LML_NORMAL, bool maskDebug = false ); |
---|
145 | |
---|
146 | /** Get a stream object targeting this log. */ |
---|
147 | Stream stream(LogMessageLevel lml = LML_NORMAL, bool maskDebug = false); |
---|
148 | |
---|
149 | /** |
---|
150 | @remarks |
---|
151 | Enable or disable outputting log messages to the debugger. |
---|
152 | */ |
---|
153 | void setDebugOutputEnabled(bool debugOutput); |
---|
154 | /** |
---|
155 | @remarks |
---|
156 | Sets the level of the log detail. |
---|
157 | */ |
---|
158 | void setLogDetail(LoggingLevel ll); |
---|
159 | /** |
---|
160 | @remarks |
---|
161 | Enable or disable time stamps. |
---|
162 | */ |
---|
163 | void setTimeStampEnabled(bool timeStamp); |
---|
164 | /** Gets the level of the log detail. |
---|
165 | */ |
---|
166 | LoggingLevel getLogDetail() const { return mLogLevel; } |
---|
167 | /** |
---|
168 | @remarks |
---|
169 | Register a listener to this log |
---|
170 | @param listener |
---|
171 | A valid listener derived class |
---|
172 | */ |
---|
173 | void addListener(LogListener* listener); |
---|
174 | |
---|
175 | /** |
---|
176 | @remarks |
---|
177 | Unregister a listener from this log |
---|
178 | @param listener |
---|
179 | A valid listener derived class |
---|
180 | */ |
---|
181 | void removeListener(LogListener* listener); |
---|
182 | |
---|
183 | /** Stream object which targets a log. |
---|
184 | @remarks |
---|
185 | A stream logger object makes it simpler to send various things to |
---|
186 | a log. You can just use the operator<< implementation to stream |
---|
187 | anything to the log, which is cached until a Stream::Flush is |
---|
188 | encountered, or the stream itself is destroyed, at which point the |
---|
189 | cached contents are sent to the underlying log. You can use Log::stream() |
---|
190 | directly without assigning it to a local variable and as soon as the |
---|
191 | streaming is finished, the object will be destroyed and the message |
---|
192 | logged. |
---|
193 | @par |
---|
194 | You can stream control operations to this object too, such as |
---|
195 | std::setw() and std::setfill() to control formatting. |
---|
196 | @note |
---|
197 | Each Stream object is not thread safe, so do not pass it between |
---|
198 | threads. Multiple threads can hold their own Stream instances pointing |
---|
199 | at the same Log though and that is threadsafe. |
---|
200 | */ |
---|
201 | class _OgrePrivate Stream |
---|
202 | { |
---|
203 | protected: |
---|
204 | Log* mTarget; |
---|
205 | LogMessageLevel mLevel; |
---|
206 | bool mMaskDebug; |
---|
207 | typedef StringUtil::StrStreamType BaseStream; |
---|
208 | BaseStream mCache; |
---|
209 | |
---|
210 | public: |
---|
211 | |
---|
212 | /// Simple type to indicate a flush of the stream to the log |
---|
213 | struct Flush {}; |
---|
214 | |
---|
215 | Stream(Log* target, LogMessageLevel lml, bool maskDebug) |
---|
216 | :mTarget(target), mLevel(lml), mMaskDebug(maskDebug) |
---|
217 | { |
---|
218 | |
---|
219 | } |
---|
220 | // copy constructor |
---|
221 | Stream(const Stream& rhs) |
---|
222 | : mTarget(rhs.mTarget), mLevel(rhs.mLevel), mMaskDebug(rhs.mMaskDebug) |
---|
223 | { |
---|
224 | // explicit copy of stream required, gcc doesn't like implicit |
---|
225 | mCache.str(rhs.mCache.str()); |
---|
226 | } |
---|
227 | ~Stream() |
---|
228 | { |
---|
229 | // flush on destroy |
---|
230 | if (mCache.tellp() > 0) |
---|
231 | { |
---|
232 | mTarget->logMessage(mCache.str(), mLevel, mMaskDebug); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | template <typename T> |
---|
237 | Stream& operator<< (const T& v) |
---|
238 | { |
---|
239 | mCache << v; |
---|
240 | return *this; |
---|
241 | } |
---|
242 | |
---|
243 | Stream& operator<< (const Flush& v) |
---|
244 | { |
---|
245 | (void)v; |
---|
246 | mTarget->logMessage(mCache.str(), mLevel, mMaskDebug); |
---|
247 | mCache.str(StringUtil::BLANK); |
---|
248 | return *this; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | }; |
---|
253 | #if OGRE_PLATFORM == OGRE_PLATFORM_NACL |
---|
254 | protected: |
---|
255 | static pp::Instance* mInstance; |
---|
256 | public: |
---|
257 | static void setInstance(pp::Instance* instance) {mInstance = instance;}; |
---|
258 | #endif |
---|
259 | |
---|
260 | }; |
---|
261 | /** @} */ |
---|
262 | /** @} */ |
---|
263 | } |
---|
264 | |
---|
265 | #include "OgreHeaderSuffix.h" |
---|
266 | |
---|
267 | #endif |
---|