1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4
5 Copyright (c) 2006-2015, 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 #ifndef SWIG
61 : public Intern::AllocateFromAssimpHeap
62 #endif
63 {
64 public:
65
66 // ----------------------------------------------------------------------
67 /** @enum LogSeverity
68 * @brief Log severity to describe the granularity of logging.
69 */
70 enum LogSeverity
71 {
72 NORMAL, //!< Normal granularity of logging
73 VERBOSE //!< Debug infos will be logged, too
74 };
75
76 // ----------------------------------------------------------------------
77 /** @enum ErrorSeverity
78 * @brief Description for severity of a log message.
79 *
80 * Every LogStream has a bitwise combination of these flags.
81 * A LogStream doesn't receive any messages of a specific type
82 * if it doesn't specify the corresponding ErrorSeverity flag.
83 */
84 enum ErrorSeverity
85 {
86 Debugging = 1, //!< Debug log message
87 Info = 2, //!< Info log message
88 Warn = 4, //!< Warn log message
89 Err = 8 //!< Error log message
90 };
91
92 public:
93
94 /** @brief Virtual destructor */
95 virtual ~Logger();
96
97 // ----------------------------------------------------------------------
98 /** @brief Writes a debug message
99 * @param message Debug message*/
100 void debug(const char* message);
101 inline void debug(const std::string &message);
102
103 // ----------------------------------------------------------------------
104 /** @brief Writes a info message
105 * @param message Info message*/
106 void info(const char* message);
107 inline void info(const std::string &message);
108
109 // ----------------------------------------------------------------------
110 /** @brief Writes a warning message
111 * @param message Warn message*/
112 void warn(const char* message);
113 inline void warn(const std::string &message);
114
115 // ----------------------------------------------------------------------
116 /** @brief Writes an error message
117 * @param message Error message*/
118 void error(const char* message);
119 inline void error(const std::string &message);
120
121 // ----------------------------------------------------------------------
122 /** @brief Set a new log severity.
123 * @param log_severity New severity for logging*/
124 void setLogSeverity(LogSeverity log_severity);
125
126 // ----------------------------------------------------------------------
127 /** @brief Get the current log severity*/
128 LogSeverity getLogSeverity() const;
129
130 // ----------------------------------------------------------------------
131 /** @brief Attach a new log-stream
132 *
133 * The logger takes ownership of the stream and is responsible
134 * for its destruction (which is done using ::delete when the logger
135 * itself is destroyed). Call detachStream to detach a stream and to
136 * gain ownership of it again.
137 * @param pStream Log-stream to attach
138 * @param severity Message filter, specified which types of log
139 * messages are dispatched to the stream. Provide a bitwise
140 * combination of the ErrorSeverity flags.
141 * @return true if the stream has been attached, false otherwise.*/
142 virtual bool attachStream(LogStream *pStream,
143 unsigned int severity = Debugging | Err | Warn | Info) = 0;
144
145 // ----------------------------------------------------------------------
146 /** @brief Detach a still attached stream from the logger (or
147 * modify the filter flags bits)
148 * @param pStream Log-stream instance for detaching
149 * @param severity Provide a bitwise combination of the ErrorSeverity
150 * flags. This value is &~ed with the current flags of the stream,
151 * if the result is 0 the stream is detached from the Logger and
152 * the caller retakes the possession of the stream.
153 * @return true if the stream has been detached, false otherwise.*/
154 virtual bool detatchStream(LogStream *pStream,
155 unsigned int severity = Debugging | Err | Warn | Info) = 0;
156
157 protected:
158
159 /** Default constructor */
160 Logger();
161
162 /** Construction with a given log severity */
163 explicit Logger(LogSeverity severity);
164
165 // ----------------------------------------------------------------------
166 /** @brief Called as a request to write a specific debug message
167 * @param message Debug message. Never longer than
168 * MAX_LOG_MESSAGE_LENGTH characters (excluding the '0').
169 * @note The message string is only valid until the scope of
170 * the function is left.
171 */
172 virtual void OnDebug(const char* message)= 0;
173
174 // ----------------------------------------------------------------------
175 /** @brief Called as a request to write a specific info message
176 * @param message Info message. Never longer than
177 * MAX_LOG_MESSAGE_LENGTH characters (ecxluding the '0').
178 * @note The message string is only valid until the scope of
179 * the function is left.
180 */
181 virtual void OnInfo(const char* message) = 0;
182
183 // ----------------------------------------------------------------------
184 /** @brief Called as a request to write a specific warn message
185 * @param message Warn message. Never longer than
186 * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
187 * @note The message string is only valid until the scope of
188 * the function is left.
189 */
190 virtual void OnWarn(const char* essage) = 0;
191
192 // ----------------------------------------------------------------------
193 /** @brief Called as a request to write a specific error message
194 * @param message Error message. Never longer than
195 * MAX_LOG_MESSAGE_LENGTH characters (exluding the '0').
196 * @note The message string is only valid until the scope of
197 * the function is left.
198 */
199 virtual void OnError(const char* message) = 0;
200
201 protected:
202
203 //! Logger severity
204 LogSeverity m_Severity;
205 };
206
207 // ----------------------------------------------------------------------------------
208 // Default constructor
Logger()209 inline Logger::Logger() {
210 setLogSeverity(NORMAL);
211 }
212
213 // ----------------------------------------------------------------------------------
214 // Virtual destructor
~Logger()215 inline Logger::~Logger()
216 {
217 }
218
219 // ----------------------------------------------------------------------------------
220 // Construction with given logging severity
Logger(LogSeverity severity)221 inline Logger::Logger(LogSeverity severity) {
222 setLogSeverity(severity);
223 }
224
225 // ----------------------------------------------------------------------------------
226 // Log severity setter
setLogSeverity(LogSeverity log_severity)227 inline void Logger::setLogSeverity(LogSeverity log_severity){
228 m_Severity = log_severity;
229 }
230
231 // ----------------------------------------------------------------------------------
232 // Log severity getter
getLogSeverity() const233 inline Logger::LogSeverity Logger::getLogSeverity() const {
234 return m_Severity;
235 }
236
237 // ----------------------------------------------------------------------------------
debug(const std::string & message)238 inline void Logger::debug(const std::string &message)
239 {
240 return debug(message.c_str());
241 }
242
243 // ----------------------------------------------------------------------------------
error(const std::string & message)244 inline void Logger::error(const std::string &message)
245 {
246 return error(message.c_str());
247 }
248
249 // ----------------------------------------------------------------------------------
warn(const std::string & message)250 inline void Logger::warn(const std::string &message)
251 {
252 return warn(message.c_str());
253 }
254
255 // ----------------------------------------------------------------------------------
info(const std::string & message)256 inline void Logger::info(const std::string &message)
257 {
258 return info(message.c_str());
259 }
260
261 // ----------------------------------------------------------------------------------
262
263 } // Namespace Assimp
264
265 #endif // !! INCLUDED_AI_LOGGER_H
266