miniassimp

view include/miniassimp/Logger.hpp @ 0:879c81d94345

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