changeset 6969:1f22c8f22ede streaming

recycling thread information
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2026 17:52:24 +0200
parents 2c0edc2660f0
children 89d31235fff6
files OrthancFramework/Sources/Logging.cpp
diffstat 1 files changed, 50 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Logging.cpp	Mon Jun 29 16:13:42 2026 +0200
+++ b/OrthancFramework/Sources/Logging.cpp	Mon Jun 29 17:52:24 2026 +0200
@@ -716,11 +716,6 @@
     {
       return context_;
     }
-
-    void CopyContext(std::list<std::string>& target) const
-    {
-      target = context_;
-    }
   };
 
 
@@ -815,7 +810,7 @@
         if (exists_)
         {
           assert(information_ != NULL);
-          information_->CopyContext(target);
+          target = information_->GetContext();
         }
         else
         {
@@ -832,12 +827,30 @@
       boost::unique_lock<boost::shared_mutex>  lock_;
       boost::thread::id                        threadId_;
       ThreadInformation*                       information_;
+      bool                                     valid_;
+
+      void Recycle()
+      {
+        if (!information_->HasThreadName() &&
+            information_->GetContext().empty())
+        {
+          Content::iterator found = informations_.content_.find(threadId_);
+          assert(found != informations_.content_.end());
+          assert(found->second != NULL);
+
+          delete found->second;
+          informations_.content_.erase(found);
+
+          valid_ = false;
+        }
+      }
 
     public:
       explicit CurrentThreadWriter(ThreadsInformations& informations) :
         informations_(informations),
         lock_(informations.mutex_),
-        threadId_(boost::this_thread::get_id())
+        threadId_(boost::this_thread::get_id()),
+        valid_(true)
       {
         Content::iterator found = informations.content_.find(threadId_);
 
@@ -855,6 +868,11 @@
 
       void SetThreadName(const std::string& name)
       {
+        if (!valid_)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+        }
+
         assert(information_ != NULL);
         information_->SetThreadName(name);
 
@@ -876,22 +894,41 @@
 #endif
       }
 
-      void ClearThreadName()
-      {
-        assert(information_ != NULL);
-        information_->ClearThreadName();
-      }
-
       void PushContext(const std::string& context)
       {
+        if (!valid_)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+        }
+
         assert(information_ != NULL);
         information_->PushContext(context);
       }
 
+      void ClearThreadName()
+      {
+        if (!valid_)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+        }
+
+        assert(information_ != NULL);
+        information_->ClearThreadName();
+
+        Recycle();
+      }
+
       void PopContext()
       {
+        if (!valid_)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+        }
+
         assert(information_ != NULL);
         information_->PopContext();
+
+        Recycle();
       }
     };
   };