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