changeset 6965:d28c3fb90bd6 streaming

refactored thread contexts
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2026 15:47:19 +0200
parents 5eb525758b7f
children edd68dfbdd98
files OrthancFramework/Sources/DicomNetworking/DicomServer.cpp OrthancFramework/Sources/JobsEngine/JobsEngine.cpp OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp OrthancFramework/Sources/Logging.cpp OrthancFramework/Sources/Logging.h OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancFramework/UnitTestsSources/MemoryCacheTests.cpp OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp OrthancServer/Sources/LuaScripting.cpp OrthancServer/Sources/OrthancWebDav.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp OrthancServer/Sources/main.cpp
diffstat 15 files changed, 237 insertions(+), 194 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -63,7 +63,7 @@
                                  unsigned int maximumPduLength,
                                  bool useDicomTls)
   {
-    Logging::ScopedThreadNameSetter setter("DICOM-SERVER");
+    Logging::ScopedCurrentThreadNameSetter setter("DICOM-SERVER");
     CLOG(INFO, DICOM) << "DICOM server started";
 
     while (server->continue_)
--- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -109,7 +109,7 @@
     
   void JobsEngine::RetryHandler(JobsEngine* engine)
   {
-    Logging::ScopedThreadNameSetter setter("JOBS-RETRY");
+    Logging::ScopedCurrentThreadNameSetter setter("JOBS-RETRY");
 
     assert(engine != NULL);
 
@@ -125,7 +125,7 @@
                           size_t workerIndex)
   {
     assert(engine != NULL);
-    Logging::ScopedThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast<std::string>(workerIndex));
+    Logging::ScopedCurrentThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast<std::string>(workerIndex));
     CLOG(INFO, JOBS) << "Worker thread " << workerIndex << " has started";
 
     while (engine->IsRunning())
@@ -140,7 +140,7 @@
         CLOG(INFO, JOBS) << "Executing " << jobType << " job with priority " << running.GetPriority()
                          << " in worker thread " << workerIndex << ": " << running.GetId();
 
-        Logging::ScopedContextSetter logContext(std::string("job ") + running.GetId());
+        Logging::ScopedCurrentThreadContextSetter logContext(std::string("job ") + running.GetId());
 
         while (engine->IsRunning())
         {
--- a/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -114,7 +114,7 @@
       pauseScheduled_(false),
       cancelScheduled_(false)
     {
-      Logging::ScopedContextSetter logContext(std::string("job ") + id_);
+      Logging::ScopedCurrentThreadContextSetter logContext(std::string("job ") + id_);
       
       if (job == NULL)
       {
--- a/OrthancFramework/Sources/Logging.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/Logging.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -631,6 +631,7 @@
   private:
     bool                    hasName_;
     std::string             name_;
+    std::list<std::string>  context_;
 
   public:
     ThreadInformation() :
@@ -645,7 +646,8 @@
       {
         throw Orthanc::OrthancException(
           Orthanc::ErrorCode_InternalError, "This thread already has a name or another thread is re-using "
-          "the same threadId and you have not called 'ClearCurrentThreadName()': " + name);
+          "the same threadId and you have not called 'ClearCurrentThreadName()': " + name,
+          false /* don't log to avoid deadlock */);
       }
 #endif
 
@@ -689,6 +691,36 @@
         throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
       }
     }
+
+    void PopContext()
+    {
+      if (context_.empty())
+      {
+#if !defined(NDEBUG)
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Cannot pop from an empty context",
+                                        false /* don't log to avoid deadlock */);
+#endif
+      }
+      else
+      {
+        context_.pop_back();
+      }
+    }
+
+    void PushContext(const std::string& message)
+    {
+      context_.push_back(message);
+    }
+
+    const std::list<std::string>& GetContext() const
+    {
+      return context_;
+    }
+
+    void CopyContext(std::list<std::string>& target) const
+    {
+      target = context_;
+    }
   };
 
 
@@ -767,6 +799,29 @@
         return (exists_ &&
                 information_->HasThreadName());
       }
