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