changeset 6944:beaa09afa130 streaming

Logs context
author Alain Mazy <am@orthanc.team>
date Thu, 11 Jun 2026 10:04:31 +0200
parents ccfc89106db3
children 1c3879dd6b89
files NEWS OrthancFramework/Sources/JobsEngine/JobsEngine.cpp OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp OrthancFramework/Sources/Logging.cpp OrthancFramework/Sources/Logging.h OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancServer/Sources/main.cpp
diffstat 7 files changed, 195 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Jun 10 15:51:24 2026 +0200
+++ b/NEWS	Thu Jun 11 10:04:31 2026 +0200
@@ -26,6 +26,10 @@
   - StoreKeyValue(storeId, key, value)
   - value = GetKeyValue(storeId, key)
   - DeleteKeyValue(storeId, key)
+* Logs:
+  - Some line of log now contain additional contextual informations like the job id they relate to.
+    A new "--logs-no-context" command line option can be used to get back to the previous behavior to
+    keep backward compatibility and reduce the lines length.
 
 
 REST API
--- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp	Thu Jun 11 10:04:31 2026 +0200
@@ -140,6 +140,8 @@
         CLOG(INFO, JOBS) << "Executing " << jobType << " job with priority " << running.GetPriority()
                          << " in worker thread " << workerIndex << ": " << running.GetId();
 
+        Logging::ScopedContextSetter logContext(std::string("job ") + running.GetId());
+
         while (engine->IsRunning())
         {
           if (!engine->ExecuteStep(running, workerIndex))
--- a/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Thu Jun 11 10:04:31 2026 +0200
@@ -114,6 +114,8 @@
       pauseScheduled_(false),
       cancelScheduled_(false)
     {
+      Logging::ScopedContextSetter logContext(std::string("job ") + id_);
+      
       if (job == NULL)
       {
         throw OrthancException(ErrorCode_NullPointer);
--- a/OrthancFramework/Sources/Logging.cpp	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancFramework/Sources/Logging.cpp	Thu Jun 11 10:04:31 2026 +0200
@@ -30,6 +30,7 @@
 #include <cassert>
 #include <stdint.h>
 #include <string.h>
+#include <stack>
 
 #if defined(__linux__) && !defined(NDEBUG)
 #  include <pthread.h>
@@ -620,6 +621,8 @@
 }
 
 
+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_;
@@ -629,8 +632,11 @@
 static bool                                     hasOrthancAdvancedLogging_ = false;  // Whether the Orthanc runtime is >= 1.12.4
 static bool                                     hasClearThreadName_ = false;  // Whether the Orthanc runtime is >= 1.12.12
 static boost::recursive_mutex                   threadNamesMutex_;
-static std::map<boost::thread::id, std::string> threadNames_;
+static ThreadNamesMap                           threadNames_;
 static bool                                     enableThreadNames_ = true;
+static boost::recursive_mutex                   threadContextsMutex_;
+static ThreadContextsMap                        threadContexts_;
+static bool                                     enableContexts_ = true;
 static std::list<Orthanc::Logging::ILoggingListener*> loggingListeners_;
 static boost::mutex                                   loggingListenersMutex_;
 
@@ -644,6 +650,21 @@
       enableThreadNames_ = enabled;
     }
 
+    bool IsThreadNamesEnabled()
+    {
+      return enableThreadNames_;
+    }
+
+    void EnableContexts(bool enabled)
+    {
+      enableContexts_ = enabled;
+    }
+
+    bool IsContextsEnabled()
+    {
+      return enableContexts_;
+    }
+
     static void GetLogPath(boost::filesystem::path& log,
                            boost::filesystem::path& link,
                            const std::string& suffix,
@@ -756,7 +777,7 @@
 
     }
 
-    static std::string GetCurrentThreadName()
+    std::string GetCurrentThreadName()
     {
       boost::thread::id threadId = boost::this_thread::get_id();
 
@@ -771,6 +792,21 @@
       return threadNames_[threadId];
     }    
 
+    bool LookupCurrentContext(std::string& result)
+    {
+      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)
+      {
+        Toolbox::JoinStrings(result, threadContexts_[threadId], " | ");
+        return true;
+      }
+
+      return false;
+    }    
+
     ScopedThreadNameSetter::ScopedThreadNameSetter(const std::string& threadName)
     {
       SetCurrentThreadName(threadName);
@@ -781,6 +817,61 @@
       ClearCurrentThreadName();
     }
 
+    ScopedContextSetter::ScopedContextSetter(const std::string& context) :
+      hasPushedContext_(false)
+    {
+      if (!context.empty())
+      {
+        PushContext(context);
+        hasPushedContext_ = true;
+      }
+    }
+
+    ScopedContextSetter::~ScopedContextSetter()
+    {
+      if (hasPushedContext_)
+      {
+        PopContext();
+      }
+    }
+
+    void PushContext(const std::string& context)
+    {
+      if (context.size() == 0)
+      {
+        return;
+      }
+      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()
+    {
+      boost::thread::id threadId = boost::this_thread::get_id();
+
+      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);
+        }
+      }
+    }
 
     void AddLoggingListener(ILoggingListener* listener)
     {
@@ -870,6 +961,12 @@
         threadName[0] = '\0';
       }
 