+
+      void FormatContext(std::string& context) const
+      {
+        assert(!exists_ || information_ != NULL);
+        if (exists_ &&
+            !information_->GetContext().empty())
+        {
+          context = "| " + boost::algorithm::join(information_->GetContext(), std::string(" | ")) + " | ";
+        }
+      }
+
+      void CopyContext(std::list<std::string>& target) const
+      {
+        if (exists_)
+        {
+          assert(information_ != NULL);
+          information_->CopyContext(target);
+        }
+        else
+        {
+          target.clear();
+        }
+      }
     };
 
 
@@ -814,7 +869,8 @@
               it->second->GetThreadName() == formattedName)
           {
             throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
-                                            "Another thread already uses this thread name: " + name);
+                                            "Another thread already uses this thread name: " + name,
+                                            false /* don't log to avoid deadlock */);
           }
         }
 #endif
@@ -825,14 +881,23 @@
         assert(information_ != NULL);
         information_->ClearThreadName();
       }
+
+      void PushContext(const std::string& context)
+      {
+        assert(information_ != NULL);
+        information_->PushContext(context);
+      }
+
+      void PopContext()
+      {
+        assert(information_ != NULL);
+        information_->PopContext();
+      }
     };
   };
 }
 
 
-typedef std::map<boost::thread::id, std::string> ThreadNamesMap;
-typedef std::map<boost::thread::id, std::vector<std::string> > ThreadContextsMap;
-
 static std::unique_ptr<LoggingStreamsContext>   loggingStreamsContext_;
 static boost::mutex                             loggingStreamsMutex_;
 static Orthanc::Logging::NullStream             nullStream_;
@@ -842,9 +907,7 @@
 static bool                                     hasClearThreadName_ = false;  // Whether the Orthanc runtime is >= 1.12.12
 static ThreadsInformations                      threadsInformations_;
 static bool                                     enableThreadNames_ = true;
-static boost::recursive_mutex                   threadContextsMutex_;
-static ThreadContextsMap                        threadContexts_;
-static bool                                     enableContexts_ = true;
+static bool                                     enableThreadContexts_ = true;
 static std::list<Orthanc::Logging::ILoggingListener*> loggingListeners_;
 static boost::shared_mutex                            loggingListenersMutex_;
 
