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