DefaultLogger.cpp 13 KB

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