@@ -853,24 +916,14 @@
 {
   namespace Logging
   {
-    void EnableThreadNames(bool enabled)
+    void SetThreadNamesEnabled(bool enabled)
     {
       enableThreadNames_ = enabled;
     }
 
-    bool IsThreadNamesEnabled()
-    {
-      return enableThreadNames_;
-    }
-
-    void EnableContexts(bool enabled)
+    void SetThreadContextsEnabled(bool enabled)
     {
-      enableContexts_ = enabled;
-    }
-
-    bool IsContextsEnabled()
-    {
-      return enableContexts_;
+      enableThreadContexts_ = enabled;
     }
 
     static void GetLogPath(boost::filesystem::path& log,
@@ -1000,89 +1053,81 @@
     }    
 
 
-    bool LookupCurrentContext(std::string& result)
+    void PushCurrentThreadContext(const std::string& context)
     {
-      boost::thread::id threadId = boost::this_thread::get_id();
-
-      boost::recursive_mutex::scoped_lock lock(threadContextsMutex_);
-
-      if (threadContexts_.find(threadId) != threadContexts_.end() && threadContexts_[threadId].size() > 0)
+      if (context.size() == 0)
       {
-        Toolbox::JoinStrings(result, threadContexts_[threadId], " | ");
-        return true;
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
       }
-
-      return false;
-    }    
-
-
-    ScopedThreadNameSetter::ScopedThreadNameSetter(const std::string& threadName)
-    {
-      SetCurrentThreadName(threadName);
-    }
-
-
-    ScopedThreadNameSetter::~ScopedThreadNameSetter()
-    {
-      ClearCurrentThreadName();
-    }
-
-    ScopedContextSetter::ScopedContextSetter(const std::string& context) :
-      hasPushedContext_(false)
-    {
-      if (!context.empty())
+      else
       {
-        PushContext(context);
-        hasPushedContext_ = true;
+        ThreadsInformations::Writer writer(threadsInformations_);
+        writer.PushContext(context);
       }
     }
 
-    ScopedContextSetter::~ScopedContextSetter()
+
+    void PopCurrentThreadContext()
+    {
+      ThreadsInformations::Writer writer(threadsInformations_);
+      writer.PopContext();
+    }
+
+
+    struct ThreadContextMemento::PImpl : public boost::noncopyable
     {
-      if (hasPushedContext_)
+      std::list<std::string>  context_;
+    };
+
+
+    ThreadContextMemento::ScopedSetter::ScopedSetter(const ThreadContextMemento& memento) :
+      count_(memento.pimpl_->context_.size())
+    {
+      if (false) // additional debug information: add the caller thread name
       {
-        PopContext();
+        if (enableThreadNames_)
+        {
+          PushCurrentThreadContext("from " + GetCurrentThreadName());
+        }
+      }
+
+      for (std::list<std::string>::const_iterator it = memento.pimpl_->context_.begin(); it != memento.pimpl_->context_.end(); ++it)
+      {
+        PushCurrentThreadContext(*it);
       }
     }
 
-    void PushContext(const std::string& context)
+
+    ThreadContextMemento::ScopedSetter::~ScopedSetter()
     {
-      if (context.size() == 0)
+      for (size_t i = 0; i < count_; i++)
       {
-        return;
+        PopCurrentThreadContext();
       }
-      boost::thread::id threadId = boost::this_thread::get_id();
-
-      // std::cout << "++ PUSH " << threadId << " - " << context << "\n";
-
-      boost::recursive_mutex::scoped_lock lock(threadContextsMutex_);
-
-      if (threadContexts_.find(threadId) == threadContexts_.end())
-      {
-        // create a new context
-        threadContexts_[threadId] = std::vector<std::string>();
-      }
-      threadContexts_[threadId].push_back(context);
     }
 
-    void PopContext()
+
+    ThreadContextMemento::ThreadContextMemento() :
+      pimpl_(new PImpl)
     {
-      boost::thread::id threadId = boost::this_thread::get_id();
+      ThreadsInformations::Reader reader(threadsInformations_);
+      reader.CopyContext(pimpl_->context_);
+    }
 
-      boost::recursive_mutex::scoped_lock lock(threadContextsMutex_);
-      // std::cout << "++ POP " << threadId << "\n";
 
-      ThreadContextsMap::iterator it = threadContexts_.find(threadId);
-      if (it != threadContexts_.end())
-      {
-        it->second.pop_back();
-        if (it->second.size() == 0) // erase as soon as it is empty so there is no need to clear it when the thread exits
-        {
-          threadContexts_.erase(threadId);
-        }
-      }
+    ThreadContextMemento::~ThreadContextMemento()
+    {
+      assert(pimpl_ != NULL);
+      delete pimpl_;
     }
 
+
+    ThreadContextMemento* CreateCurrentThreadContextMemento()
+    {
+      return new ThreadContextMemento;
+    }
+
+
     void AddLoggingListener(ILoggingListener* listener)
     {
       boost::unique_lock<boost::shared_mutex> lock(loggingListenersMutex_);
@@ -1164,7 +1209,10 @@
               static_cast<int>(duration.fractional_seconds()));
 
       std::string threadName;
+      std::string context;
 
+      if (enableThreadNames_ ||
+          enableThreadContexts_)
       {
         ThreadsInformations::Reader reader(threadsInformations_);
 
@@ -1172,12 +1220,11 @@
         {
           threadName = reader.GetThreadName() + " ";
         }
-      }
 
-      std::string context;
-      if (enableContexts_ && LookupCurrentContext(context))
-      {
-        context = std::string("| ") + context + " | ";
+        if (enableThreadContexts_)
+        {
+          reader.FormatContext(context);
+        }
       }
 
       std::string internalPluginName = "";
--- a/OrthancFramework/Sources/Logging.h	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/Logging.h	Mon Jun 29 15:47:19 2026 +0200
@@ -28,6 +28,7 @@
 #include "OrthancFramework.h"
 #include "Compatibility.h"
 
+#include <boost/noncopyable.hpp>
 #include <iostream>
 #include <stdint.h>
 
@@ -75,27 +76,37 @@
       LogCategory_LUA     = (1 << 6)
     };
 
-    class ScopedThreadNameSetter
+
+    class ThreadContextMemento : public boost::noncopyable
     {
+    private:
+      struct PImpl;
+      PImpl* pimpl_;
+
     public:
-      explicit ScopedThreadNameSetter(const std::string& threadName);
+      ThreadContextMemento();
+
+      ~ThreadContextMemento();
 
-      ~ScopedThreadNameSetter();
+      class ScopedSetter : public boost::noncopyable
+      {
+      private:
+        size_t  count_;
+
+      public:
+        ScopedSetter(const ThreadContextMemento& memento);
+
+        ~ScopedSetter();
+      };
     };
 
-    class ScopedContextSetter
-    {
-      bool hasPushedContext_;
-    public:
-      explicit ScopedContextSetter(const std::string& context);
 
-      ~ScopedContextSetter();
-    };
-
-    class ILoggingListener
+    class ILoggingListener : public boost::noncopyable
     {
     public:
-      virtual ~ILoggingListener() {}
+      virtual ~ILoggingListener()
+      {
+      }
 
       virtual void HandleLog(LogLevel level,
                              LogCategory category,
@@ -105,6 +116,7 @@
                              const std::string& message) = 0;
     };
 
+
     ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level);
 
     ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level);
