Mercurial > hg > orthanc
changeset 6964:5eb525758b7f streaming
refactoring Logging::threadNames_
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Mon, 29 Jun 2026 11:33:16 +0200 |
| parents | a710e864790d |
| children | d28c3fb90bd6 |
| files | OrthancFramework/Sources/Logging.cpp |
| diffstat | 1 files changed, 240 insertions(+), 54 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/Logging.cpp Fri Jun 26 15:12:06 2026 +0200 +++ b/OrthancFramework/Sources/Logging.cpp Mon Jun 29 11:33:16 2026 +0200 @@ -30,7 +30,6 @@ #include <cassert> #include <stdint.h> #include <string.h> -#include <stack> #if defined(__linux__) && !defined(NDEBUG) # include <pthread.h> @@ -538,10 +537,12 @@ #include "SystemToolbox.h" #include "Toolbox.h" +#include <boost/algorithm/string/join.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp> #include <boost/thread.hpp> #include <fstream> +#include <list> namespace @@ -621,6 +622,214 @@ } +static const size_t THREAD_NAME_MAX_SIZE = 16; // Thread names are limited to 16 char + a space + +namespace +{ + class ThreadInformation : public boost::noncopyable + { + private: + bool hasName_; + std::string name_; + + public: + ThreadInformation() : + hasName_(false) + { + } + + void SetThreadName(const std::string& name) + { +#if !defined(NDEBUG) + if (hasName_) + { + 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); + } +#endif + + hasName_ = true; + + if (name.size() < THREAD_NAME_MAX_SIZE) + { + name_ = std::string(THREAD_NAME_MAX_SIZE - name.size(), ' ') + name; + } + else if (name.size() == THREAD_NAME_MAX_SIZE) + { + name_ = name; + } + else + { + name_ = name.substr(THREAD_NAME_MAX_SIZE); + } + + assert(name_.size() == THREAD_NAME_MAX_SIZE); + } + + void ClearThreadName() + { + hasName_ = false; + name_.clear(); + } + + bool HasThreadName() const + { + return hasName_; + } + + std::string GetThreadName() const + { + if (hasName_) + { + return name_; + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + } + }; + + + class ThreadsInformations : public boost::noncopyable + { + private: + typedef std::map<boost::thread::id, ThreadInformation*> Content; + + boost::shared_mutex mutex_; + Content content_; + + public: + ~ThreadsInformations() + { + Clear(); + } + + void Clear() + { + boost::unique_lock<boost::shared_mutex> lock(mutex_); + + for (Content::iterator it = content_.begin(); it != content_.end(); ++it) + { + assert(it->second != NULL); + delete it->second; + } + + content_.clear(); + } + + + class Reader : public boost::noncopyable + { + private: + boost::shared_lock<boost::shared_mutex> lock_; + boost::thread::id threadId_; + bool exists_; + const ThreadInformation* information_; + + public: + explicit Reader(ThreadsInformations& informations) : + lock_(informations.mutex_), + threadId_(boost::this_thread::get_id()), + exists_(false), + information_(NULL) + { + Content::const_iterator found = informations.content_.find(threadId_); + + if (found != informations.content_.end()) + { + assert(found->second != NULL); + + exists_ = true; + information_ = found->second; + } + } + + std::string GetThreadName() const + { + assert(!exists_ || information_ != NULL); + + if (exists_ && + information_->HasThreadName()) + { + return information_->GetThreadName(); + } + else + { + return boost::lexical_cast<std::string>(threadId_); + } + } + + bool HasThreadName() const + { + assert(!exists_ || information_ != NULL); + return (exists_ && + information_->HasThreadName()); + } + }; + + + class Writer : public boost::noncopyable + { + private: + ThreadsInformations& informations_; + boost::unique_lock<boost::shared_mutex> lock_; + boost::thread::id threadId_; + ThreadInformation* information_; + + public: + explicit Writer(ThreadsInformations& informations) : + informations_(informations), + lock_(informations.mutex_), + threadId_(boost::this_thread::get_id()) + { + Content::iterator found = informations.content_.find(threadId_); + + if (found == informations.content_.end()) + { + information_ = new ThreadInformation; + informations.content_[threadId_] = information_; + } + else + { + assert(found->second != NULL); + information_ = found->second; + } + } + + void SetThreadName(const std::string& name) + { + assert(information_ != NULL); + information_->SetThreadName(name); + +#if !defined(NDEBUG) + const std::string formattedName = information_->GetThreadName(); + + for (Content::const_iterator it = informations_.content_.begin(); it != informations_.content_.end(); ++it) + { + assert(it->second != NULL); + if (it->first != threadId_ && + it->second->HasThreadName() && + it->second->GetThreadName() == formattedName) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "Another thread already uses this thread name: " + name); + } + } +#endif + } + + void ClearThreadName() + { + assert(information_ != NULL); + information_->ClearThreadName(); + } + }; + }; +} + + typedef std::map<boost::thread::id, std::string> ThreadNamesMap; typedef std::map<boost::thread::id, std::vector<std::string> > ThreadContextsMap; @@ -631,8 +840,7 @@ static std::string pluginName_; // this string can only be non-empty if running from a plugin 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::shared_mutex threadNamesMutex_; -static ThreadNamesMap threadNames_; +static ThreadsInformations threadsInformations_; static bool enableThreadNames_ = true; static boost::recursive_mutex threadContextsMutex_; static ThreadContextsMap threadContexts_; @@ -725,34 +933,17 @@ void SetCurrentThreadName(const std::string& name) { - if (name.size() > 16) + if (name.size() > THREAD_NAME_MAX_SIZE) { - throw OrthancException(ErrorCode_InternalError, std::string("Thread name can not exceed 16 characters: ") + name); + throw OrthancException(ErrorCode_InternalError, "Thread name can not exceed " + + boost::lexical_cast<std::string>(THREAD_NAME_MAX_SIZE) + ": " + name); } if (pluginContext_ == NULL) { - const boost::thread::id threadId = boost::this_thread::get_id(); - { - boost::unique_lock<boost::shared_mutex> lock(threadNamesMutex_); - -#if !defined(NDEBUG) - if (threadNames_.find(threadId) != threadNames_.end()) - { - throw OrthancException(ErrorCode_InternalError, std::string("This thread already has a name or another thread is re-using the same threadId and you have not called 'ClearCurrentThreadName()': ") + name); - } - - for (ThreadNamesMap::const_iterator it = threadNames_.begin(); it != threadNames_.end(); ++it) - { - if (it->second == name) - { - throw OrthancException(ErrorCode_InternalError, std::string("Another thread already uses this thread name: ") + name); - } - } -#endif - - threadNames_[threadId] = name; + ThreadsInformations::Writer writer(threadsInformations_); + writer.SetThreadName(name); } #if defined(__linux__) && !defined(NDEBUG) && !defined(__LSB_VERSION__) @@ -769,11 +960,14 @@ bool HasCurrentThreadName() { - const boost::thread::id threadId = boost::this_thread::get_id(); - + if (pluginContext_ == NULL) { - boost::shared_lock<boost::shared_mutex> lock(threadNamesMutex_); - return threadNames_.find(threadId) != threadNames_.end(); + ThreadsInformations::Reader reader(threadsInformations_); + return reader.HasThreadName(); + } + else + { + throw OrthancException(ErrorCode_NotImplemented); // Not available for plugins } } @@ -782,12 +976,8 @@ { if (pluginContext_ == NULL) { - const boost::thread::id threadId = boost::this_thread::get_id(); - - { - boost::unique_lock<boost::shared_mutex> lock(threadNamesMutex_); - threadNames_.erase(threadId); - } + ThreadsInformations::Writer writer(threadsInformations_); + writer.ClearThreadName(); } else if (hasClearThreadName_) // only recent runtimes support it (from 1.12.12) { @@ -798,20 +988,15 @@ std::string GetCurrentThreadName() { - const boost::thread::id threadId = boost::this_thread::get_id(); - + if (pluginContext_ == NULL) { - boost::shared_lock<boost::shared_mutex> lock(threadNamesMutex_); - - std::map<boost::thread::id, std::string>::const_iterator found = threadNames_.find(threadId); - if (found != threadNames_.end()) - { - return found->second; - } + ThreadsInformations::Reader reader(threadsInformations_); + return reader.GetThreadName(); } - - // "SetCurrentThreadName()" has not been invoked for this thread - return boost::lexical_cast<std::string>(threadId); + else + { + throw OrthancException(ErrorCode_NotImplemented); // Not available for plugins + } } @@ -978,14 +1163,15 @@ static_cast<int>(duration.seconds()), static_cast<int>(duration.fractional_seconds())); - char threadName[20]; // thread names are limited to 16 char + a space - if (enableThreadNames_) + std::string threadName; + { - sprintf(threadName, "%16s ", GetCurrentThreadName().c_str()); - } - else - { - threadName[0] = '\0'; + ThreadsInformations::Reader reader(threadsInformations_); + + if (enableThreadNames_) + { + threadName = reader.GetThreadName() + " "; + } } std::string context;