+      std::string context;
+      if (enableContexts_ && LookupCurrentContext(context))
+      {
+        context = std::string("| ") + context + " | ";
+      }
+
       std::string internalPluginName = "";
       if (pluginName != NULL)
       {
@@ -877,7 +974,7 @@
       }
 
       prefix = (std::string(date) + threadName + internalPluginName + path.filename().string() + ":" +
-                boost::lexical_cast<std::string>(line) + "] ");
+                boost::lexical_cast<std::string>(line) + "] " + context);
 
       if (level != LogLevel_ERROR &&
           level != LogLevel_WARNING &&
--- a/OrthancFramework/Sources/Logging.h	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancFramework/Sources/Logging.h	Thu Jun 11 10:04:31 2026 +0200
@@ -83,6 +83,15 @@
       ~ScopedThreadNameSetter();
     };
 
+    class ScopedContextSetter
+    {
+      bool hasPushedContext_;
+    public:
+      explicit ScopedContextSetter(const std::string& context);
+
+      ~ScopedContextSetter();
+    };
+
     class ILoggingListener
     {
     public:
@@ -120,12 +129,26 @@
 
     ORTHANC_PUBLIC void ClearCurrentThreadName();
 
+    ORTHANC_PUBLIC std::string GetCurrentThreadName();
+
+    ORTHANC_PUBLIC void PushContext(const std::string& context);
+
+    ORTHANC_PUBLIC void PopContext();
+
+    ORTHANC_PUBLIC bool LookupCurrentContext(std::string& result);
+
     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);
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Thu Jun 11 10:04:31 2026 +0200
@@ -28,6 +28,7 @@
 #include "../Logging.h"
 #include "../OrthancException.h"
 #include "../MetricsRegistry.h"
+#include "../Toolbox.h"
 #include "FutureState.h"
 
 #include <boost/lexical_cast.hpp>
@@ -57,6 +58,8 @@
     virtual void Execute() = 0;
 
     virtual void Cancel() = 0;
+
+    virtual std::string GetTransmitContext() = 0;
   };
 
 
@@ -65,17 +68,25 @@
   private:
     std::unique_ptr<ICallable>               callable_;
     boost::weak_ptr<Internals::FutureState>  state_;
+    std::string                              transmitContext_;
 
   public:
     CallableTask(ICallable* callable,
-                 boost::shared_ptr<Internals::FutureState>& state) :
+                 boost::shared_ptr<Internals::FutureState>& state,
+                 const std::string& transmitContext) :
       callable_(callable),
-      state_(state)
+      state_(state),
+      transmitContext_(transmitContext)
     {
       assert(callable != NULL);
     }
 
-    void Execute() ORTHANC_OVERRIDE
+    virtual std::string GetTransmitContext() ORTHANC_OVERRIDE
+    {
+      return transmitContext_;
+    }
+
+    virtual void Execute() ORTHANC_OVERRIDE
     {
       boost::shared_ptr<Internals::FutureState> locked = state_.lock();
 
@@ -100,7 +111,7 @@
       }
     }
 
-    void Cancel() ORTHANC_OVERRIDE
+    virtual void Cancel() ORTHANC_OVERRIDE
     {
       boost::shared_ptr<Internals::FutureState> locked = state_.lock();
 
@@ -120,20 +131,28 @@
   {
   private:
     std::unique_ptr<IRunnable>  runnable_;
+    std::string                 transmitContext_;
 
   public:
-    explicit RunnableTask(IRunnable* runnable) :
-      runnable_(runnable)
+    explicit RunnableTask(IRunnable* runnable,
+                          const std::string& transmitContext) :
+      runnable_(runnable),
+      transmitContext_(transmitContext)
     {
       assert(runnable != NULL);
     }
 
-    void Execute() ORTHANC_OVERRIDE
+    virtual std::string GetTransmitContext() ORTHANC_OVERRIDE
+    {
+      return transmitContext_;
+    }
+
+    virtual void Execute() ORTHANC_OVERRIDE
     {
       runnable_->Run();
     }
 
-    void Cancel() ORTHANC_OVERRIDE
+    virtual void Cancel() ORTHANC_OVERRIDE
     {
     }
   };
@@ -250,7 +269,10 @@
 
         try
         {
-          dynamic_cast<ITask&>(*task).Execute();
+          ITask& iTask = dynamic_cast<ITask&>(*task);
+          
+          Logging::ScopedContextSetter logContext(iTask.GetTransmitContext());
+          iTask.Execute();
         }
         catch (const OrthancException& e)
         {
@@ -378,6 +400,33 @@
     }
   }
 
+  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)
   {
@@ -399,7 +448,7 @@
 
     boost::shared_ptr<Internals::FutureState> state(boost::make_shared<Internals::FutureState>());
 
-    queue_.Enqueue(new CallableTask(protection.release(), state));
+    queue_.Enqueue(new CallableTask(protection.release(), state, GetContextToTransmit()));
 
     return new Future(state);
   }
@@ -423,7 +472,7 @@
       }
     }
 
-    queue_.Enqueue(new RunnableTask(protection.release()));
+    queue_.Enqueue(new RunnableTask(protection.release(), GetContextToTransmit()));
   }
 
 
--- a/OrthancServer/Sources/main.cpp	Wed Jun 10 15:51:24 2026 +0200
+++ b/OrthancServer/Sources/main.cpp	Thu Jun 11 10:04:31 2026 +0200
@@ -2083,6 +2083,10 @@
     {
       Logging::EnableThreadNames(false);
     }
+    else if (argument == "--logs-no-context")
+    {
+      Logging::EnableContexts(false);
+    }
     else if (argument == "--trace")
     {
       SetGlobalVerbosity(Verbosity_Trace);