@@ -123,6 +135,10 @@
 
     ORTHANC_PUBLIC void Flush();
 
+    ORTHANC_PUBLIC void SetThreadNamesEnabled(bool enabled);
+
+    ORTHANC_PUBLIC void SetThreadContextsEnabled(bool enabled);
+
     ORTHANC_PUBLIC void SetCurrentThreadName(const std::string& name);
 
     ORTHANC_PUBLIC bool HasCurrentThreadName();
@@ -131,24 +147,16 @@
 
     ORTHANC_PUBLIC std::string GetCurrentThreadName();
 
-    ORTHANC_PUBLIC void PushContext(const std::string& context);
+    ORTHANC_PUBLIC void PushCurrentThreadContext(const std::string& context);
 
-    ORTHANC_PUBLIC void PopContext();
+    ORTHANC_PUBLIC void PopCurrentThreadContext();
 
-    ORTHANC_PUBLIC bool LookupCurrentContext(std::string& result);
+    ORTHANC_PUBLIC ThreadContextMemento* CreateCurrentThreadContextMemento();
 
     ORTHANC_PUBLIC void AddLoggingListener(ILoggingListener* listener);
 
     ORTHANC_PUBLIC void ClearLoggingListeners();
 
-    ORTHANC_PUBLIC void EnableThreadNames(bool enabled);
-
-    ORTHANC_PUBLIC bool IsThreadNamesEnabled();
-
-    ORTHANC_PUBLIC void EnableContexts(bool enabled);
-
-    ORTHANC_PUBLIC bool IsContextsEnabled();
-
     ORTHANC_PUBLIC void EnableInfoLevel(bool enabled);
 
     ORTHANC_PUBLIC void EnableTraceLevel(bool enabled);
@@ -177,7 +185,37 @@
 
     ORTHANC_PUBLIC void SetTargetFolder(const std::string& path);
 
