Browse Source

Synchronize `DefaultLogger` (#5898)

Co-authored-by: Kim Kulling <[email protected]>
Stefan 9 months ago
parent
commit
739ebfd207
2 changed files with 22 additions and 1 deletions
  1. 13 1
      code/Common/DefaultLogger.cpp
  2. 9 0
      include/assimp/DefaultLogger.hpp

+ 13 - 1
code/Common/DefaultLogger.cpp

@@ -318,9 +318,13 @@ bool DefaultLogger::attachStream(LogStream *pStream, unsigned int severity) {
     }
 
     if (0 == severity) {
-        severity = Logger::Info | Logger::Err | Logger::Warn | Logger::Debugging;
+        severity = SeverityAll;
     }
 
+#ifndef ASSIMP_BUILD_SINGLETHREADED
+    std::lock_guard<std::mutex> lock(m_arrayMutex);
+#endif
+
     for (StreamIt it = m_StreamArray.begin();
             it != m_StreamArray.end();
             ++it) {
@@ -346,6 +350,10 @@ bool DefaultLogger::detachStream(LogStream *pStream, unsigned int severity) {
         severity = SeverityAll;
     }
 
+#ifndef ASSIMP_BUILD_SINGLETHREADED
+    std::lock_guard<std::mutex> lock(m_arrayMutex);
+#endif
+
     bool res(false);
     for (StreamIt it = m_StreamArray.begin(); it != m_StreamArray.end(); ++it) {
         if ((*it)->m_pStream == pStream) {
@@ -385,6 +393,10 @@ DefaultLogger::~DefaultLogger() {
 void DefaultLogger::WriteToStreams(const char *message, ErrorSeverity ErrorSev) {
     ai_assert(nullptr != message);
 
+#ifndef ASSIMP_BUILD_SINGLETHREADED
+    std::lock_guard<std::mutex> lock(m_arrayMutex);
+#endif
+
     // Check whether this is a repeated message
     auto thisLen = ::strlen(message);
     if (thisLen == lastLen - 1 && !::strncmp(message, lastMsg, lastLen - 1)) {

+ 9 - 0
include/assimp/DefaultLogger.hpp

@@ -56,6 +56,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "NullLogger.hpp"
 #include <vector>
 
+#ifndef ASSIMP_BUILD_SINGLETHREADED
+#include <mutex>
+#include <thread>
+#endif
+
 namespace Assimp {
 // ------------------------------------------------------------------------------------
 class IOStream;
@@ -184,6 +189,10 @@ private:
     //! Attached streams
     StreamArray m_StreamArray;
 
+#ifndef ASSIMP_BUILD_SINGLETHREADED
+    std::mutex m_arrayMutex;
+#endif
+
     bool noRepeatMsg;
     char lastMsg[MAX_LOG_MESSAGE_LENGTH * 2];
     size_t lastLen;