DefaultLogger.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ---------------------------------------------------------------------------
  3. Free 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 "DefaultLogger.h"
  35. #include "aiAssert.h"
  36. #include "DefaultIOSystem.h"
  37. #include "Win32DebugLogStream.h"
  38. #include "IOStream.h"
  39. #include "FileLogStream.h"
  40. #include <iostream>
  41. namespace Assimp
  42. {
  43. // ---------------------------------------------------------------------------
  44. DefaultLogger *DefaultLogger::m_pLogger = NULL;
  45. // ---------------------------------------------------------------------------
  46. //
  47. struct LogStreamInfo
  48. {
  49. unsigned int m_uiErrorSeverity;
  50. LogStream *m_pStream;
  51. // Constructor
  52. LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
  53. m_uiErrorSeverity( uiErrorSev ),
  54. m_pStream( pStream )
  55. {
  56. // empty
  57. }
  58. };
  59. // ---------------------------------------------------------------------------
  60. // Creates the only singleton instance
  61. Logger *DefaultLogger::create(const std::string &name, LogSeverity severity)
  62. {
  63. ai_assert (NULL == m_pLogger);
  64. m_pLogger = new DefaultLogger( name, severity );
  65. return m_pLogger;
  66. }
  67. // ---------------------------------------------------------------------------
  68. // Singleton getter
  69. Logger *DefaultLogger::get()
  70. {
  71. ai_assert (NULL != m_pLogger);
  72. return m_pLogger;
  73. }
  74. // ---------------------------------------------------------------------------
  75. // Kills the only instance
  76. void DefaultLogger::kill()
  77. {
  78. ai_assert (NULL != m_pLogger);
  79. delete m_pLogger;
  80. m_pLogger = NULL;
  81. }
  82. // ---------------------------------------------------------------------------
  83. // Debug message
  84. void DefaultLogger::debug(const std::string &message)
  85. {
  86. if ( m_Severity == Logger::NORMAL )
  87. return;
  88. const std::string msg( "Debug: " + message );
  89. writeToStreams( msg, Logger::DEBUGGING );
  90. }
  91. // ---------------------------------------------------------------------------
  92. // Logs an info
  93. void DefaultLogger::info(const std::string &message)
  94. {
  95. const std::string msg( "Info: " + message );
  96. writeToStreams( msg , Logger::INFO );
  97. }
  98. // ---------------------------------------------------------------------------
  99. // Logs a warning
  100. void DefaultLogger::warn( const std::string &message )
  101. {
  102. const std::string msg( "Warn: " + message );
  103. writeToStreams( msg, Logger::WARN );
  104. }
  105. // ---------------------------------------------------------------------------
  106. // Logs an error
  107. void DefaultLogger::error( const std::string &message )
  108. {
  109. const std::string msg( "Error: " + message );
  110. writeToStreams( msg, Logger::ERR );
  111. }
  112. // ---------------------------------------------------------------------------
  113. // Severity setter
  114. void DefaultLogger::setLogSeverity( LogSeverity log_severity )
  115. {
  116. m_Severity = log_severity;
  117. }
  118. // ---------------------------------------------------------------------------
  119. // Attachs a new stream
  120. void DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
  121. {
  122. ai_assert ( NULL != pStream );
  123. LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
  124. m_StreamArray.push_back( pInfo );
  125. }
  126. // ---------------------------------------------------------------------------
  127. // Detatch a stream
  128. void DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
  129. {
  130. ai_assert ( NULL != pStream );
  131. for ( StreamIt it = m_StreamArray.begin();
  132. it != m_StreamArray.end();
  133. ++it )
  134. {
  135. if ( (*it)->m_pStream == pStream )
  136. {
  137. unsigned int uiSev = (*it)->m_uiErrorSeverity;
  138. if ( severity & Logger::INFO )
  139. uiSev &= ( ~Logger::INFO );
  140. if ( severity & Logger::WARN )
  141. uiSev &= ( ~Logger::WARN );
  142. if ( severity & Logger::ERR )
  143. uiSev &= ( ~Logger::ERR );
  144. (*it)->m_uiErrorSeverity = uiSev;
  145. if ( (*it)->m_uiErrorSeverity == 0 )
  146. {
  147. it = m_StreamArray.erase( it );
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. // ---------------------------------------------------------------------------
  154. // Constructor
  155. DefaultLogger::DefaultLogger( const std::string &name, LogSeverity severity ) :
  156. m_Severity( severity )
  157. {
  158. m_Streams.push_back( new Win32DebugLogStream() );
  159. if (name.empty())
  160. return;
  161. m_Streams.push_back( new FileLogStream( name ) );
  162. }
  163. // ---------------------------------------------------------------------------
  164. // Destructor
  165. DefaultLogger::~DefaultLogger()
  166. {
  167. for ( StreamIt it = m_StreamArray.begin();
  168. it != m_StreamArray.end();
  169. ++it )
  170. {
  171. delete *it;
  172. }
  173. for (std::vector<LogStream*>::iterator it = m_Streams.begin();
  174. it != m_Streams.end();
  175. ++it)
  176. {
  177. delete *it;
  178. }
  179. m_Streams.clear();
  180. }
  181. // ---------------------------------------------------------------------------
  182. // Writes message to stream
  183. void DefaultLogger::writeToStreams(const std::string &message,
  184. ErrorSeverity ErrorSev )
  185. {
  186. if ( message.empty() )
  187. return;
  188. for ( ConstStreamIt it = this->m_StreamArray.begin();
  189. it != m_StreamArray.end();
  190. ++it)
  191. {
  192. if ( ErrorSev & (*it)->m_uiErrorSeverity )
  193. {
  194. (*it)->m_pStream->write( message );
  195. }
  196. }
  197. for (std::vector<LogStream*>::iterator it = m_Streams.begin();
  198. it != m_Streams.end();
  199. ++it)
  200. {
  201. (*it)->write( message + std::string("\n"));
  202. }
  203. }
  204. // ---------------------------------------------------------------------------
  205. } // Namespace Assimp