-    struct ORTHANC_LOCAL NullStream : public std::ostream 
+
+    class ScopedCurrentThreadNameSetter : public boost::noncopyable
+    {
+    public:
+      explicit ScopedCurrentThreadNameSetter(const std::string& threadName)
+      {
+        SetCurrentThreadName(threadName);
+      }
+
+      ~ScopedCurrentThreadNameSetter()
+      {
+        ClearCurrentThreadName();
+      }
+    };
+
+
+    class ScopedCurrentThreadContextSetter : public boost::noncopyable
+    {
+    public:
+      explicit ScopedCurrentThreadContextSetter(const std::string& context)
+      {
+        PushCurrentThreadContext(context);
+      }
+
+      ~ScopedCurrentThreadContextSetter()
+      {
+        PopCurrentThreadContext();
+      }
+    };
+
+    struct ORTHANC_LOCAL NullStream : public std::ostream
     {
       NullStream() : 
         std::ios(0), 
@@ -257,7 +295,6 @@
      ORTHANC_ENABLE_LOGGING_STDIO == 1)
 // This is notably for WebAssembly
 
-#include <boost/noncopyable.hpp>
 #include <sstream>
 
 namespace Orthanc
@@ -299,7 +336,6 @@
 #if (ORTHANC_ENABLE_LOGGING == 1 &&             \
      ORTHANC_ENABLE_LOGGING_STDIO == 0)
 
-#include <boost/noncopyable.hpp>
 #include <sstream>
 
 namespace Orthanc
--- a/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -48,7 +48,7 @@
  
       static void WorkerThread(Worker* that)
       {
-        Logging::ScopedThreadNameSetter setter(that->threadName_);
+        Logging::ScopedCurrentThreadNameSetter setter(that->threadName_);
 
         while (that->continue_)
         {
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -58,40 +58,34 @@
     virtual void Execute() = 0;
 
     virtual void Cancel() = 0;
-
-    virtual std::string GetTransmitContext() = 0;
   };
 
 
   class ThreadPool::CallableTask : public ITask
   {
   private:
-    std::unique_ptr<ICallable>               callable_;
-    boost::weak_ptr<Internals::FutureState>  state_;
-    std::string                              transmitContext_;
+    std::unique_ptr<ICallable>                      callable_;
+    boost::weak_ptr<Internals::FutureState>         state_;
+    std::unique_ptr<Logging::ThreadContextMemento>  callerMemento_;
 
   public:
     CallableTask(ICallable* callable,
-                 boost::shared_ptr<Internals::FutureState>& state,
-                 const std::string& transmitContext) :
+                 boost::shared_ptr<Internals::FutureState>& state) :
       callable_(callable),
       state_(state),
-      transmitContext_(transmitContext)
+      callerMemento_(Logging::CreateCurrentThreadContextMemento())
     {
       assert(callable != NULL);
     }
 
-    virtual std::string GetTransmitContext() ORTHANC_OVERRIDE
-    {
-      return transmitContext_;
-    }
-
     virtual void Execute() ORTHANC_OVERRIDE
     {
       boost::shared_ptr<Internals::FutureState> locked = state_.lock();
 
       if (locked)
       {
+        Logging::ThreadContextMemento::ScopedSetter setter(*callerMemento_);
+
         try
         {
           locked->AcquireResult(callable_->Call());
@@ -130,25 +124,20 @@
   class ThreadPool::RunnableTask : public ITask
   {
   private:
-    std::unique_ptr<IRunnable>  runnable_;
-    std::string                 transmitContext_;
+    std::unique_ptr<IRunnable>                      runnable_;
+    std::unique_ptr<Logging::ThreadContextMemento>  callerMemento_;
 
   public:
-    explicit RunnableTask(IRunnable* runnable,
-                          const std::string& transmitContext) :
+    explicit RunnableTask(IRunnable* runnable) :
       runnable_(runnable),
-      transmitContext_(transmitContext)
+      callerMemento_(Logging::CreateCurrentThreadContextMemento())
     {
       assert(runnable != NULL);
     }
 
-    virtual std::string GetTransmitContext() ORTHANC_OVERRIDE
-    {
-      return transmitContext_;
-    }
-
     virtual void Execute() ORTHANC_OVERRIDE
     {
+      Logging::ThreadContextMemento::ScopedSetter setter(*callerMemento_);
       runnable_->Run();
     }
 
@@ -237,7 +226,7 @@
 
   void ThreadPool::WorkerLoop(const std::string& threadName)
   {
-    Logging::ScopedThreadNameSetter setter(threadName);
+    Logging::ScopedCurrentThreadNameSetter setter(threadName);
 
     while (true)
     {
@@ -270,8 +259,6 @@
         try
         {
           ITask& iTask = dynamic_cast<ITask&>(*task);
-          
-          Logging::ScopedContextSetter logContext(iTask.GetTransmitContext());
           iTask.Execute();
         }
         catch (const OrthancException& e)
@@ -400,33 +387,6 @@
     }
   }
 
-  static std::string GetContextToTransmit()
-  {
-    std::string transmitContext;
-
-    if (Logging::IsContextsEnabled())
-    {
-      std::vector<std::string> contexts;
-
-      if (false) // additional debug information: add the caller thread name
-      {
-        if (Logging::IsThreadNamesEnabled())
-        {
-          contexts.push_back(std::string("from " +  Logging::GetCurrentThreadName()));
-        }
-      }
-
-      std::string currentContext;
-      if (Logging::LookupCurrentContext(currentContext))
-      {
-        contexts.push_back(currentContext);
-      }
-      Toolbox::JoinStrings(transmitContext, contexts, " | ");
-    }
-
-    return transmitContext;
-  }
-
 
   Future* ThreadPool::Submit(ICallable* callable)
   {
@@ -448,7 +408,7 @@
 
     boost::shared_ptr<Internals::FutureState> state(boost::make_shared<Internals::FutureState>());
 
-    queue_.Enqueue(new CallableTask(protection.release(), state, GetContextToTransmit()));
+    queue_.Enqueue(new CallableTask(protection.release(), state));
 
     return new Future(state);
   }
@@ -472,7 +432,7 @@
       }
     }
 
-    queue_.Enqueue(new RunnableTask(protection.release(), GetContextToTransmit()));
+    queue_.Enqueue(new RunnableTask(protection.release()));
   }
 
 
--- a/OrthancFramework/UnitTestsSources/MemoryCacheTests.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancFramework/UnitTestsSources/MemoryCacheTests.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -403,7 +403,7 @@
 
 void ThreadingScenarioHappyThread1()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-1");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-1");
 
   // the first thread to call Fetch (will be in charge of adding)
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioHappyCache);
@@ -432,7 +432,7 @@
 
 void ThreadingScenarioHappyThread2()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-2");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-2");
 
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioHappyCache);
   std::string v;
