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