Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Logging.cpp @ 5450:9ffd6d18daf3 pg-transactions
log lines now contain the thread name
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 05 Dec 2023 16:26:35 +0100 |
parents | 0ea402b4d901 |
children | 8345267e8de5 |
comparison
equal
deleted
inserted
replaced
5449:29858f424fc7 | 5450:9ffd6d18daf3 |
---|---|
25 #include "Logging.h" | 25 #include "Logging.h" |
26 | 26 |
27 #include "OrthancException.h" | 27 #include "OrthancException.h" |
28 | 28 |
29 #include <stdint.h> | 29 #include <stdint.h> |
30 #include <boost/thread/thread.hpp> | |
30 | 31 |
31 | 32 |
32 /********************************************************* | 33 /********************************************************* |
33 * Common section | 34 * Common section |
34 *********************************************************/ | 35 *********************************************************/ |
532 }; | 533 }; |
533 } | 534 } |
534 | 535 |
535 | 536 |
536 | 537 |
537 static std::unique_ptr<LoggingStreamsContext> loggingStreamsContext_; | 538 static std::unique_ptr<LoggingStreamsContext> loggingStreamsContext_; |
538 static boost::mutex loggingStreamsMutex_; | 539 static boost::mutex loggingStreamsMutex_; |
539 static Orthanc::Logging::NullStream nullStream_; | 540 static Orthanc::Logging::NullStream nullStream_; |
540 static OrthancPluginContext* pluginContext_ = NULL; | 541 static OrthancPluginContext* pluginContext_ = NULL; |
542 static std::map<boost::thread::id, std::string> threadNames_; | |
543 static bool enableThreadNames_ = true; | |
544 | |
541 | 545 |
542 | 546 |
543 namespace Orthanc | 547 namespace Orthanc |
544 { | 548 { |
545 namespace Logging | 549 namespace Logging |
546 { | 550 { |
551 void EnableThreadNames(bool enabled) | |
552 { | |
553 enableThreadNames_ = enabled; | |
554 } | |
555 | |
547 static void GetLogPath(boost::filesystem::path& log, | 556 static void GetLogPath(boost::filesystem::path& log, |
548 boost::filesystem::path& link, | 557 boost::filesystem::path& link, |
549 const std::string& suffix, | 558 const std::string& suffix, |
550 const std::string& directory) | 559 const std::string& directory) |
551 { | 560 { |
610 !loggingStreamsContext_->file_->is_open()) | 619 !loggingStreamsContext_->file_->is_open()) |
611 { | 620 { |
612 throw OrthancException(ErrorCode_CannotWriteFile); | 621 throw OrthancException(ErrorCode_CannotWriteFile); |
613 } | 622 } |
614 } | 623 } |
615 | 624 |
625 void SetCurrentThreadNameInternal(const boost::thread::id& id, const std::string& name) | |
626 { | |
627 // this method assumes that the loggingStreamsMutex is already locked | |
628 if (name.size() > 16) | |
629 { | |
630 throw OrthancException(ErrorCode_InternalError, std::string("Thread name can not exceed 16 characters: ") + name); | |
631 } | |
632 | |
633 threadNames_[id] = name; | |
634 } | |
635 | |
636 void SetCurrentThreadName(const std::string& name) | |
637 { | |
638 boost::mutex::scoped_lock lock(loggingStreamsMutex_); | |
639 SetCurrentThreadNameInternal(boost::this_thread::get_id(), name); | |
640 } | |
641 | |
642 bool HasCurrentThreadName() | |
643 { | |
644 boost::thread::id threadId = boost::this_thread::get_id(); | |
645 | |
646 boost::mutex::scoped_lock lock(loggingStreamsMutex_); | |
647 return threadNames_.find(threadId) != threadNames_.end(); | |
648 } | |
649 | |
650 static std::string GetCurrentThreadName() | |
651 { | |
652 // this method assumes that the loggingStreamsMutex is already locked | |
653 boost::thread::id threadId = boost::this_thread::get_id(); | |
654 | |
655 if (threadNames_.find(threadId) == threadNames_.end()) | |
656 { | |
657 // set the threadId as the thread name | |
658 SetCurrentThreadNameInternal(threadId, boost::lexical_cast<std::string>(threadId)); | |
659 } | |
660 | |
661 return threadNames_[threadId]; | |
662 } | |
616 | 663 |
617 static void GetLinePrefix(std::string& prefix, | 664 static void GetLinePrefix(std::string& prefix, |
618 LogLevel level, | 665 LogLevel level, |
619 const char* file, | 666 const char* file, |
620 int line, | 667 int line, |
677 static_cast<int>(duration.hours()), | 724 static_cast<int>(duration.hours()), |
678 static_cast<int>(duration.minutes()), | 725 static_cast<int>(duration.minutes()), |
679 static_cast<int>(duration.seconds()), | 726 static_cast<int>(duration.seconds()), |
680 static_cast<int>(duration.fractional_seconds())); | 727 static_cast<int>(duration.fractional_seconds())); |
681 | 728 |
682 prefix = (std::string(date) + path.filename().string() + ":" + | 729 char threadName[20]; // thread names are limited to 16 char + a space |
730 if (enableThreadNames_) | |
731 { | |
732 sprintf(threadName, "%16s ", GetCurrentThreadName().c_str()); | |
733 } | |
734 else | |
735 { | |
736 threadName[0] = '\0'; | |
737 } | |
738 | |
739 prefix = (std::string(date) + threadName + path.filename().string() + ":" + | |
683 boost::lexical_cast<std::string>(line) + "] "); | 740 boost::lexical_cast<std::string>(line) + "] "); |
684 | 741 |
685 if (level != LogLevel_ERROR && | 742 if (level != LogLevel_ERROR && |
686 level != LogLevel_WARNING && | 743 level != LogLevel_WARNING && |
687 category != LogCategory_GENERIC) | 744 category != LogCategory_GENERIC) |