DefaultLogger.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. /** @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. // ----------------------------------------------------------------------------------
  53. // Represents a logstream + its error severity
  54. struct LogStreamInfo
  55. {
  56. unsigned int m_uiErrorSeverity;
  57. LogStream *m_pStream;
  58. // Constructor
  59. LogStreamInfo( unsigned int uiErrorSev, LogStream *pStream ) :
  60. m_uiErrorSeverity( uiErrorSev ),
  61. m_pStream( pStream )
  62. {
  63. // empty
  64. }
  65. // Destructor
  66. ~LogStreamInfo()
  67. {
  68. // empty
  69. }
  70. };
  71. // ----------------------------------------------------------------------------------
  72. // Construct a default log stream
  73. LogStream* LogStream::createDefaultStream(DefaultLogStreams streams,
  74. const char* name /*= "AssimpLog.txt"*/,
  75. IOSystem* io /*= NULL*/)
  76. {
  77. switch (streams)
  78. {
  79. // This is a platform-specific feature
  80. case DLS_DEBUGGER:
  81. #ifdef WIN32
  82. return new Win32DebugLogStream();
  83. #else
  84. return NULL;
  85. #endif
  86. // Platform-independent default streams
  87. case DLS_CERR:
  88. return new StdOStreamLogStream(std::cerr);
  89. case DLS_COUT:
  90. return new StdOStreamLogStream(std::cout);
  91. case DLS_FILE:
  92. return (name && *name ? new FileLogStream(name,io) : NULL);
  93. default:
  94. // We don't know this default log stream, so raise an assertion
  95. ai_assert(false);
  96. };
  97. // For compilers without dead code path detection
  98. return NULL;
  99. }
  100. // ----------------------------------------------------------------------------------
  101. // Creates the only singleton instance
  102. Logger *DefaultLogger::create(const char* name /*= "AssimpLog.txt"*/,
  103. LogSeverity severity /*= NORMAL*/,
  104. unsigned int defStreams /*= DLS_DEBUGGER | DLS_FILE*/,
  105. IOSystem* io /*= NULL*/)
  106. {
  107. // enter the mutex here to avoid concurrency problems
  108. #ifndef ASSIMP_BUILD_SINGLETHREADED
  109. boost::mutex::scoped_lock lock(loggerMutex);
  110. #endif
  111. if (m_pLogger && !isNullLogger() )
  112. delete m_pLogger;
  113. m_pLogger = new DefaultLogger( severity );
  114. // Attach default log streams
  115. // Stream the log to the MSVC debugger?
  116. if (defStreams & DLS_DEBUGGER)
  117. m_pLogger->attachStream( LogStream::createDefaultStream(DLS_DEBUGGER));
  118. // Stream the log to COUT?
  119. if (defStreams & DLS_COUT)
  120. m_pLogger->attachStream( LogStream::createDefaultStream(DLS_COUT));
  121. // Stream the log to CERR?
  122. if (defStreams & DLS_CERR)
  123. m_pLogger->attachStream( LogStream::createDefaultStream(DLS_CERR));
  124. // Stream the log to a file
  125. if (defStreams & DLS_FILE && name && *name)
  126. m_pLogger->attachStream( LogStream::createDefaultStream(DLS_FILE,name,io));
  127. return m_pLogger;
  128. }
  129. // ----------------------------------------------------------------------------------
  130. void Logger::debug(const std::string &message) {
  131. // SECURITY FIX: otherwise it's easy to produce overruns ...
  132. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  133. ai_assert(false);
  134. return;
  135. }
  136. return OnDebug(message.c_str());
  137. }
  138. // ----------------------------------------------------------------------------------
  139. void Logger::info(const std::string &message) {
  140. // SECURITY FIX: otherwise it's easy to produce overruns ...
  141. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  142. ai_assert(false);
  143. return;
  144. }
  145. return OnInfo(message.c_str());
  146. }
  147. // ----------------------------------------------------------------------------------
  148. void Logger::warn(const std::string &message) {
  149. // SECURITY FIX: otherwise it's easy to produce overruns ...
  150. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  151. ai_assert(false);
  152. return;
  153. }
  154. return OnWarn(message.c_str());
  155. }
  156. // ----------------------------------------------------------------------------------
  157. void Logger::error(const std::string &message) {
  158. // SECURITY FIX: otherwise it's easy to produce overruns ...
  159. if (message.length()>MAX_LOG_MESSAGE_LENGTH) {
  160. ai_assert(false);
  161. return;
  162. }
  163. return OnError(message.c_str());
  164. }
  165. // ----------------------------------------------------------------------------------
  166. void DefaultLogger::set( Logger *logger )
  167. {
  168. // enter the mutex here to avoid concurrency problems
  169. #ifndef ASSIMP_BUILD_SINGLETHREADED
  170. boost::mutex::scoped_lock lock(loggerMutex);
  171. #endif
  172. if (!logger)logger = &s_pNullLogger;
  173. if (m_pLogger && !isNullLogger() )
  174. delete m_pLogger;
  175. DefaultLogger::m_pLogger = logger;
  176. }
  177. // ----------------------------------------------------------------------------------
  178. bool DefaultLogger::isNullLogger()
  179. {
  180. return m_pLogger == &s_pNullLogger;
  181. }
  182. // ----------------------------------------------------------------------------------
  183. // Singleton getter
  184. Logger *DefaultLogger::get()
  185. {
  186. return m_pLogger;
  187. }
  188. // ----------------------------------------------------------------------------------
  189. // Kills the only instance
  190. void DefaultLogger::kill()
  191. {
  192. // enter the mutex here to avoid concurrency problems
  193. #ifndef ASSIMP_BUILD_SINGLETHREADED
  194. boost::mutex::scoped_lock lock(loggerMutex);
  195. #endif
  196. if (m_pLogger != &s_pNullLogger)return;
  197. delete m_pLogger;
  198. m_pLogger = &s_pNullLogger;
  199. }
  200. // ----------------------------------------------------------------------------------
  201. // Debug message
  202. void DefaultLogger::OnDebug( const char* message )
  203. {
  204. if ( m_Severity == Logger::NORMAL )
  205. return;
  206. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  207. ::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message );
  208. WriteToStreams( msg, Logger::DEBUGGING );
  209. }
  210. // ----------------------------------------------------------------------------------
  211. // Logs an info
  212. void DefaultLogger::OnInfo( const char* message )
  213. {
  214. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  215. ::sprintf(msg,"Info, T%i: %s", GetThreadID(), message );
  216. WriteToStreams( msg , Logger::INFO );
  217. }
  218. // ----------------------------------------------------------------------------------
  219. // Logs a warning
  220. void DefaultLogger::OnWarn( const char* message )
  221. {
  222. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  223. ::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message );
  224. WriteToStreams( msg, Logger::WARN );
  225. }
  226. // ----------------------------------------------------------------------------------
  227. // Logs an error
  228. void DefaultLogger::OnError( const char* message )
  229. {
  230. char msg[MAX_LOG_MESSAGE_LENGTH*2];
  231. ::sprintf(msg,"Error, T%i: %s", GetThreadID(), message );
  232. WriteToStreams( msg, Logger::ERR );
  233. }
  234. // ----------------------------------------------------------------------------------
  235. // Attachs a new stream
  236. bool DefaultLogger::attachStream( LogStream *pStream, unsigned int severity )
  237. {
  238. if (!pStream)
  239. return false;
  240. if (0 == severity) {
  241. severity = Logger::INFO | Logger::ERR | Logger::WARN | Logger::DEBUGGING;
  242. }
  243. for ( StreamIt it = m_StreamArray.begin();
  244. it != m_StreamArray.end();
  245. ++it )
  246. {
  247. if ( (*it)->m_pStream == pStream )
  248. {
  249. (*it)->m_uiErrorSeverity |= severity;
  250. return true;
  251. }
  252. }
  253. LogStreamInfo *pInfo = new LogStreamInfo( severity, pStream );
  254. m_StreamArray.push_back( pInfo );
  255. return true;
  256. }
  257. // ----------------------------------------------------------------------------------
  258. // Detatch a stream
  259. bool DefaultLogger::detatchStream( LogStream *pStream, unsigned int severity )
  260. {
  261. if (!pStream)
  262. return false;
  263. if (0 == severity) {
  264. severity = Logger::INFO | Logger::ERR | Logger::WARN | Logger::DEBUGGING;
  265. }
  266. for ( StreamIt it = m_StreamArray.begin();
  267. it != m_StreamArray.end();
  268. ++it )
  269. {
  270. if ( (*it)->m_pStream == pStream )
  271. {
  272. (*it)->m_uiErrorSeverity &= ~severity;
  273. if ( (*it)->m_uiErrorSeverity == 0 )
  274. {
  275. m_StreamArray.erase( it );
  276. break;
  277. }
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. // ----------------------------------------------------------------------------------
  284. // Constructor
  285. DefaultLogger::DefaultLogger(LogSeverity severity)
  286. : Logger ( severity )
  287. , noRepeatMsg (false)
  288. , lastLen( 0 )
  289. {
  290. lastMsg[0] = '\0';
  291. }
  292. // ----------------------------------------------------------------------------------
  293. // Destructor
  294. DefaultLogger::~DefaultLogger()
  295. {
  296. for ( StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it )
  297. delete *it;
  298. }
  299. // ----------------------------------------------------------------------------------
  300. // Writes message to stream
  301. void DefaultLogger::WriteToStreams(const char *message,
  302. ErrorSeverity ErrorSev )
  303. {
  304. ai_assert(NULL != message);
  305. // Check whether this is a repeated message
  306. if (! ::strncmp( message,lastMsg, lastLen-1))
  307. {
  308. if (!noRepeatMsg)
  309. {
  310. noRepeatMsg = true;
  311. message = "Skipping one or more lines with the same contents\n";
  312. }
  313. else return;
  314. }
  315. else
  316. {
  317. // append a new-line character to the message to be printed
  318. lastLen = ::strlen(message);
  319. ::memcpy(lastMsg,message,lastLen+1);
  320. ::strcat(lastMsg+lastLen,"\n");
  321. message = lastMsg;
  322. noRepeatMsg = false;
  323. ++lastLen;
  324. }
  325. for ( ConstStreamIt it = m_StreamArray.begin();
  326. it != m_StreamArray.end();
  327. ++it)
  328. {
  329. if ( ErrorSev & (*it)->m_uiErrorSeverity )
  330. (*it)->m_pStream->write( message);
  331. }
  332. }
  333. // ----------------------------------------------------------------------------------
  334. // Returns thread id, if not supported only a zero will be returned.
  335. unsigned int DefaultLogger::GetThreadID()
  336. {
  337. // fixme: we can get this value via boost::threads
  338. #ifdef WIN32
  339. return (unsigned int)::GetCurrentThreadId();
  340. #else
  341. return 0; // not supported
  342. #endif
  343. }
  344. // ----------------------------------------------------------------------------------
  345. } // !namespace Assimp