1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2015, assimp team
6 All rights reserved.
7 
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
16 * Redistributions in binary form must reproduce the above
17   copyright notice, this list of conditions and the
18   following disclaimer in the documentation and/or other
19   materials provided with the distribution.
20 
21 * Neither the name of the assimp team, nor the names of its
22   contributors may be used to endorse or promote products
23   derived from this software without specific prior
24   written permission of the assimp team.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 ----------------------------------------------------------------------
39 */
40 /** @file DefaultLogger.hpp
41 */
42 
43 #ifndef INCLUDED_AI_DEFAULTLOGGER
44 #define INCLUDED_AI_DEFAULTLOGGER
45 
46 #include "Logger.hpp"
47 #include "LogStream.hpp"
48 #include "NullLogger.hpp"
49 #include <vector>
50 
51 namespace Assimp    {
52 // ------------------------------------------------------------------------------------
53 class IOStream;
54 struct LogStreamInfo;
55 
56 /** default name of logfile */
57 #define ASSIMP_DEFAULT_LOG_NAME "AssimpLog.txt"
58 
59 // ------------------------------------------------------------------------------------
60 /** @brief CPP-API: Primary logging facility of Assimp.
61  *
62  *  The library stores its primary #Logger as a static member of this class.
63  *  #get() returns this primary logger. By default the underlying implementation is
64  *  just a #NullLogger which rejects all log messages. By calling #create(), logging
65  *  is turned on. To capture the log output multiple log streams (#LogStream) can be
66  *  attach to the logger. Some default streams for common streaming locations (such as
67  *  a file, std::cout, OutputDebugString()) are also provided.
68  *
69  *  If you wish to customize the logging at an even deeper level supply your own
70  *  implementation of #Logger to #set().
71  *  @note The whole logging stuff causes a small extra overhead for all imports. */
72 class ASSIMP_API DefaultLogger :
73     public Logger   {
74 
75 public:
76 
77     // ----------------------------------------------------------------------
78     /** @brief Creates a logging instance.
79      *  @param name Name for log file. Only valid in combination
80      *    with the aiDefaultLogStream_FILE flag.
81      *  @param severity Log severity, VERBOSE turns on debug messages
82      *  @param defStreams  Default log streams to be attached. Any bitwise
83      *    combination of the aiDefaultLogStream enumerated values.
84      *    If #aiDefaultLogStream_FILE is specified but an empty string is
85      *    passed for 'name', no log file is created at all.
86      *  @param  io IOSystem to be used to open external files (such as the
87      *   log file). Pass NULL to rely on the default implementation.
88      *  This replaces the default #NullLogger with a #DefaultLogger instance. */
89     static Logger *create(const char* name = ASSIMP_DEFAULT_LOG_NAME,
90         LogSeverity severity    = NORMAL,
91         unsigned int defStreams = aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_FILE,
92         IOSystem* io            = NULL);
93 
94     // ----------------------------------------------------------------------
95     /** @brief Setup a custom #Logger implementation.
96      *
97      *  Use this if the provided #DefaultLogger class doesn't fit into
98      *  your needs. If the provided message formatting is OK for you,
99      *  it's much easier to use #create() and to attach your own custom
100      *  output streams to it.
101      *  @param logger Pass NULL to setup a default NullLogger*/
102     static void set (Logger *logger);
103 
104     // ----------------------------------------------------------------------
105     /** @brief  Getter for singleton instance
106      *   @return Only instance. This is never null, but it could be a
107      *  NullLogger. Use isNullLogger to check this.*/
108     static Logger *get();
109 
110     // ----------------------------------------------------------------------
111     /** @brief  Return whether a #NullLogger is currently active
112      *  @return true if the current logger is a #NullLogger.
113      *  Use create() or set() to setup a logger that does actually do
114      *  something else than just rejecting all log messages. */
115     static bool isNullLogger();
116 
117     // ----------------------------------------------------------------------
118     /** @brief  Kills the current singleton logger and replaces it with a
119      *  #NullLogger instance. */
120     static void kill();
121 
122     // ----------------------------------------------------------------------
123     /** @copydoc Logger::attachStream   */
124     bool attachStream(LogStream *pStream,
125         unsigned int severity);
126 
127     // ----------------------------------------------------------------------
128     /** @copydoc Logger::detatchStream */
129     bool detatchStream(LogStream *pStream,
130         unsigned int severity);
131 
132 
133 private:
134 
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 private:
145 
146     /** @brief  Logs debug infos, only been written when severity level VERBOSE is set */
147     void OnDebug(const char* message);
148 
149     /** @brief  Logs an info message */
150     void OnInfo(const char*  message);
151 
152     /** @brief  Logs a warning message */
153     void OnWarn(const char*  message);
154 
155     /** @brief  Logs an error message */
156     void OnError(const char* message);
157 
158     // ----------------------------------------------------------------------
159     /** @brief Writes a message to all streams */
160     void WriteToStreams(const char* message, ErrorSeverity ErrorSev );
161 
162     // ----------------------------------------------------------------------
163     /** @brief Returns the thread id.
164      *  @note This is an OS specific feature, if not supported, a
165      *    zero will be returned.
166      */
167     unsigned int GetThreadID();
168 
169 private:
170     //  Aliases for stream container
171     typedef std::vector<LogStreamInfo*> StreamArray;
172     typedef std::vector<LogStreamInfo*>::iterator StreamIt;
173     typedef std::vector<LogStreamInfo*>::const_iterator ConstStreamIt;
174 
175     //! only logging instance
176     static Logger *m_pLogger;
177     static NullLogger s_pNullLogger;
178 
179     //! Attached streams
180     StreamArray m_StreamArray;
181 
182     bool noRepeatMsg;
183     char lastMsg[MAX_LOG_MESSAGE_LENGTH*2];
184     size_t lastLen;
185 };
186 // ------------------------------------------------------------------------------------
187 
188 } // Namespace Assimp
189 
190 #endif // !! INCLUDED_AI_DEFAULTLOGGER
191