DefaultLogger.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "DefaultIOSystem.h"
  38. // Default log streams
  39. #include "Win32DebugLogStream.h"
  40. #include "StdOStreamLogStream.h"
  41. #include "FileLogStream.h"
  42. #include "../include/assimp/NullLogger.hpp"
  43. #include "../include/assimp/DefaultLogger.hpp"
  44. #include "../include/assimp/ai_assert.h"
  45. #include <iostream>
  46. #include <stdio.h>
  47. #ifndef ASSIMP_BUILD_SINGLETHREADED
  48. # include <boost/thread/thread.hpp>
  49. # include <boost/thread/mutex.hpp>
  50. boost::mutex loggerMutex;
  51. #endif
  52. namespace Assimp {
  53. // ----------------------------------------------------------------------------------
  54. NullLogger DefaultLogger::s_pNullLogger;
  55. Logger *DefaultLogger::m_pLogger = &DefaultLogger::s_pNullLogger;
  56. static const unsigned int SeverityAll = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
  57. // ----------------------------------------------------------------------------------
  58. // Represents a log-stream + its error severity
  59. struct LogStreamInfo
  60. {
  61. unsigned int m_uiErrorSeverity;
  62. LogStream *m_pStream;
  63. // Constructor
  64. LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
  65. m_uiErrorSeverity( uiErrorSev ),
  66. m_pStream( pStream )
  67. {
  68. // empty
  69. }
  70. // Destructor
  71. ~LogStreamInfo()
  72. {
  73. delete m_pStream;
  74. }
  75. };
  76. // ----------------------------------------------------------------------------------
  77. // Construct a default log stream
  78. LogStream* LogStream::createDefaultStream(aiDefaultLogStream streams,
  79. const char* name /*= "AssimpLog.txt"*/,
  80. IOSystem* io /*= NULL*/)
  81. {
  82. switch (streams)
  83. {
  84. // This is a platform-specific feature
  85. case aiDefaultLogStream_DEBUGGER:
  86. #ifdef WIN32
  87. return new Win32DebugLogStream();
  88. #else
  89. return NULL;
  90. #endif
  91. // Platform-independent default streams
  92. case aiDefaultLogStream_STDERR:
  93. return new StdOStreamLogStream(std::cerr);
  94. case aiDefaultLogStream_STDOUT:
  95. return new StdOStreamLogStream(std::cout);
  96. case aiDefaultLogStream_FILE:
  97. return (name && *name ? new FileLogStream(name,io) : NULL);
  98. default:
  99. // We don't know this default log stream, so raise an assertion
  100. ai_assert(false);
  101. };
  102. // For compilers without dead code path detection
  103. return NULL;
  104. }
  105. // ----------------------------------------------------------------------------------
  106. // Creates the only singleton instance
  107. Logger *DefaultLogger::create(const char* name /*= "AssimpLog.txt"*/,
  108. LogSeverity severity /*= NORMAL*/,
  109. unsigned int defStreams /*= aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_FILE*/,
  110. IOSystem* io /*= NULL*/)
  111. {
  112. // enter the mutex here to avoid concurrency problems
  113. #ifndef ASSIMP_BUILD_SINGLETHREADED
  114. boost::mutex::scoped_lock lock(loggerMutex);
  115. #endif
  116. if (m_pLogger && !isNullLogger() )
  117. delete m_pLogger;
  118. m_pLogger = new DefaultLogger( severity );
  119. // Attach default log streams
  120. // Stream the log to the MSVC debugger?
  121. if (defStreams & aiDefaultLogStream_DEBUGGER)
  122. m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_DEBUGGER));
  123. // Stream the log to COUT?
  124. if (defStreams & aiDefaultLogStream_STDOUT)
  125. m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_STDOUT));
  126. // Stream the log to CERR?
  127. if (defStreams & aiDefaultLogStream_STDERR)
  128. m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_STDERR));
  129. // Stream the log to a file
  130. if (defStreams & aiDefaultLogStream_FILE && name && *name)
  131. m_pLogger->attachStream( LogStream::createDefaultStream(aiDefaultLogStream_FILE,name,io));
  132. return m_pLogger;
  133. }
  134. // ----------------------------------------------------------------------------------
  135. void Logger::debug(const char* message) {
  136. // SECURITY FIX: otherwise it's easy to produce overruns since
  137. // sometimes importers will include data from the input file
  138. // (i.e. node names) in their messages.
  139. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  140. ai_assert(false);
  141. return;
  142. }
  143. return OnDebug(message);
  144. }
  145. // ----------------------------------------------------------------------------------
  146. void Logger::info(const char* message) {
  147. // SECURITY FIX: see above
  148. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  149. ai_assert(false);
  150. return;
  151. }
  152. return OnInfo(message);
  153. }
  154. // ----------------------------------------------------------------------------------
  155. void Logger::warn(const char* message) {
  156. // SECURITY FIX: see above
  157. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  158. ai_assert(false);
  159. return;
  160. }
  161. return OnWarn(message);
  162. }
  163. // ----------------------------------------------------------------------------------
  164. void Logger::error(const char* message) {
  165. // SECURITY FIX: see above
  166. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  167. ai_assert(false);
  168. return;
  169. }
  170. return OnError(message);
  171. }
  172. // ----------------------------------------------------------------------------------
  173. void DefaultLogger::set( Logger *logger )
  174. {
  175. // enter the mutex here to avoid concurrency problems
  176. #ifndef ASSIMP_BUILD_SINGLETHREADED
  177. boost::mutex::scoped_lock lock(loggerMutex);
  178. #endif
  179. if (!logger)logger = &s_pNullLogger;
  180. if (m_pLogger && !isNullLogger() )
  181. delete m_pLogger;
  182. DefaultLogger::m_pLogger = logger;
  183. }
  184. // ----------------------------------------------------------------------------------
  185. bool DefaultLogger::isNullLogger()
  186. {
  187. return m_pLogger == &s_pNullLogger;
  188. }
  189. // ----------------------------------------------------------------------------------
  190. // Singleton getter
  191. Logger *DefaultLogger::get()
  192. {
  193. return m_pLogger;
  194. }
  195. // ----------------------------------------------------------------------------------
  196. // Kills the only instance
  197. void DefaultLogger::kill()
  198. {
  199. // enter the mutex here to avoid concurrency problems
  200. #ifndef ASSIMP_BUILD_SINGLETHREADED
  201. boost::mutex::scoped_lock lock(loggerMutex);
  202. #endif
  203. if (m_pLogger == &s_pNullLogger)return;
  204. delete m_pLogger;
  205. m_pLogger = &s_pNullLogger;
  206. }
  207. // ----------------------------------------------------------------------------------
  208. // Debug message
  209. void DefaultLogger::OnDebug( const char* message )
  210. {
  211. if ( m_Severity == Logger::NORMAL )
  212. return;
  213. char msg[MAX_LOG_MESSAGE_LENGTH + 16];
  214. ::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message );
  215. WriteToStreams( msg, Logger::Debugging );
  216. }
  217. // ----------------------------------------------------------------------------------
  218. // Logs an info
  219. void DefaultLogger::OnInfo( const char* message )
  220. {
  221. char msg[MAX_LOG_MESSAGE_LENGTH + 16];
  222. ::sprintf(msg,"Info, T%i: %s", GetThreadID(), message );
  223. WriteToStreams( msg , Logger::Info );
  224. }
  225. // ----------------------------------------------------------------------------------
  226. // Logs a warning
  227. void DefaultLogger::OnWarn( const char* message )
  228. {
  229. char msg[MAX_LOG_MESSAGE_LENGTH + 16];
  230. ::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message );
  231. WriteToStreams( msg, Logger::Warn );
  232. }
  233. // ----------------------------------------------------------------------------------
  234. // Logs an error
  235. void DefaultLogger::OnError( const char* message )
  236. {
  237. char msg[MAX_LOG_MESSAGE_LENGTH + 16];
  238. ::sprintf(msg,"Error, T%i: %s", GetThreadID(), message );
  239. WriteToStreams( msg, Logger::Err );
  240. }
  241. // ----------------------------------------------------------------------------------
  242. // Will attach a new stream
  243. bool DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
  244. {
  245. if (!pStream)
  246. return false;
  247. if (0 == severity) {
  248. severity = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
  249. }
  250. for ( StreamIt it = m_StreamArray.begin();
  251. it != m_StreamArray.end();
  252. ++it )
  253. {
  254. if ( (*it)->m_pStream == pStream )
  255. {
  256. (*it)->m_uiErrorSeverity |= severity;
  257. return true;
  258. }
  259. }
  260. LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
  261. m_StreamArray.push_back( pInfo );
  262. return true;
  263. }
  264. // ----------------------------------------------------------------------------------
  265. // Detatch a stream
  266. bool DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
  267. {
  268. if (!pStream)
  269. return false;
  270. if (0 == severity) {
  271. severity = SeverityAll;
  272. }
  273. for ( StreamIt it = m_StreamArray.begin();
  274. it != m_StreamArray.end();
  275. ++it )
  276. {
  277. if ( (*it)->m_pStream == pStream )
  278. {
  279. (*it)->m_uiErrorSeverity &= ~severity;
  280. if ( (*it)->m_uiErrorSeverity == 0 )
  281. {
  282. // don't delete the underlying stream 'cause the caller gains ownership again
  283. (**it).m_pStream = NULL;
  284. delete *it;
  285. m_StreamArray.erase( it );
  286. break;
  287. }
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. // ----------------------------------------------------------------------------------
  294. // Constructor
  295. DefaultLogger::DefaultLogger(LogSeverity severity)
  296. : Logger ( severity )
  297. , noRepeatMsg (false)
  298. , lastLen( 0 )
  299. {
  300. lastMsg[0] = '\0';
  301. }
  302. // ----------------------------------------------------------------------------------
  303. // Destructor
  304. DefaultLogger::~DefaultLogger()
  305. {
  306. for ( StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it ) {
  307. // also frees the underlying stream, we are its owner.
  308. delete *it;
  309. }
  310. }
  311. // ----------------------------------------------------------------------------------
  312. // Writes message to stream
  313. void DefaultLogger::WriteToStreams(const char *message,
  314. ErrorSeverity ErrorSev )
  315. {
  316. ai_assert(NULL != message);
  317. // Check whether this is a repeated message
  318. if (! ::strncmp( message,lastMsg, lastLen-1))
  319. {
  320. if (!noRepeatMsg)
  321. {
  322. noRepeatMsg = true;
  323. message = "Skipping one or more lines with the same contents\n";
  324. }
  325. else return;
  326. }
  327. else
  328. {
  329. // append a new-line character to the message to be printed
  330. lastLen = ::strlen(message);
  331. ::memcpy(lastMsg,message,lastLen+1);
  332. ::strcat(lastMsg+lastLen,"\n");
  333. message = lastMsg;
  334. noRepeatMsg = false;
  335. ++lastLen;
  336. }
  337. for ( ConstStreamIt it = m_StreamArray.begin();
  338. it != m_StreamArray.end();
  339. ++it)
  340. {
  341. if ( ErrorSev & (*it)->m_uiErrorSeverity )
  342. (*it)->m_pStream->write( message);
  343. }
  344. }
  345. // ----------------------------------------------------------------------------------
  346. // Returns thread id, if not supported only a zero will be returned.
  347. unsigned int DefaultLogger::GetThreadID()
  348. {
  349. // fixme: we can get this value via boost::threads
  350. #ifdef WIN32
  351. return (unsigned int)::GetCurrentThreadId();
  352. #else
  353. return 0; // not supported
  354. #endif
  355. }
  356. // ----------------------------------------------------------------------------------
  357. } // !namespace Assimp