| 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 | /** |
| 43 | * @file DefaultLogger.hpp |
| 44 | */ |
| 45 | |
| 46 | #pragma once |
| 47 | #ifndef INCLUDED_AI_DEFAULTLOGGER |
| 48 | #define INCLUDED_AI_DEFAULTLOGGER |
| 49 | |
| 50 | #ifdef __GNUC__ |
| 51 | # pragma GCC system_header |
| 52 | #endif |
| 53 | |
| 54 | #include "LogStream.hpp" |
| 55 | #include "Logger.hpp" |
| 56 | #include "NullLogger.hpp" |
| 57 | #include <vector> |
| 58 | |
| 59 | #ifndef ASSIMP_BUILD_SINGLETHREADED |
| 60 | #include <mutex> |
| 61 | #include <thread> |
| 62 | #endif |
| 63 | |
| 64 | namespace Assimp { |
| 65 | // ------------------------------------------------------------------------------------ |
| 66 | class IOStream; |
| 67 | struct LogStreamInfo; |
| 68 | |
| 69 | /** default name of log-file */ |
| 70 | #define ASSIMP_DEFAULT_LOG_NAME "AssimpLog.txt" |
| 71 | |
| 72 | // ------------------------------------------------------------------------------------ |
| 73 | /** @brief CPP-API: Primary logging facility of Assimp. |
| 74 | * |
| 75 | * The library stores its primary #Logger as a static member of this class. |
| 76 | * #get() returns this primary logger. By default the underlying implementation is |
| 77 | * just a #NullLogger which rejects all log messages. By calling #create(), logging |
| 78 | * is turned on. To capture the log output multiple log streams (#LogStream) can be |
| 79 | * attach to the logger. Some default streams for common streaming locations (such as |
| 80 | * a file, std::cout, OutputDebugString()) are also provided. |
| 81 | * |
| 82 | * If you wish to customize the logging at an even deeper level supply your own |
| 83 | * implementation of #Logger to #set(). |
| 84 | * @note The whole logging stuff causes a small extra overhead for all imports. */ |
| 85 | class ASSIMP_API DefaultLogger : public Logger { |
| 86 | public: |
| 87 | // ---------------------------------------------------------------------- |
| 88 | /** @brief Creates a logging instance. |
| 89 | * @param name Name for log file. Only valid in combination |
| 90 | * with the aiDefaultLogStream_FILE flag. |
| 91 | * @param severity Log severity, DEBUG turns on debug messages and VERBOSE turns on all messages. |
| 92 | * @param defStreams Default log streams to be attached. Any bitwise |
| 93 | * combination of the aiDefaultLogStream enumerated values. |
| 94 | * If #aiDefaultLogStream_FILE is specified but an empty string is |
| 95 | * passed for 'name', no log file is created at all. |
| 96 | * @param io IOSystem to be used to open external files (such as the |
| 97 | * log file). Pass nullptr to rely on the default implementation. |
| 98 | * This replaces the default #NullLogger with a #DefaultLogger instance. */ |
| 99 | static Logger *create(const char *name = ASSIMP_DEFAULT_LOG_NAME, |
| 100 | LogSeverity severity = NORMAL, |
| 101 | unsigned int defStreams = aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_FILE, |
| 102 | IOSystem *io = nullptr); |
| 103 | |
| 104 | // ---------------------------------------------------------------------- |
| 105 | /** @brief Setup a custom #Logger implementation. |
| 106 | * |
| 107 | * Use this if the provided #DefaultLogger class doesn't fit into |
| 108 | * your needs. If the provided message formatting is OK for you, |
| 109 | * it's much easier to use #create() and to attach your own custom |
| 110 | * output streams to it. |
| 111 | * Since set is intended to be used for custom loggers, the user is |
| 112 | * responsible for instantiation and destruction (new / delete). |
| 113 | * Before deletion of the custom logger, set(nullptr); must be called. |
| 114 | * @param logger Pass NULL to setup a default NullLogger*/ |
| 115 | static void set(Logger *logger); |
| 116 | |
| 117 | // ---------------------------------------------------------------------- |
| 118 | /** @brief Getter for singleton instance |
| 119 | * @return Only instance. This is never null, but it could be a |
| 120 | * NullLogger. Use isNullLogger to check this.*/ |
| 121 | static Logger *get(); |
| 122 | |
| 123 | // ---------------------------------------------------------------------- |
| 124 | /** @brief Return whether a #NullLogger is currently active |
| 125 | * @return true if the current logger is a #NullLogger. |
| 126 | * Use create() or set() to setup a logger that does actually do |
| 127 | * something else than just rejecting all log messages. */ |
| 128 | static bool isNullLogger(); |
| 129 | |
| 130 | // ---------------------------------------------------------------------- |
| 131 | /** @brief Kills and deletes the current singleton logger and replaces |
| 132 | * it with a #NullLogger instance. */ |
| 133 | static void kill(); |
| 134 | |
| 135 | // ---------------------------------------------------------------------- |
| 136 | /** @copydoc Logger::attachStream */ |
| 137 | bool attachStream(LogStream *pStream, unsigned int severity) override; |
| 138 | |
| 139 | // ---------------------------------------------------------------------- |
| 140 | /** @copydoc Logger::detachStream */ |
| 141 | bool detachStream(LogStream *pStream, unsigned int severity) override; |
| 142 | |
| 143 | private: |
| 144 | // ---------------------------------------------------------------------- |
| 145 | /** @briefPrivate construction for internal use by create(). |
| 146 | * @param severity Logging granularity */ |
| 147 | explicit DefaultLogger(LogSeverity severity); |
| 148 | |
| 149 | // ---------------------------------------------------------------------- |
| 150 | /** @briefDestructor */ |
| 151 | ~DefaultLogger() override; |
| 152 | |
| 153 | /** @brief Logs debug infos, only been written when severity level DEBUG or higher is set */ |
| 154 | void OnDebug(const char *message) override; |
| 155 | |
| 156 | /** @brief Logs debug infos, only been written when severity level VERBOSE is set */ |
| 157 | void OnVerboseDebug(const char *message) override; |
| 158 | |
| 159 | /** @brief Logs an info message */ |
| 160 | void OnInfo(const char *message) override; |
| 161 | |
| 162 | /** @brief Logs a warning message */ |
| 163 | void OnWarn(const char *message) override; |
| 164 | |
| 165 | /** @brief Logs an error message */ |
| 166 | void OnError(const char *message) override; |
| 167 | |
| 168 | // ---------------------------------------------------------------------- |
| 169 | /** @brief Writes a message to all streams */ |
| 170 | void WriteToStreams(const char *message, ErrorSeverity ErrorSev); |
| 171 | |
| 172 | // ---------------------------------------------------------------------- |
| 173 | /** @brief Returns the thread id. |
| 174 | * @note This is an OS specific feature, if not supported, a |
| 175 | * zero will be returned. |
| 176 | */ |
| 177 | unsigned int GetThreadID(); |
| 178 | |
| 179 | private: |
| 180 | // Aliases for stream container |
| 181 | using StreamArray = std::vector<LogStreamInfo *>; |
| 182 | using StreamIt = std::vector<LogStreamInfo *>::iterator; |
| 183 | using ConstStreamIt = std::vector<LogStreamInfo *>::const_iterator; |
| 184 | |
| 185 | //! only logging instance |
| 186 | static Logger *m_pLogger; |
| 187 | static NullLogger s_pNullLogger; |
| 188 | |
| 189 | //! Attached streams |
| 190 | StreamArray m_StreamArray; |
| 191 | |
| 192 | #ifndef ASSIMP_BUILD_SINGLETHREADED |
| 193 | std::mutex m_arrayMutex; |
| 194 | #endif |
| 195 | |
| 196 | bool noRepeatMsg; |
| 197 | char lastMsg[MAX_LOG_MESSAGE_LENGTH * 2]; |
| 198 | size_t lastLen; |
| 199 | }; |
| 200 | |
| 201 | // ------------------------------------------------------------------------------------ |
| 202 | |
| 203 | } // Namespace Assimp |
| 204 | |
| 205 | #endif // !! INCLUDED_AI_DEFAULTLOGGER |
| 206 | |