@@ -476,7 +476,7 @@
 
 void ThreadingScenarioFailureThread1()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-1");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-1");
 
   // the first thread to call Fetch (will be in charge of adding)
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioFailureCache);
@@ -501,7 +501,7 @@
 
 void ThreadingScenarioFailureThread2()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-2");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-2");
 
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioFailureCache);
   std::string v;
@@ -539,7 +539,7 @@
 
 void ThreadingScenarioInvalidateThread1()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-1");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-1");
 
   // the first thread to call Fetch (will be in charge of adding)
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioInvalidateCache);
@@ -565,7 +565,7 @@
 
 void ThreadingScenarioInvalidateThread2()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("MEM-TEST-2");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("MEM-TEST-2");
 
   Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioInvalidateCache);
   std::string v;
--- a/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -188,7 +188,7 @@
 
 static void DeletionWorker()
 {
-  Orthanc::Logging::ScopedThreadNameSetter setter("DELETION");
+  Orthanc::Logging::ScopedCurrentThreadNameSetter setter("DELETION");
 
   static const unsigned int GRANULARITY = 100;  // In milliseconds
 
--- a/OrthancServer/Sources/LuaScripting.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/LuaScripting.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -1023,7 +1023,7 @@
 
   void LuaScripting::HeartBeatThread(LuaScripting* that)
   {
-    Logging::ScopedThreadNameSetter setter("LUA-HEARTBEAT");
+    Logging::ScopedCurrentThreadNameSetter setter("LUA-HEARTBEAT");
 
     static const unsigned int GRANULARITY = 100;  // In milliseconds
     
@@ -1061,7 +1061,7 @@
 
   void LuaScripting::EventThread(LuaScripting* that)
   {
-    Logging::ScopedThreadNameSetter setter("LUA-EVENTS");
+    Logging::ScopedCurrentThreadNameSetter setter("LUA-EVENTS");
 
     for (;;)
     {
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -1172,7 +1172,7 @@
 
   void OrthancWebDav::UploadWorker(OrthancWebDav* that)
   {
-    Logging::ScopedThreadNameSetter setter("WEBDAV-UPLOAD");
+    Logging::ScopedCurrentThreadNameSetter setter("WEBDAV-UPLOAD");
 
     assert(that != NULL);
 
--- a/OrthancServer/Sources/ServerContext.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -253,7 +253,7 @@
   void ServerContext::MemoryTrimmingThread(ServerContext* that,
                                            unsigned int intervalInSeconds)
   {
-    Logging::ScopedThreadNameSetter setter("MEMORY-TRIM");
+    Logging::ScopedCurrentThreadNameSetter setter("MEMORY-TRIM");
 
     boost::posix_time::ptime lastExecution = boost::posix_time::second_clock::universal_time();
 
@@ -283,7 +283,7 @@
   void ServerContext::ChangeThread(ServerContext* that,
                                    unsigned int sleepDelay)
   {
-    Logging::ScopedThreadNameSetter setter("CHANGES");
+    Logging::ScopedCurrentThreadNameSetter setter("CHANGES");
 
     while (!that->done_)
     {
@@ -327,7 +327,7 @@
   void ServerContext::JobEventsThread(ServerContext* that,
                                       unsigned int sleepDelay)
   {
-    Logging::ScopedThreadNameSetter setter("JOB-EVENTS");
+    Logging::ScopedCurrentThreadNameSetter setter("JOB-EVENTS");
 
     while (!that->done_)
     {
@@ -371,7 +371,7 @@
   void ServerContext::SaveJobsThread(ServerContext* that,
                                      unsigned int sleepDelay)
   {
-    Logging::ScopedThreadNameSetter setter("SAVE-JOBS");
+    Logging::ScopedCurrentThreadNameSetter setter("SAVE-JOBS");
 
     static const boost::posix_time::time_duration PERIODICITY =
       boost::posix_time::seconds(10);
--- a/OrthancServer/Sources/ServerIndex.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/ServerIndex.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -276,7 +276,7 @@
   void ServerIndex::FlushThread(ServerIndex* that,
                                 unsigned int threadSleepGranularityMilliseconds)
   {
-    Logging::ScopedThreadNameSetter setter("DB-FLUSH");
+    Logging::ScopedCurrentThreadNameSetter setter("DB-FLUSH");
 
     // By default, wait for 10 seconds before flushing
     static const unsigned int SLEEP_SECONDS = 10;
@@ -458,7 +458,7 @@
   void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that,
                                                    unsigned int threadSleepGranularityMilliseconds)
   {
-    Logging::ScopedThreadNameSetter setter("UNSTABLE-MON");
+    Logging::ScopedCurrentThreadNameSetter setter("UNSTABLE-MON");
 
     unsigned int stableAge = 0;
     
--- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -256,7 +256,7 @@
 
   void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that)
   {
-    Logging::ScopedThreadNameSetter setter(::GetInstanceWorkerThreadName());
+    Logging::ScopedCurrentThreadNameSetter setter(::GetInstanceWorkerThreadName());
 
     while (true)
     {
--- a/OrthancServer/Sources/main.cpp	Mon Jun 29 11:33:16 2026 +0200
+++ b/OrthancServer/Sources/main.cpp	Mon Jun 29 15:47:19 2026 +0200
@@ -2081,11 +2081,11 @@
     }
     else if (argument == "--logs-no-thread")
     {
-      Logging::EnableThreadNames(false);
+      Logging::SetThreadNamesEnabled(false);
     }
     else if (argument == "--logs-no-context")
     {
-      Logging::EnableContexts(false);
+      Logging::SetThreadContextsEnabled(false);
     }
     else if (argument == "--trace")
     {