DefaultLogger.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, 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. #include "AssimpPCH.h"
  35. #include "DefaultIOSystem.h"
  36. #include "Win32DebugLogStream.h"
  37. #include "FileLogStream.h"
  38. namespace Assimp
  39. {
  40. // ---------------------------------------------------------------------------
  41. NullLogger DefaultLogger::s_pNullLogger;
  42. Logger *DefaultLogger::m_pLogger = &DefaultLogger::s_pNullLogger;
  43. // ---------------------------------------------------------------------------
  44. //
  45. struct LogStreamInfo
  46. {
  47. unsigned int m_uiErrorSeverity;
  48. LogStream *m_pStream;
  49. // Constructor
  50. LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
  51. m_uiErrorSeverity( uiErrorSev ),
  52. m_pStream( pStream )
  53. {
  54. // empty
  55. }
  56. // Destructor
  57. ~LogStreamInfo()
  58. {
  59. // empty
  60. }
  61. };
  62. // ---------------------------------------------------------------------------
  63. // Creates the only singleton instance
  64. Logger *DefaultLogger::create(const std::string &name, LogSeverity severity)
  65. {
  66. if (m_pLogger && !isNullLogger() )
  67. delete m_pLogger;
  68. m_pLogger = new DefaultLogger( name, severity );
  69. return m_pLogger;
  70. }
  71. // ---------------------------------------------------------------------------
  72. void DefaultLogger::set( Logger *logger )
  73. {
  74. if (!logger)logger = &s_pNullLogger;
  75. if (m_pLogger && !isNullLogger() )
  76. delete m_pLogger;
  77. DefaultLogger::m_pLogger = logger;
  78. }
  79. // ---------------------------------------------------------------------------
  80. bool DefaultLogger::isNullLogger()
  81. {
  82. return m_pLogger == &s_pNullLogger;
  83. }
  84. // ---------------------------------------------------------------------------
  85. // Singleton getter
  86. Logger *DefaultLogger::get()
  87. {
  88. return m_pLogger;
  89. }
  90. // ---------------------------------------------------------------------------
  91. // Kills the only instance
  92. void DefaultLogger::kill()
  93. {
  94. if (m_pLogger != &s_pNullLogger)return;
  95. delete m_pLogger;
  96. m_pLogger = &s_pNullLogger;
  97. }
  98. // ---------------------------------------------------------------------------
  99. // Debug message
  100. void DefaultLogger::debug( const std::string &message )
  101. {
  102. if ( m_Severity == Logger::NORMAL )
  103. return;
  104. const std::string msg( "Debug, T" + getThreadID() + ": " + message );
  105. writeToStreams( msg, Logger::DEBUGGING );
  106. }
  107. // ---------------------------------------------------------------------------
  108. // Logs an info
  109. void DefaultLogger::info( const std::string &message )
  110. {
  111. const std::string msg( "Info, T" + getThreadID() + ": " + message );
  112. writeToStreams( msg , Logger::INFO );
  113. }
  114. // ---------------------------------------------------------------------------
  115. // Logs a warning
  116. void DefaultLogger::warn( const std::string &message )
  117. {
  118. const std::string msg( "Warn, T" + getThreadID() + ": "+ message );
  119. writeToStreams( msg, Logger::WARN );
  120. }
  121. // ---------------------------------------------------------------------------
  122. // Logs an error
  123. void DefaultLogger::error( const std::string &message )
  124. {
  125. const std::string msg( "Error, T"+ getThreadID() + ": " + message );
  126. writeToStreams( msg, Logger::ERR );
  127. }
  128. // ---------------------------------------------------------------------------
  129. // Severity setter
  130. void DefaultLogger::setLogSeverity( LogSeverity log_severity )
  131. {
  132. m_Severity = log_severity;
  133. }
  134. // ---------------------------------------------------------------------------
  135. // Attachs a new stream
  136. void DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
  137. {
  138. ai_assert ( NULL != pStream );
  139. // fix (Aramis)
  140. if (0 == severity)
  141. {
  142. severity = Logger::INFO | Logger::ERR | Logger::WARN | Logger::DEBUGGING;
  143. }
  144. for ( StreamIt it = m_StreamArray.begin();
  145. it != m_StreamArray.end();
  146. ++it )
  147. {
  148. if ( (*it)->m_pStream == pStream )
  149. {
  150. (*it)->m_uiErrorSeverity |= severity;
  151. return;
  152. }
  153. }
  154. LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
  155. m_StreamArray.push_back( pInfo );
  156. }
  157. // ---------------------------------------------------------------------------
  158. // Detatch a stream
  159. void DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
  160. {
  161. ai_assert ( NULL != pStream );
  162. // fix (Aramis)
  163. if (0 == severity)
  164. {
  165. severity = Logger::INFO | Logger::ERR | Logger::WARN | Logger::DEBUGGING;
  166. }
  167. for ( StreamIt it = m_StreamArray.begin();
  168. it != m_StreamArray.end();
  169. ++it )
  170. {
  171. if ( (*it)->m_pStream == pStream )
  172. {
  173. unsigned int uiSev = (*it)->m_uiErrorSeverity;
  174. if ( severity & Logger::INFO )
  175. uiSev &= ( ~Logger::INFO );
  176. if ( severity & Logger::WARN )
  177. uiSev &= ( ~Logger::WARN );
  178. if ( severity & Logger::ERR )
  179. uiSev &= ( ~Logger::ERR );
  180. // fix (Aramis)
  181. if ( severity & Logger::DEBUGGING )
  182. uiSev &= ( ~Logger::DEBUGGING );
  183. (*it)->m_uiErrorSeverity = uiSev;
  184. if ( (*it)->m_uiErrorSeverity == 0 )
  185. {
  186. it = m_StreamArray.erase( it );
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. // ---------------------------------------------------------------------------
  193. // Constructor
  194. DefaultLogger::DefaultLogger( const std::string &name, LogSeverity severity ) :
  195. m_Severity( severity )
  196. {
  197. #ifdef WIN32
  198. m_Streams.push_back( new Win32DebugLogStream() );
  199. #endif
  200. if (name.empty())
  201. return;
  202. m_Streams.push_back( new FileLogStream( name ) );
  203. noRepeatMsg = false;
  204. }
  205. // ---------------------------------------------------------------------------
  206. // Destructor
  207. DefaultLogger::~DefaultLogger()
  208. {
  209. for ( StreamIt it = m_StreamArray.begin();
  210. it != m_StreamArray.end();
  211. ++it )
  212. {
  213. delete *it;
  214. }
  215. for (std::vector<LogStream*>::iterator it = m_Streams.begin();
  216. it != m_Streams.end();
  217. ++it)
  218. {
  219. delete *it;
  220. }
  221. m_Streams.clear();
  222. }
  223. // ---------------------------------------------------------------------------
  224. // Writes message to stream
  225. void DefaultLogger::writeToStreams(const std::string &message,
  226. ErrorSeverity ErrorSev )
  227. {
  228. if ( message.empty() )
  229. return;
  230. std::string s;
  231. if (message == lastMsg)
  232. {
  233. if (!noRepeatMsg)
  234. {
  235. noRepeatMsg = true;
  236. s = "Skipping one or more lines with the same contents";
  237. }
  238. else return;
  239. }
  240. else
  241. {
  242. lastMsg = s = message;
  243. noRepeatMsg = false;
  244. }
  245. for ( ConstStreamIt it = m_StreamArray.begin();
  246. it != m_StreamArray.end();
  247. ++it)
  248. {
  249. if ( ErrorSev & (*it)->m_uiErrorSeverity )
  250. {
  251. (*it)->m_pStream->write( s);
  252. }
  253. }
  254. for (std::vector<LogStream*>::iterator it = m_Streams.begin();
  255. it != m_Streams.end();
  256. ++it)
  257. {
  258. (*it)->write( s + std::string("\n"));
  259. }
  260. }
  261. // ---------------------------------------------------------------------------
  262. // Returns thread id, if not supported only a zero will be returned.
  263. std::string DefaultLogger::getThreadID()
  264. {
  265. std::string thread_id( "0" );
  266. #ifdef WIN32
  267. HANDLE hThread = GetCurrentThread();
  268. if ( hThread )
  269. {
  270. std::stringstream thread_msg;
  271. thread_msg << ::GetCurrentThreadId() /*<< " "*/;
  272. return thread_msg.str();
  273. }
  274. else
  275. return thread_id;
  276. #else
  277. return thread_id;
  278. #endif
  279. }
  280. // ---------------------------------------------------------------------------
  281. } // Namespace Assimp