DefaultLogger.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, 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 "FileLogStream.h"
  39. #include "StdOStreamLogStream.h"
  40. #include "Win32DebugLogStream.h"
  41. #include <assimp/StringUtils.h>
  42. #include <assimp/DefaultIOSystem.h>
  43. #include <assimp/ai_assert.h>
  44. #include <stdio.h>
  45. #include <assimp/DefaultLogger.hpp>
  46. #include <assimp/NullLogger.hpp>
  47. #include <iostream>
  48. #ifndef ASSIMP_BUILD_SINGLETHREADED
  49. #include <mutex>
  50. #include <thread>
  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 /*= nullptr*/) {
  79. switch (streams) {
  80. // This is a platform-specific feature
  81. case aiDefaultLogStream_DEBUGGER:
  82. #ifdef WIN32
  83. return new Win32DebugLogStream();
  84. #else
  85. return nullptr;
  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) : nullptr);
  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 nullptr;
  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 /*= nullptr*/) {
  107. // enter the mutex here to avoid concurrency problems
  108. #ifndef ASSIMP_BUILD_SINGLETHREADED
  109. std::lock_guard<std::mutex> lock(loggerMutex);
  110. #endif
  111. if (m_pLogger && !isNullLogger()) {
  112. delete m_pLogger;
  113. }
  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. }
  120. // Stream the log to COUT?
  121. if (defStreams & aiDefaultLogStream_STDOUT) {
  122. m_pLogger->attachStream(LogStream::createDefaultStream(aiDefaultLogStream_STDOUT));
  123. }
  124. // Stream the log to CERR?
  125. if (defStreams & aiDefaultLogStream_STDERR) {
  126. m_pLogger->attachStream(LogStream::createDefaultStream(aiDefaultLogStream_STDERR));
  127. }
  128. // Stream the log to a file
  129. if (defStreams & aiDefaultLogStream_FILE && name && *name) {
  130. m_pLogger->attachStream(LogStream::createDefaultStream(aiDefaultLogStream_FILE, name, io));
  131. }
  132. return m_pLogger;
  133. }
  134. // ----------------------------------------------------------------------------------
  135. void Logger::debug(const char *message) {
  136. // SECURITY FIX: otherwise it's easy to produce overruns since
  137. // sometimes importers will include data from the input file
  138. // (i.e. node names) in their messages.
  139. if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) {
  140. return OnDebug("<fixme: long message discarded>");
  141. }
  142. return OnDebug(message);
  143. }
  144. // ----------------------------------------------------------------------------------
  145. void Logger::verboseDebug(const char *message) {
  146. // SECURITY FIX: see above
  147. if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) {
  148. return OnVerboseDebug("<fixme: long message discarded>");
  149. }
  150. return OnVerboseDebug(message);
  151. }
  152. // ----------------------------------------------------------------------------------
  153. void Logger::info(const char *message) {
  154. // SECURITY FIX: see above
  155. if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) {
  156. return OnInfo("<fixme: long message discarded>");
  157. }
  158. return OnInfo(message);
  159. }
  160. // ----------------------------------------------------------------------------------
  161. void Logger::warn(const char *message) {
  162. // SECURITY FIX: see above
  163. if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) {
  164. return OnWarn("<fixme: long message discarded>");
  165. }
  166. return OnWarn(message);
  167. }
  168. // ----------------------------------------------------------------------------------
  169. void Logger::error(const char *message) {
  170. // SECURITY FIX: see above
  171. if (strlen(message) > MAX_LOG_MESSAGE_LENGTH) {
  172. return OnError("<fixme: long message discarded>");
  173. }
  174. return OnError(message);
  175. }
  176. // ----------------------------------------------------------------------------------
  177. void DefaultLogger::set(Logger *logger) {
  178. // enter the mutex here to avoid concurrency problems
  179. #ifndef ASSIMP_BUILD_SINGLETHREADED
  180. std::lock_guard<std::mutex> lock(loggerMutex);
  181. #endif
  182. if (nullptr == logger) {
  183. m_pLogger = &s_pNullLogger;
  184. }
  185. else {
  186. m_pLogger = logger;
  187. }
  188. }
  189. // ----------------------------------------------------------------------------------
  190. bool DefaultLogger::isNullLogger() {
  191. return m_pLogger == &s_pNullLogger;
  192. }
  193. // ----------------------------------------------------------------------------------
  194. Logger *DefaultLogger::get() {
  195. return m_pLogger;
  196. }
  197. // ----------------------------------------------------------------------------------
  198. // Kills the only instance
  199. void DefaultLogger::kill() {
  200. // enter the mutex here to avoid concurrency problems
  201. #ifndef ASSIMP_BUILD_SINGLETHREADED
  202. std::lock_guard<std::mutex> lock(loggerMutex);
  203. #endif
  204. if (m_pLogger == &s_pNullLogger) {
  205. return;
  206. }
  207. delete m_pLogger;
  208. m_pLogger = &s_pNullLogger;
  209. }
  210. // ----------------------------------------------------------------------------------
  211. // Debug message
  212. void DefaultLogger::OnDebug(const char *message) {
  213. if (m_Severity < Logger::DEBUGGING) {
  214. return;
  215. }
  216. static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16;
  217. char msg[Size];
  218. ai_snprintf(msg, Size, "Debug, T%u: %s", GetThreadID(), message);
  219. WriteToStreams(msg, Logger::Debugging);
  220. }
  221. // Verbose debug message
  222. void DefaultLogger::OnVerboseDebug(const char *message) {
  223. if (m_Severity < Logger::VERBOSE) {
  224. return;
  225. }
  226. static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16;
  227. char msg[Size];
  228. ai_snprintf(msg, Size, "Debug, T%u: %s", GetThreadID(), message);
  229. WriteToStreams(msg, Logger::Debugging);
  230. }
  231. // ----------------------------------------------------------------------------------
  232. // Logs an info
  233. void DefaultLogger::OnInfo(const char *message) {
  234. static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16;
  235. char msg[Size];
  236. ai_snprintf(msg, Size, "Info, T%u: %s", GetThreadID(), message);
  237. WriteToStreams(msg, Logger::Info);
  238. }
  239. // ----------------------------------------------------------------------------------
  240. // Logs a warning
  241. void DefaultLogger::OnWarn(const char *message) {
  242. static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16;
  243. char msg[Size];
  244. ai_snprintf(msg, Size, "Warn, T%u: %s", GetThreadID(), message);
  245. WriteToStreams(msg, Logger::Warn);
  246. }
  247. // ----------------------------------------------------------------------------------
  248. // Logs an error
  249. void DefaultLogger::OnError(const char *message) {
  250. static const size_t Size = MAX_LOG_MESSAGE_LENGTH + 16;
  251. char msg[Size];
  252. ai_snprintf(msg, Size, "Error, T%u: %s", GetThreadID(), message);
  253. WriteToStreams(msg, Logger::Err);
  254. }
  255. // ----------------------------------------------------------------------------------
  256. // Will attach a new stream
  257. bool DefaultLogger::attachStream(LogStream *pStream, unsigned int severity) {
  258. if (nullptr == pStream) {
  259. return false;
  260. }
  261. if (0 == severity) {
  262. severity = SeverityAll;
  263. }
  264. #ifndef ASSIMP_BUILD_SINGLETHREADED
  265. std::lock_guard<std::mutex> lock(m_arrayMutex);
  266. #endif
  267. for (StreamIt it = m_StreamArray.begin();
  268. it != m_StreamArray.end();
  269. ++it) {
  270. if ((*it)->m_pStream == pStream) {
  271. (*it)->m_uiErrorSeverity |= severity;
  272. return true;
  273. }
  274. }
  275. LogStreamInfo *pInfo = new LogStreamInfo(severity, pStream);
  276. m_StreamArray.push_back(pInfo);
  277. return true;
  278. }
  279. // ----------------------------------------------------------------------------------
  280. // Detach a stream
  281. bool DefaultLogger::detachStream(LogStream *pStream, unsigned int severity) {
  282. if (nullptr == pStream) {
  283. return false;
  284. }
  285. if (0 == severity) {
  286. severity = SeverityAll;
  287. }
  288. #ifndef ASSIMP_BUILD_SINGLETHREADED
  289. std::lock_guard<std::mutex> lock(m_arrayMutex);
  290. #endif
  291. bool res(false);
  292. for (StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it) {
  293. if ((*it)->m_pStream == pStream) {
  294. (*it)->m_uiErrorSeverity &= ~severity;
  295. if ((*it)->m_uiErrorSeverity == 0) {
  296. // don't delete the underlying stream 'cause the caller gains ownership again
  297. (**it).m_pStream = nullptr;
  298. delete *it;
  299. m_StreamArray.erase(it);
  300. res = true;
  301. break;
  302. }
  303. return true;
  304. }
  305. }
  306. return res;
  307. }
  308. // ----------------------------------------------------------------------------------
  309. // Constructor
  310. DefaultLogger::DefaultLogger(LogSeverity severity) :
  311. Logger(severity), noRepeatMsg(false), lastLen(0) {
  312. lastMsg[0] = '\0';
  313. }
  314. // ----------------------------------------------------------------------------------
  315. // Destructor
  316. DefaultLogger::~DefaultLogger() {
  317. for (StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it) {
  318. // also frees the underlying stream, we are its owner.
  319. delete *it;
  320. }
  321. }
  322. // ----------------------------------------------------------------------------------
  323. // Writes message to stream
  324. void DefaultLogger::WriteToStreams(const char *message, ErrorSeverity ErrorSev) {
  325. ai_assert(nullptr != message);
  326. #ifndef ASSIMP_BUILD_SINGLETHREADED
  327. std::lock_guard<std::mutex> lock(m_arrayMutex);
  328. #endif
  329. // Check whether this is a repeated message
  330. auto thisLen = ::strlen(message);
  331. if (thisLen == lastLen - 1 && !::strncmp(message, lastMsg, lastLen - 1)) {
  332. if (!noRepeatMsg) {
  333. noRepeatMsg = true;
  334. message = "Skipping one or more lines with the same contents\n";
  335. }
  336. return;
  337. } else {
  338. // append a new-line character to the message to be printed
  339. lastLen = thisLen;
  340. ::memcpy(lastMsg, message, lastLen + 1);
  341. ::strcat(lastMsg + lastLen, "\n");
  342. message = lastMsg;
  343. noRepeatMsg = false;
  344. ++lastLen;
  345. }
  346. for (ConstStreamIt it = m_StreamArray.begin();
  347. it != m_StreamArray.end();
  348. ++it) {
  349. if (ErrorSev & (*it)->m_uiErrorSeverity)
  350. (*it)->m_pStream->write(message);
  351. }
  352. }
  353. // ----------------------------------------------------------------------------------
  354. // Returns thread id, if not supported only a zero will be returned.
  355. unsigned int DefaultLogger::GetThreadID() {
  356. // fixme: we can get this value via std::threads
  357. // std::this_thread::get_id().hash() returns a (big) size_t, not sure if this is useful in this case.
  358. #ifdef WIN32
  359. return (unsigned int)::GetCurrentThreadId();
  360. #else
  361. return 0; // not supported
  362. #endif
  363. }
  364. // ----------------------------------------------------------------------------------
  365. } // namespace Assimp