1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
7 
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
16 * Redistributions in binary form must reproduce the above
17   copyright notice, this list of conditions and the
18   following disclaimer in the documentation and/or other
19   materials provided with the distribution.
20 
21 * Neither the name of the assimp team, nor the names of its
22   contributors may be used to endorse or promote products
23   derived from this software without specific prior
24   written permission of the assimp team.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 /** @file Logger.hpp
42  *  @brief Abstract base class 'Logger', base of the logging system.
43  */
44 #ifndef INCLUDED_AI_LOGGER_H
45 #define INCLUDED_AI_LOGGER_H
46 
47 #include "types.h"
48 namespace Assimp	{
49 class LogStream;
50 
51 // Maximum length of a log message. Longer messages are rejected.
52 #define MAX_LOG_MESSAGE_LENGTH 1024u
53 
54 // ----------------------------------------------------------------------------------
55 /**	@brief CPP-API: Abstract interface for logger implementations.
56  *  Assimp provides a default implementation and uses it for almost all
57  *  logging stuff ('DefaultLogger'). This class defines just basic logging
58  *  behaviour and is not of interest for you. Instead, take a look at #DefaultLogger. */
59 class ASSIMP_API Logger
60 	: public Intern::AllocateFromAssimpHeap	{
61 public:
62 
63 	// ----------------------------------------------------------------------
64 	/**	@enum	LogSeverity
65 	 *	@brief	Log severity to describe the granularity of logging.
66 	 */
67 	enum LogSeverity
68 	{
69 		NORMAL,		//!< Normal granularity of logging
70 		VERBOSE		//!< Debug infos will be logged, too
71 	};
72 
73 	// ----------------------------------------------------------------------
74 	/**	@enum	ErrorSeverity
75 	 *	@brief	Description for severity of a log message.
76 	 *
77 	 *  Every LogStream has a bitwise combination of these flags.
78 	 *  A LogStream doesn't receive any messages of a specific type
79 	 *  if it doesn't specify the corresponding ErrorSeverity flag.
80 	 */
81 	enum ErrorSeverity
82 	{
83 		Debugging	= 1,	//!< Debug log message
84 		Info		= 2, 	//!< Info log message
85 		Warn		= 4,	//!< Warn log message
86 		Err			= 8		//!< Error log message
87 	};
88 
89 public:
90 
91 	/** @brief	Virtual destructor */
92 	virtual ~Logger();
93 
94 	// ----------------------------------------------------------------------
95 	/** @brief	Writes a debug message
96 	 *	 @param	message	Debug message*/
97 	void debug(const char* message);
98 	inline void debug(const std::string &message);
99 
100 	// ----------------------------------------------------------------------
101 	/** @brief	Writes a info message
102 	 *	@param	message Info message*/
103 	void info(const char* message);
104 	inline void info(const std::string &message);
105 
106 	// ----------------------------------------------------------------------
107 	/** @brief	Writes a warning message
108 	 *	@param	message Warn message*/
109 	void warn(const char* message);
110 	inline void warn(const std::string &message);
111 
112 	// ----------------------------------------------------------------------
113 	/** @brief	Writes an error message
114 	 *	@param	message	Error message*/
115 	void error(const char* message);
116 	inline void error(const std::string &message);
117 
118 	// ----------------------------------------------------------------------
119 	/** @brief	Set a new log severity.
120 	 *	@param	log_severity New severity for logging*/
121 	void setLogSeverity(LogSeverity log_severity);
122 
123 	// ----------------------------------------------------------------------
124 	/** @brief Get the current log severity*/
125 	LogSeverity getLogSeverity() const;
126 
127 	// ----------------------------------------------------------------------
128 	/** @brief	Attach a new log-stream
129 	 *
130 	 *  The logger takes ownership of the stream and is responsible
131 	 *  for its destruction (which is done using ::delete when the logger
132 	 *  itself is destroyed). Call detachStream to detach a stream and to
133 	 *  gain ownership of it again.
134 	 *	 @param	pStream	 Log-stream to attach
135 	 *  @param severity  Message filter, specified which types of log
136 	 *    messages are dispatched to the stream. Provide a bitwise
137 	 *    combination of the ErrorSeverity flags.
138 	 *  @return true if the stream has been attached, false otherwise.*/
139 	virtual bool attachStream(LogStream *pStream,
140 		unsigned int severity = Debugging | Err | Warn | Info) = 0;
141 
142 	// ----------------------------------------------------------------------
143 	/** @brief	Detach a still attached stream from the logger (or
144 	 *          modify the filter flags bits)
145 	 *	 @param	pStream	Log-stream instance for detaching
146 	 *  @param severity Provide a bitwise combination of the ErrorSeverity
147 	 *    flags. This value is &~ed with the current flags of the stream,
148 	 *    if the result is 0 the stream is detached from the Logger and
149 	 *    the caller retakes the possession of the stream.
150 	 *  @return true if the stream has been detached, false otherwise.*/
151 	virtual bool detatchStream(LogStream *pStream,
152 		unsigned int severity = Debugging | Err | Warn | Info) = 0;
153 
154 protected:
155 
156 	/** Default constructor */
157 	Logger();
158 
159 	/** Construction with a given log severity */
160 	Logger(LogSeverity severity);
161 
162 	// ----------------------------------------------------------------------
163 	/** @brief Called as a request to write a specific debug message
164 	 *	@param	message	Debug message. Never longer than
165 	 *    MAX_LOG_MESSAGE_LENGTH characters (excluding the '0').
166 	 *  @note  The message string is only valid until the scope of
167 	 *    the function is left.
168 	 */
169 	virtual void OnDebug(const char* message)= 0;
170 
171 	// ----------------------------------------------------------------------
172 	/** @brief Called as a request to write a specific info message
173 	 *	@param	message	Info message. Never longer than
174 	 *    MAX_LOG_MESSAGE_LENGTH characters (ecxluding the '0').
175 	 *  @note  The message string is only valid until the scope of
176 	 *    the function is left.
177 	 */
178 	virtual void OnInfo(const char* message) = 0;
179 
180 	// ----------------------------------------------------------------------
181 	/** @brief Called as a request to write a specific warn message
182 	 *	@param	message	Warn message. Never longer than
183 	 *    MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
184 	 *  @note  The message string is only valid until the scope of
185 	 *    the function is left.
186 	 */
187 	virtual void OnWarn(const char* essage) = 0;
188 
189 	// ----------------------------------------------------------------------
190 	/** @brief Called as a request to write a specific error message
191 	 *	@param	message Error message. Never longer than
192 	 *    MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
193 	 *  @note  The message string is only valid until the scope of
194 	 *    the function is left.
195 	 */
196 	virtual void OnError(const char* message) = 0;
197 
198 protected:
199 
200 	//!	Logger severity
201 	LogSeverity m_Severity;
202 };
203 
204 // ----------------------------------------------------------------------------------
205 //	Default constructor
Logger()206 inline Logger::Logger()	{
207 	setLogSeverity(NORMAL);
208 }
209 
210 // ----------------------------------------------------------------------------------
211 //	Virtual destructor
~Logger()212 inline  Logger::~Logger()
213 {
214 }
215 
216 // ----------------------------------------------------------------------------------
217 // Construction with given logging severity
Logger(LogSeverity severity)218 inline Logger::Logger(LogSeverity severity)	{
219 	setLogSeverity(severity);
220 }
221 
222 // ----------------------------------------------------------------------------------
223 // Log severity setter
setLogSeverity(LogSeverity log_severity)224 inline void Logger::setLogSeverity(LogSeverity log_severity){
225 	m_Severity = log_severity;
226 }
227 
228 // ----------------------------------------------------------------------------------
229 // Log severity getter
getLogSeverity() const230 inline Logger::LogSeverity Logger::getLogSeverity() const {
231 	return m_Severity;
232 }
233 
234 // ----------------------------------------------------------------------------------
debug(const std::string & message)235 inline void Logger::debug(const std::string &message)
236 {
237 	return debug(message.c_str());
238 }
239 
240 // ----------------------------------------------------------------------------------
error(const std::string & message)241 inline void Logger::error(const std::string &message)
242 {
243 	return error(message.c_str());
244 }
245 
246 // ----------------------------------------------------------------------------------
warn(const std::string & message)247 inline void Logger::warn(const std::string &message)
248 {
249 	return warn(message.c_str());
250 }
251 
252 // ----------------------------------------------------------------------------------
info(const std::string & message)253 inline void Logger::info(const std::string &message)
254 {
255 	return info(message.c_str());
256 }
257 
258 // ----------------------------------------------------------------------------------
259 
260 } // Namespace Assimp
261 
262 #endif // !! INCLUDED_AI_LOGGER_H
263