DefaultLogger.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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 std::string &message) {
  132. // SECURITY FIX: otherwise it's easy to produce overruns ...
  133. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  134. ai_assert(false);
  135. return;
  136. }
  137. return OnDebug(message.c_str());
  138. }
  139. // ----------------------------------------------------------------------------------
  140. void Logger::info(const std::string &message) {
  141. // SECURITY FIX: otherwise it's easy to produce overruns ...
  142. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  143. ai_assert(false);
  144. return;
  145. }
  146. return OnInfo(message.c_str());
  147. }
  148. // ----------------------------------------------------------------------------------
  149. void Logger::warn(const std::string &message) {
  150. // SECURITY FIX: otherwise it's easy to produce overruns ...
  151. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  152. ai_assert(false);
  153. return;
  154. }
  155. return OnWarn(message.c_str());
  156. }
  157. // ----------------------------------------------------------------------------------
  158. void Logger::error(const std::string &message) {
  159. // SECURITY FIX: otherwise it's easy to produce overruns ...
  160. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  161. ai_assert(false);
  162. return;
  163. }
  164. return OnError(message.c_str());
  165. }
  166. // ----------------------------------------------------------------------------------
  167. void DefaultLogger::set( Logger *logger )
  168. {
  169. // enter the mutex here to avoid concurrency problems
  170. #ifndef ASSIMP_BUILD_SINGLETHREADED
  171. boost::mutex::scoped_lock lock(loggerMutex);
  172. #endif
  173. if (!logger)logger = &s_pNullLogger;
  174. if (m_pLogger && !isNullLogger() )
  175. delete m_pLogger;
  176. DefaultLogger::m_pLogger = logger;
  177. }
  178. // ----------------------------------------------------------------------------------
  179. bool DefaultLogger::isNullLogger()
  180. {
  181. return m_pLogger == &s_pNullLogger;
  182. }
  183. // ----------------------------------------------------------------------------------
  184. // Singleton getter
  185. Logger *DefaultLogger::get()
  186. {
  187. return m_pLogger;
  188. }
  189. // ----------------------------------------------------------------------------------
  190. // Kills the only instance
  191. void DefaultLogger::kill()
  192. {
  193. // enter the mutex here to avoid concurrency problems
  194. #ifndef ASSIMP_BUILD_SINGLETHREADED
  195. boost::mutex::scoped_lock lock(loggerMutex);
  196. #endif
  197. if (m_pLogger == &s_pNullLogger)return;
  198. delete m_pLogger;
  199. m_pLogger = &s_pNullLogger;
  200. }
  201. // ----------------------------------------------------------------------------------
  202. // Debug message
  203. void DefaultLogger::OnDebug( const char* message )
  204. {
  205. if ( m_Severity == Logger::NORMAL )
  206. return;
  207. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  208. ::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message );
  209. WriteToStreams( msg, Logger::Debugging );
  210. }
  211. // ----------------------------------------------------------------------------------
  212. // Logs an info
  213. void DefaultLogger::OnInfo( const char* message )
  214. {
  215. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  216. ::sprintf(msg,"Info, T%i: %s", GetThreadID(), message );
  217. WriteToStreams( msg , Logger::Info );
  218. }
  219. // ----------------------------------------------------------------------------------
  220. // Logs a warning
  221. void DefaultLogger::OnWarn( const char* message )
  222. {
  223. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  224. ::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message );
  225. WriteToStreams( msg, Logger::Warn );
  226. }
  227. // ----------------------------------------------------------------------------------
  228. // Logs an error
  229. void DefaultLogger::OnError( const char* message )
  230. {
  231. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  232. ::sprintf(msg,"Error, T%i: %s", GetThreadID(), message );
  233. WriteToStreams( msg, Logger::Err );
  234. }
  235. // ----------------------------------------------------------------------------------
  236. // Will attach a new stream
  237. bool DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
  238. {
  239. if (!pStream)
  240. return false;
  241. if (0 == severity) {
  242. severity = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
  243. }
  244. for ( StreamIt it = m_StreamArray.begin();
  245. it != m_StreamArray.end();
  246. ++it )
  247. {
  248. if ( (*it)->m_pStream == pStream )
  249. {
  250. (*it)->m_uiErrorSeverity |= severity;
  251. return true;
  252. }
  253. }
  254. LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
  255. m_StreamArray.push_back( pInfo );
  256. return true;
  257. }
  258. // ----------------------------------------------------------------------------------
  259. // Detatch a stream
  260. bool DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
  261. {
  262. if (!pStream)
  263. return false;
  264. if (0 == severity) {
  265. severity = SeverityAll;
  266. }
  267. for ( StreamIt it = m_StreamArray.begin();
  268. it != m_StreamArray.end();
  269. ++it )
  270. {
  271. if ( (*it)->m_pStream == pStream )
  272. {
  273. (*it)->m_uiErrorSeverity &= ~severity;
  274. if ( (*it)->m_uiErrorSeverity == 0 )
  275. {
  276. // don't delete the underlying stream 'cause the caller gains ownership again
  277. (**it).m_pStream = NULL;
  278. delete *it;
  279. m_StreamArray.erase( it );
  280. break;
  281. }
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. // ----------------------------------------------------------------------------------
  288. // Constructor
  289. DefaultLogger::DefaultLogger(LogSeverity severity)
  290. : Logger ( severity )
  291. , noRepeatMsg (false)
  292. , lastLen( 0 )
  293. {
  294. lastMsg[0] = '\0';
  295. }
  296. // ----------------------------------------------------------------------------------
  297. // Destructor
  298. DefaultLogger::~DefaultLogger()
  299. {
  300. for ( StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it ) {
  301. // also frees the underlying stream, we are its owner.
  302. delete *it;
  303. }
  304. }
  305. // ----------------------------------------------------------------------------------
  306. // Writes message to stream
  307. void DefaultLogger::WriteToStreams(const char *message,
  308. ErrorSeverity ErrorSev )
  309. {
  310. ai_assert(NULL != message);
  311. // Check whether this is a repeated message
  312. if (! ::strncmp( message,lastMsg, lastLen-1))
  313. {
  314. if (!noRepeatMsg)
  315. {
  316. noRepeatMsg = true;
  317. message = "Skipping one or more lines with the same contents\n";
  318. }
  319. else return;
  320. }
  321. else
  322. {
  323. // append a new-line character to the message to be printed
  324. lastLen = ::strlen(message);
  325. ::memcpy(lastMsg,message,lastLen+1);
  326. ::strcat(lastMsg+lastLen,"\n");
  327. message = lastMsg;
  328. noRepeatMsg = false;
  329. ++lastLen;
  330. }
  331. for ( ConstStreamIt it = m_StreamArray.begin();
  332. it != m_StreamArray.end();
  333. ++it)
  334. {
  335. if ( ErrorSev & (*it)->m_uiErrorSeverity )
  336. (*it)->m_pStream->write( message);
  337. }
  338. }
  339. // ----------------------------------------------------------------------------------
  340. // Returns thread id, if not supported only a zero will be returned.
  341. unsigned int DefaultLogger::GetThreadID()
  342. {
  343. // fixme: we can get this value via boost::threads
  344. #ifdef WIN32
  345. return (unsigned int)::GetCurrentThreadId();
  346. #else
  347. return 0; // not supported
  348. #endif
  349. }
  350. // ----------------------------------------------------------------------------------
  351. } // !namespace Assimp