vrshoot

annotate libs/assimp/DefaultLogger.cpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 ---------------------------------------------------------------------------
nuclear@0 3 Open Asset Import Library (assimp)
nuclear@0 4 ---------------------------------------------------------------------------
nuclear@0 5
nuclear@0 6 Copyright (c) 2006-2012, assimp team
nuclear@0 7
nuclear@0 8 All rights reserved.
nuclear@0 9
nuclear@0 10 Redistribution and use of this software in source and binary forms,
nuclear@0 11 with or without modification, are permitted provided that the following
nuclear@0 12 conditions are met:
nuclear@0 13
nuclear@0 14 * Redistributions of source code must retain the above
nuclear@0 15 copyright notice, this list of conditions and the
nuclear@0 16 following disclaimer.
nuclear@0 17
nuclear@0 18 * Redistributions in binary form must reproduce the above
nuclear@0 19 copyright notice, this list of conditions and the
nuclear@0 20 following disclaimer in the documentation and/or other
nuclear@0 21 materials provided with the distribution.
nuclear@0 22
nuclear@0 23 * Neither the name of the assimp team, nor the names of its
nuclear@0 24 contributors may be used to endorse or promote products
nuclear@0 25 derived from this software without specific prior
nuclear@0 26 written permission of the assimp team.
nuclear@0 27
nuclear@0 28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 39 ---------------------------------------------------------------------------
nuclear@0 40 */
nuclear@0 41
nuclear@0 42 /** @file DefaultLogger.cpp
nuclear@0 43 * @brief Implementation of DefaultLogger (and Logger)
nuclear@0 44 */
nuclear@0 45
nuclear@0 46 #include "AssimpPCH.h"
nuclear@0 47 #include "DefaultIOSystem.h"
nuclear@0 48
nuclear@0 49 // Default log streams
nuclear@0 50 #include "Win32DebugLogStream.h"
nuclear@0 51 #include "StdOStreamLogStream.h"
nuclear@0 52 #include "FileLogStream.h"
nuclear@0 53
nuclear@0 54 #ifndef ASSIMP_BUILD_SINGLETHREADED
nuclear@0 55 # include <boost/thread/thread.hpp>
nuclear@0 56 # include <boost/thread/mutex.hpp>
nuclear@0 57
nuclear@0 58 boost::mutex loggerMutex;
nuclear@0 59 #endif
nuclear@0 60
nuclear@0 61 namespace Assimp {
nuclear@0 62
nuclear@0 63 // ----------------------------------------------------------------------------------
nuclear@0 64 NullLogger DefaultLogger::s_pNullLogger;
nuclear@0 65 Logger *DefaultLogger::m_pLogger = &DefaultLogger::s_pNullLogger;
nuclear@0 66
nuclear@0 67 static const unsigned int SeverityAll = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
nuclear@0 68
nuclear@0 69 // ----------------------------------------------------------------------------------
nuclear@0 70 // Represents a log-stream + its error severity
nuclear@0 71 struct LogStreamInfo
nuclear@0 72 {
nuclear@0 73 unsigned int m_uiErrorSeverity;
nuclear@0 74 LogStream *m_pStream;
nuclear@0 75
nuclear@0 76 // Constructor
nuclear@0 77 LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
nuclear@0 78 m_uiErrorSeverity( uiErrorSev ),
nuclear@0 79 m_pStream( pStream )
nuclear@0 80 {
nuclear@0 81 // empty
nuclear@0 82 }
nuclear@0 83
nuclear@0 84 // Destructor
nuclear@0 85 ~LogStreamInfo()
nuclear@0 86 {
nuclear@0 87 delete m_pStream;
nuclear@0 88 }
nuclear@0 89 };
nuclear@0 90
nuclear@0 91 // ----------------------------------------------------------------------------------
nuclear@0 92 // Construct a default log stream
nuclear@0 93 LogStream* LogStream::createDefaultStream(aiDefaultLogStream streams,
nuclear@0 94 const char* name /*= "AssimpLog.txt"*/,
nuclear@0 95 IOSystem* io /*= NULL*/)
nuclear@0 96 {
nuclear@0 97 switch (streams)
nuclear@0 98 {
nuclear@0 99 // This is a platform-specific feature
nuclear@0 100 case aiDefaultLogStream_DEBUGGER:
nuclear@0 101 #ifdef WIN32
nuclear@0 102 return new Win32DebugLogStream();
nuclear@0 103 #else
nuclear@0 104 return NULL;
nuclear@0 105 #endif
nuclear@0 106
nuclear@0 107 // Platform-independent default streams
nuclear@0 108 case aiDefaultLogStream_STDERR:
nuclear@0 109 return new StdOStreamLogStream(std::cerr);
nuclear@0 110 case aiDefaultLogStream_STDOUT:
nuclear@0 111 return new StdOStreamLogStream(std::cout);
nuclear@0 112 case aiDefaultLogStream_FILE:
nuclear@0 113 return (name && *name ? new FileLogStream(name,io) : NULL);
nuclear@0 114 default:
nuclear@0 115 // We don't know this default log stream, so raise an assertion
nuclear@0 116 ai_assert(false);
nuclear@0 117
nuclear@0 118 };
nuclear@0 119
nuclear@0 120 // For compilers without dead code path detection
nuclear@0 121 return NULL;
nuclear@0 122 }
nuclear@0 123
nuclear@0 124 // ----------------------------------------------------------------------------------
nuclear@0 125 // Creates the only singleton instance
nuclear@0 126 Logger *DefaultLogger::create(const char* name /*= "AssimpLog.txt"*/,
nuclear@0 127 LogSeverity severity /*= NORMAL*/,
nuclear@0 128 unsigned int defStreams /*= aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_FILE*/,
nuclear@0 129 IOSystem* io /*= NULL*/)
nuclear@0 130 {
nuclear@0 131 // enter the mutex here to avoid concurrency problems
nuclear@0 132 #ifndef ASSIMP_BUILD_SINGLETHREADED
nuclear@0 133 boost::mutex::scoped_lock lock(loggerMutex);
nuclear@0 134 #endif
nuclear@0 135
nuclear@0 136 if (m_pLogger && !isNullLogger() )
nuclear@0 137 delete m_pLogger;
nuclear@0 138
nuclear@0 139 m_pLogger = new DefaultLogger( severity );
nuclear@0 140
nuclear@0 141 // Attach default log streams
nuclear@0 142 // Stream the log to the MSVC debugger?
nuclear@0 143 if (defStreams & aiDefaultLogStream_DEBUGGER)
nuclear@0 144 m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_DEBUGGER));
nuclear@0 145
nuclear@0 146 // Stream the log to COUT?
nuclear@0 147 if (defStreams & aiDefaultLogStream_STDOUT)
nuclear@0 148 m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_STDOUT));
nuclear@0 149
nuclear@0 150 // Stream the log to CERR?
nuclear@0 151 if (defStreams & aiDefaultLogStream_STDERR)
nuclear@0 152 m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_STDERR));
nuclear@0 153
nuclear@0 154 // Stream the log to a file
nuclear@0 155 if (defStreams & aiDefaultLogStream_FILE && name && *name)
nuclear@0 156 m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_FILE,name,io));
nuclear@0 157
nuclear@0 158 return m_pLogger;
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 // ----------------------------------------------------------------------------------
nuclear@0 162 void Logger::debug(const char* message) {
nuclear@0 163
nuclear@0 164 // SECURITY FIX: otherwise it's easy to produce overruns since
nuclear@0 165 // sometimes importers will include data from the input file
nuclear@0 166 // (i.e. node names) in their messages.
nuclear@0 167 if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
nuclear@0 168 ai_assert(false);
nuclear@0 169 return;
nuclear@0 170 }
nuclear@0 171 return OnDebug(message);
nuclear@0 172 }
nuclear@0 173
nuclear@0 174 // ----------------------------------------------------------------------------------
nuclear@0 175 void Logger::info(const char* message) {
nuclear@0 176
nuclear@0 177 // SECURITY FIX: see above
nuclear@0 178 if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
nuclear@0 179 ai_assert(false);
nuclear@0 180 return;
nuclear@0 181 }
nuclear@0 182 return OnInfo(message);
nuclear@0 183 }
nuclear@0 184
nuclear@0 185 // ----------------------------------------------------------------------------------
nuclear@0 186 void Logger::warn(const char* message) {
nuclear@0 187
nuclear@0 188 // SECURITY FIX: see above
nuclear@0 189 if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
nuclear@0 190 ai_assert(false);
nuclear@0 191 return;
nuclear@0 192 }
nuclear@0 193 return OnWarn(message);
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 // ----------------------------------------------------------------------------------
nuclear@0 197 void Logger::error(const char* message) {
nuclear@0 198
nuclear@0 199 // SECURITY FIX: see above
nuclear@0 200 if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
nuclear@0 201 ai_assert(false);
nuclear@0 202 return;
nuclear@0 203 }
nuclear@0 204 return OnError(message);
nuclear@0 205 }
nuclear@0 206
nuclear@0 207 // ----------------------------------------------------------------------------------
nuclear@0 208 void DefaultLogger::set( Logger *logger )
nuclear@0 209 {
nuclear@0 210 // enter the mutex here to avoid concurrency problems
nuclear@0 211 #ifndef ASSIMP_BUILD_SINGLETHREADED
nuclear@0 212 boost::mutex::scoped_lock lock(loggerMutex);
nuclear@0 213 #endif
nuclear@0 214
nuclear@0 215 if (!logger)logger = &s_pNullLogger;
nuclear@0 216 if (m_pLogger && !isNullLogger() )
nuclear@0 217 delete m_pLogger;
nuclear@0 218
nuclear@0 219 DefaultLogger::m_pLogger = logger;
nuclear@0 220 }
nuclear@0 221
nuclear@0 222 // ----------------------------------------------------------------------------------
nuclear@0 223 bool DefaultLogger::isNullLogger()
nuclear@0 224 {
nuclear@0 225 return m_pLogger == &s_pNullLogger;
nuclear@0 226 }
nuclear@0 227
nuclear@0 228 // ----------------------------------------------------------------------------------
nuclear@0 229 // Singleton getter
nuclear@0 230 Logger *DefaultLogger::get()
nuclear@0 231 {
nuclear@0 232 return m_pLogger;
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 // ----------------------------------------------------------------------------------
nuclear@0 236 // Kills the only instance
nuclear@0 237 void DefaultLogger::kill()
nuclear@0 238 {
nuclear@0 239 // enter the mutex here to avoid concurrency problems
nuclear@0 240 #ifndef ASSIMP_BUILD_SINGLETHREADED
nuclear@0 241 boost::mutex::scoped_lock lock(loggerMutex);
nuclear@0 242 #endif
nuclear@0 243
nuclear@0 244 if (m_pLogger == &s_pNullLogger)return;
nuclear@0 245 delete m_pLogger;
nuclear@0 246 m_pLogger = &s_pNullLogger;
nuclear@0 247 }
nuclear@0 248
nuclear@0 249 // ----------------------------------------------------------------------------------
nuclear@0 250 // Debug message
nuclear@0 251 void DefaultLogger::OnDebug( const char* message )
nuclear@0 252 {
nuclear@0 253 if ( m_Severity == Logger::NORMAL )
nuclear@0 254 return;
nuclear@0 255
nuclear@0 256 char msg[MAX_LOG_MESSAGE_LENGTH*2];
nuclear@0 257 ::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message );
nuclear@0 258
nuclear@0 259 WriteToStreams( msg, Logger::Debugging );
nuclear@0 260 }
nuclear@0 261
nuclear@0 262 // ----------------------------------------------------------------------------------
nuclear@0 263 // Logs an info
nuclear@0 264 void DefaultLogger::OnInfo( const char* message )
nuclear@0 265 {
nuclear@0 266 char msg[MAX_LOG_MESSAGE_LENGTH*2];
nuclear@0 267 ::sprintf(msg,"Info, T%i: %s", GetThreadID(), message );
nuclear@0 268
nuclear@0 269 WriteToStreams( msg , Logger::Info );
nuclear@0 270 }
nuclear@0 271
nuclear@0 272 // ----------------------------------------------------------------------------------
nuclear@0 273 // Logs a warning
nuclear@0 274 void DefaultLogger::OnWarn( const char* message )
nuclear@0 275 {
nuclear@0 276 char msg[MAX_LOG_MESSAGE_LENGTH*2];
nuclear@0 277 ::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message );
nuclear@0 278
nuclear@0 279 WriteToStreams( msg, Logger::Warn );
nuclear@0 280 }
nuclear@0 281
nuclear@0 282 // ----------------------------------------------------------------------------------
nuclear@0 283 // Logs an error
nuclear@0 284 void DefaultLogger::OnError( const char* message )
nuclear@0 285 {
nuclear@0 286 char msg[MAX_LOG_MESSAGE_LENGTH*2];
nuclear@0 287 ::sprintf(msg,"Error, T%i: %s", GetThreadID(), message );
nuclear@0 288
nuclear@0 289 WriteToStreams( msg, Logger::Err );
nuclear@0 290 }
nuclear@0 291
nuclear@0 292 // ----------------------------------------------------------------------------------
nuclear@0 293 // Will attach a new stream
nuclear@0 294 bool DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
nuclear@0 295 {
nuclear@0 296 if (!pStream)
nuclear@0 297 return false;
nuclear@0 298
nuclear@0 299 if (0 == severity) {
nuclear@0 300 severity = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
nuclear@0 301 }
nuclear@0 302
nuclear@0 303 for ( StreamIt it = m_StreamArray.begin();
nuclear@0 304 it != m_StreamArray.end();
nuclear@0 305 ++it )
nuclear@0 306 {
nuclear@0 307 if ( (*it)->m_pStream == pStream )
nuclear@0 308 {
nuclear@0 309 (*it)->m_uiErrorSeverity |= severity;
nuclear@0 310 return true;
nuclear@0 311 }
nuclear@0 312 }
nuclear@0 313
nuclear@0 314 LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
nuclear@0 315 m_StreamArray.push_back( pInfo );
nuclear@0 316 return true;
nuclear@0 317 }
nuclear@0 318
nuclear@0 319 // ----------------------------------------------------------------------------------
nuclear@0 320 // Detatch a stream
nuclear@0 321 bool DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
nuclear@0 322 {
nuclear@0 323 if (!pStream)
nuclear@0 324 return false;
nuclear@0 325
nuclear@0 326 if (0 == severity) {
nuclear@0 327 severity = SeverityAll;
nuclear@0 328 }
nuclear@0 329
nuclear@0 330 for ( StreamIt it = m_StreamArray.begin();
nuclear@0 331 it != m_StreamArray.end();
nuclear@0 332 ++it )
nuclear@0 333 {
nuclear@0 334 if ( (*it)->m_pStream == pStream )
nuclear@0 335 {
nuclear@0 336 (*it)->m_uiErrorSeverity &= ~severity;
nuclear@0 337 if ( (*it)->m_uiErrorSeverity == 0 )
nuclear@0 338 {
nuclear@0 339 // don't delete the underlying stream 'cause the caller gains ownership again
nuclear@0 340 (**it).m_pStream = NULL;
nuclear@0 341 delete *it;
nuclear@0 342 m_StreamArray.erase( it );
nuclear@0 343 break;
nuclear@0 344 }
nuclear@0 345 return true;
nuclear@0 346 }
nuclear@0 347 }
nuclear@0 348 return false;
nuclear@0 349 }
nuclear@0 350
nuclear@0 351 // ----------------------------------------------------------------------------------
nuclear@0 352 // Constructor
nuclear@0 353 DefaultLogger::DefaultLogger(LogSeverity severity)
nuclear@0 354
nuclear@0 355 : Logger ( severity )
nuclear@0 356 , noRepeatMsg (false)
nuclear@0 357 , lastLen( 0 )
nuclear@0 358 {
nuclear@0 359 lastMsg[0] = '\0';
nuclear@0 360 }
nuclear@0 361
nuclear@0 362 // ----------------------------------------------------------------------------------
nuclear@0 363 // Destructor
nuclear@0 364 DefaultLogger::~DefaultLogger()
nuclear@0 365 {
nuclear@0 366 for ( StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it ) {
nuclear@0 367 // also frees the underlying stream, we are its owner.
nuclear@0 368 delete *it;
nuclear@0 369 }
nuclear@0 370 }
nuclear@0 371
nuclear@0 372 // ----------------------------------------------------------------------------------
nuclear@0 373 // Writes message to stream
nuclear@0 374 void DefaultLogger::WriteToStreams(const char *message,
nuclear@0 375 ErrorSeverity ErrorSev )
nuclear@0 376 {
nuclear@0 377 ai_assert(NULL != message);
nuclear@0 378
nuclear@0 379 // Check whether this is a repeated message
nuclear@0 380 if (! ::strncmp( message,lastMsg, lastLen-1))
nuclear@0 381 {
nuclear@0 382 if (!noRepeatMsg)
nuclear@0 383 {
nuclear@0 384 noRepeatMsg = true;
nuclear@0 385 message = "Skipping one or more lines with the same contents\n";
nuclear@0 386 }
nuclear@0 387 else return;
nuclear@0 388 }
nuclear@0 389 else
nuclear@0 390 {
nuclear@0 391 // append a new-line character to the message to be printed
nuclear@0 392 lastLen = ::strlen(message);
nuclear@0 393 ::memcpy(lastMsg,message,lastLen+1);
nuclear@0 394 ::strcat(lastMsg+lastLen,"\n");
nuclear@0 395
nuclear@0 396 message = lastMsg;
nuclear@0 397 noRepeatMsg = false;
nuclear@0 398 ++lastLen;
nuclear@0 399 }
nuclear@0 400 for ( ConstStreamIt it = m_StreamArray.begin();
nuclear@0 401 it != m_StreamArray.end();
nuclear@0 402 ++it)
nuclear@0 403 {
nuclear@0 404 if ( ErrorSev & (*it)->m_uiErrorSeverity )
nuclear@0 405 (*it)->m_pStream->write( message);
nuclear@0 406 }
nuclear@0 407 }
nuclear@0 408
nuclear@0 409 // ----------------------------------------------------------------------------------
nuclear@0 410 // Returns thread id, if not supported only a zero will be returned.
nuclear@0 411 unsigned int DefaultLogger::GetThreadID()
nuclear@0 412 {
nuclear@0 413 // fixme: we can get this value via boost::threads
nuclear@0 414 #ifdef WIN32
nuclear@0 415 return (unsigned int)::GetCurrentThreadId();
nuclear@0 416 #else
nuclear@0 417 return 0; // not supported
nuclear@0 418 #endif
nuclear@0 419 }
nuclear@0 420
nuclear@0 421 // ----------------------------------------------------------------------------------
nuclear@0 422
nuclear@0 423 } // !namespace Assimp