Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

424 lignes
12 KiB

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