DefaultLogger.cpp 13 KB

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