Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Logging.cpp @ 5561:0b18690c1935
SDK: added OrthancPluginLogMessage to display plugin name + file and line from plugin
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Tue, 23 Apr 2024 09:34:02 +0200 |
parents | 7c5d30a3d5d2 |
children | e02cdf358905 |
comparison
equal
deleted
inserted
replaced
5560:c80dbbae3f60 | 5561:0b18690c1935 |
---|---|
309 { | 309 { |
310 void InitializePluginContext(void* pluginContext) | 310 void InitializePluginContext(void* pluginContext) |
311 { | 311 { |
312 } | 312 } |
313 | 313 |
314 void InitializePluginContext(void* pluginContext, const char* pluginName) | |
315 { | |
316 } | |
317 | |
314 void Initialize() | 318 void Initialize() |
315 { | 319 { |
316 } | 320 } |
317 | 321 |
318 void Finalize() | 322 void Finalize() |
434 } | 438 } |
435 } | 439 } |
436 } | 440 } |
437 | 441 |
438 void InitializePluginContext(void* pluginContext) | 442 void InitializePluginContext(void* pluginContext) |
443 { | |
444 } | |
445 | |
446 void InitializePluginContext(void* pluginContext, const char* pluginName) | |
439 { | 447 { |
440 } | 448 } |
441 | 449 |
442 void Initialize() | 450 void Initialize() |
443 { | 451 { |
486 typedef enum | 494 typedef enum |
487 { | 495 { |
488 _OrthancPluginService_LogInfo = 1, | 496 _OrthancPluginService_LogInfo = 1, |
489 _OrthancPluginService_LogWarning = 2, | 497 _OrthancPluginService_LogWarning = 2, |
490 _OrthancPluginService_LogError = 3, | 498 _OrthancPluginService_LogError = 3, |
499 _OrthancPluginService_LogMessage = 45, | |
491 _OrthancPluginService_INTERNAL = 0x7fffffff | 500 _OrthancPluginService_INTERNAL = 0x7fffffff |
492 } _OrthancPluginService; | 501 } _OrthancPluginService; |
493 | 502 |
494 typedef struct _OrthancPluginContext_t | 503 typedef struct _OrthancPluginContext_t |
495 { | 504 { |
498 void (*Free) (void* buffer); | 507 void (*Free) (void* buffer); |
499 int32_t (*InvokeService) (struct _OrthancPluginContext_t* context, | 508 int32_t (*InvokeService) (struct _OrthancPluginContext_t* context, |
500 _OrthancPluginService service, | 509 _OrthancPluginService service, |
501 const void* params); | 510 const void* params); |
502 } OrthancPluginContext; | 511 } OrthancPluginContext; |
512 | |
513 typedef struct | |
514 { | |
515 const char* message; | |
516 const char* plugin; | |
517 const char* file; | |
518 uint32_t line; | |
519 uint32_t category; // can be a LogCategory or a OrthancPluginLogCategory | |
520 uint32_t level; // can be a LogLevel or a OrthancPluginLogLevel | |
521 } _OrthancPluginLogMessage; | |
522 | |
503 } | 523 } |
504 | 524 |
505 | 525 |
506 #include "Enumerations.h" | 526 #include "Enumerations.h" |
507 #include "SystemToolbox.h" | 527 #include "SystemToolbox.h" |
537 | 557 |
538 | 558 |
539 static std::unique_ptr<LoggingStreamsContext> loggingStreamsContext_; | 559 static std::unique_ptr<LoggingStreamsContext> loggingStreamsContext_; |
540 static boost::mutex loggingStreamsMutex_; | 560 static boost::mutex loggingStreamsMutex_; |
541 static Orthanc::Logging::NullStream nullStream_; | 561 static Orthanc::Logging::NullStream nullStream_; |
542 static OrthancPluginContext* pluginContext_ = NULL; | 562 static OrthancPluginContext* pluginContext_ = NULL; // this is != NULL only when running from a plugin |
563 static const char* pluginName_ = NULL; // this is != NULL only when running from a plugin | |
543 static boost::recursive_mutex threadNamesMutex_; | 564 static boost::recursive_mutex threadNamesMutex_; |
544 static std::map<boost::thread::id, std::string> threadNames_; | 565 static std::map<boost::thread::id, std::string> threadNames_; |
545 static bool enableThreadNames_ = true; | 566 static bool enableThreadNames_ = true; |
546 | 567 |
547 | 568 |
665 return threadNames_[threadId]; | 686 return threadNames_[threadId]; |
666 } | 687 } |
667 | 688 |
668 static void GetLinePrefix(std::string& prefix, | 689 static void GetLinePrefix(std::string& prefix, |
669 LogLevel level, | 690 LogLevel level, |
691 const char* pluginName, // when logging in the core but coming from a plugin, pluginName_ is NULL but this argument is != NULL | |
670 const char* file, | 692 const char* file, |
671 int line, | 693 int line, |
672 LogCategory category) | 694 LogCategory category) |
673 { | 695 { |
674 boost::filesystem::path path(file); | 696 boost::filesystem::path path(file); |
738 else | 760 else |
739 { | 761 { |
740 threadName[0] = '\0'; | 762 threadName[0] = '\0'; |
741 } | 763 } |
742 | 764 |
743 prefix = (std::string(date) + threadName + path.filename().string() + ":" + | 765 std::string internalPluginName = ""; |
766 if (pluginName != NULL) | |
767 { | |
768 internalPluginName = std::string(pluginName) + ":/"; | |
769 } | |
770 | |
771 prefix = (std::string(date) + threadName + internalPluginName + path.filename().string() + ":" + | |
744 boost::lexical_cast<std::string>(line) + "] "); | 772 boost::lexical_cast<std::string>(line) + "] "); |
745 | 773 |
746 if (level != LogLevel_ERROR && | 774 if (level != LogLevel_ERROR && |
747 level != LogLevel_WARNING && | 775 level != LogLevel_WARNING && |
748 category != LogCategory_GENERIC) | 776 category != LogCategory_GENERIC) |
759 boost::mutex::scoped_lock lock(loggingStreamsMutex_); | 787 boost::mutex::scoped_lock lock(loggingStreamsMutex_); |
760 loggingStreamsContext_.reset(NULL); | 788 loggingStreamsContext_.reset(NULL); |
761 pluginContext_ = reinterpret_cast<OrthancPluginContext*>(pluginContext); | 789 pluginContext_ = reinterpret_cast<OrthancPluginContext*>(pluginContext); |
762 | 790 |
763 EnableInfoLevel(true); // allow the plugin to log at info level (but the Orthanc Core still decides of the level) | 791 EnableInfoLevel(true); // allow the plugin to log at info level (but the Orthanc Core still decides of the level) |
792 } | |
793 | |
794 void InitializePluginContext(void* pluginContext, const char* pluginName) | |
795 { | |
796 InitializePluginContext(pluginContext); | |
797 pluginName_ = pluginName; | |
764 } | 798 } |
765 | 799 |
766 | 800 |
767 void Initialize() | 801 void Initialize() |
768 { | 802 { |
835 } | 869 } |
836 } | 870 } |
837 | 871 |
838 | 872 |
839 void InternalLogger::Setup(LogCategory category, | 873 void InternalLogger::Setup(LogCategory category, |
874 const char* pluginName, | |
840 const char* file, | 875 const char* file, |
841 int line) | 876 int line) |
842 { | 877 { |
843 if (pluginContext_ != NULL) | 878 if (pluginContext_ != NULL) |
844 { | 879 { |
867 // stream is set to "/dev/null" | 902 // stream is set to "/dev/null" |
868 return; | 903 return; |
869 } | 904 } |
870 | 905 |
871 std::string prefix; | 906 std::string prefix; |
872 GetLinePrefix(prefix, level_, file, line, category); | 907 GetLinePrefix(prefix, level_, pluginName, file, line, category); |
873 | 908 |
874 { | 909 { |
875 // We lock the global mutex. The mutex is locked until the | 910 // We lock the global mutex. The mutex is locked until the |
876 // destructor is called: No change in the output can be done. | 911 // destructor is called: No change in the output can be done. |
877 lock_.lock(); | 912 lock_.lock(); |
932 LogCategory category, | 967 LogCategory category, |
933 const char* file, | 968 const char* file, |
934 int line) : | 969 int line) : |
935 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | 970 lock_(loggingStreamsMutex_, boost::defer_lock_t()), |
936 level_(level), | 971 level_(level), |
937 stream_(&nullStream_) // By default, logging to "/dev/null" is simulated | 972 stream_(&nullStream_), // By default, logging to "/dev/null" is simulated |
938 { | 973 category_(category), |
939 Setup(category, file, line); | 974 file_(file), |
940 } | 975 line_(line) |
976 { | |
977 Setup(category, NULL, file, line); | |
978 } | |
979 | |
980 InternalLogger::InternalLogger(LogLevel level, | |
981 LogCategory category, | |
982 const char* pluginName, | |
983 const char* file, | |
984 int line) : | |
985 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | |
986 level_(level), | |
987 stream_(&nullStream_), // By default, logging to "/dev/null" is simulated | |
988 category_(category), | |
989 file_(file), | |
990 line_(line) | |
991 { | |
992 Setup(category, pluginName, file, line); | |
993 } | |
994 | |
941 | 995 |
942 | 996 |
943 InternalLogger::InternalLogger(LogLevel level, | 997 InternalLogger::InternalLogger(LogLevel level, |
944 const char* file, | 998 const char* file, |
945 int line) : | 999 int line) : |
946 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | 1000 lock_(loggingStreamsMutex_, boost::defer_lock_t()), |
947 level_(level), | 1001 level_(level), |
948 stream_(&nullStream_) // By default, logging to "/dev/null" is simulated | 1002 stream_(&nullStream_), // By default, logging to "/dev/null" is simulated |
949 { | 1003 category_(LogCategory_GENERIC), |
950 Setup(LogCategory_GENERIC, file, line); | 1004 file_(file), |
1005 line_(line) | |
1006 { | |
1007 Setup(LogCategory_GENERIC, NULL, file, line); | |
951 } | 1008 } |
952 | 1009 |
953 | 1010 |
954 InternalLogger::~InternalLogger() | 1011 InternalLogger::~InternalLogger() |
955 { | 1012 { |
959 | 1016 |
960 std::string message = pluginStream_->str(); | 1017 std::string message = pluginStream_->str(); |
961 | 1018 |
962 if (pluginContext_ != NULL) | 1019 if (pluginContext_ != NULL) |
963 { | 1020 { |
964 switch (level_) | 1021 if (pluginName_ != NULL) // this shall happen only if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 12, 4) && ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) |
965 { | 1022 { |
966 case LogLevel_ERROR: | 1023 _OrthancPluginLogMessage m; |
967 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogError, message.c_str()); | 1024 m.category = category_; |
968 break; | 1025 m.level = level_; |
969 | 1026 m.file = file_; |
970 case LogLevel_WARNING: | 1027 m.line = line_; |
971 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogWarning, message.c_str()); | 1028 m.plugin = pluginName_; |
972 break; | 1029 m.message = message.c_str(); |
973 | 1030 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogMessage, &m); |
974 case LogLevel_INFO: | 1031 } |
975 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogInfo, message.c_str()); | 1032 else |
976 break; | 1033 { |
977 | 1034 switch (level_) |
978 default: | 1035 { |
979 break; | 1036 case LogLevel_ERROR: |
1037 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogError, message.c_str()); | |
1038 break; | |
1039 | |
1040 case LogLevel_WARNING: | |
1041 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogWarning, message.c_str()); | |
1042 break; | |
1043 | |
1044 case LogLevel_INFO: | |
1045 pluginContext_->InvokeService(pluginContext_, _OrthancPluginService_LogInfo, message.c_str()); | |
1046 break; | |
1047 | |
1048 default: | |
1049 break; | |
1050 } | |
980 } | 1051 } |
981 } | 1052 } |
982 } | 1053 } |
983 else if (stream_ != &nullStream_) | 1054 else if (stream_ != &nullStream_) |
984 { | 1055 { |