DefaultLogger.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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. return;
  141. }
  142. return OnDebug(message);
  143. }
  144. // ----------------------------------------------------------------------------------
  145. void Logger::info(const char* message) {
  146. // SECURITY FIX: see above
  147. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  148. return;
  149. }
  150. return OnInfo(message);
  151. }
  152. // ----------------------------------------------------------------------------------
  153. void Logger::warn(const char* message) {
  154. // SECURITY FIX: see above
  155. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  156. return;
  157. }
  158. return OnWarn(message);
  159. }
  160. // ----------------------------------------------------------------------------------
  161. void Logger::error(const char* message) {
  162. // SECURITY FIX: see above
  163. if (strlen(message)>MAX_LOG_MESSAGE_LENGTH) {
  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%u: %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%u: %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%u: %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%u: %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