comparison OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 5572:f0dc99bc811c

removed circular dependency of OrthancPluginsCppWrapper with Orthanc::Logging
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Apr 2024 15:50:01 +0200
parents cc4b7b4d5deb
children d7eaa568da15
comparison
equal deleted inserted replaced
5571:addccb1590d2 5572:f0dc99bc811c
59 59
60 60
61 namespace OrthancPlugins 61 namespace OrthancPlugins
62 { 62 {
63 static OrthancPluginContext* globalContext_ = NULL; 63 static OrthancPluginContext* globalContext_ = NULL;
64 static std::string pluginName_;
64 65
65 66
66 void SetGlobalContext(OrthancPluginContext* context) 67 void SetGlobalContext(OrthancPluginContext* context)
67 { 68 {
68 if (context == NULL) 69 if (context == NULL)
77 { 78 {
78 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); 79 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
79 } 80 }
80 } 81 }
81 82
82 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) && ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 12, 4) 83
83 static const char* pluginName_ = NULL; 84 void SetGlobalContext(OrthancPluginContext* context,
84 85 const char* pluginName)
85 void SetGlobalContext(OrthancPluginContext* context, const char* pluginName)
86 { 86 {
87 SetGlobalContext(context); 87 SetGlobalContext(context);
88 pluginName_ = pluginName; 88 pluginName_ = pluginName;
89 } 89 }
90 #endif 90
91 91
92 void ResetGlobalContext() 92 void ResetGlobalContext()
93 { 93 {
94 globalContext_ = NULL; 94 globalContext_ = NULL;
95 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) && ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 12, 4) 95 pluginName_.clear();
96 pluginName_ = NULL;
97 #endif
98 } 96 }
99 97
100 bool HasGlobalContext() 98 bool HasGlobalContext()
101 { 99 {
102 return globalContext_ != NULL; 100 return globalContext_ != NULL;
113 { 111 {
114 return globalContext_; 112 return globalContext_;
115 } 113 }
116 } 114 }
117 115
116 void LogMessage(OrthancPluginLogLevel level,
117 const char* file,
118 uint32_t line,
119 const std::string& message)
120 {
121 if (HasGlobalContext())
122 {
123 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4)
124 const char* pluginName = (pluginName_.empty() ? NULL : pluginName_.c_str());
125 OrthancPluginLogMessage(GetGlobalContext(), message.c_str(), pluginName, file, line, OrthancPluginLogCategory_Generic, level);
126 #else
127 switch (level)
128 {
129 case OrthancPluginLogLevel_Error:
130 OrthancPluginLogError(GetGlobalContext(), message.c_str());
131 break;
132
133 case OrthancPluginLogLevel_Warning:
134 OrthancPluginLogWarning(GetGlobalContext(), message.c_str());
135 break;
136
137 case OrthancPluginLogLevel_Info:
138 OrthancPluginLogInfo(GetGlobalContext(), message.c_str());
139 break;
140
141 default:
142 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
143 }
144 #endif
145 }
146 }
147
118 void LogError(const std::string& message) 148 void LogError(const std::string& message)
119 { 149 {
120 if (HasGlobalContext()) 150 if (HasGlobalContext())
121 { 151 {
122 OrthancPluginLogError(GetGlobalContext(), message.c_str()); 152 OrthancPluginLogError(GetGlobalContext(), message.c_str());
136 if (HasGlobalContext()) 166 if (HasGlobalContext())
137 { 167 {
138 OrthancPluginLogInfo(GetGlobalContext(), message.c_str()); 168 OrthancPluginLogInfo(GetGlobalContext(), message.c_str());
139 } 169 }
140 } 170 }
141
142
143 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) && ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 12, 4)
144 // This file does not have any dependencies on Logging.h, we must call the "native" plugin service
145
146 void _LogMessage(OrthancPluginLogLevel level, const char* file, uint32_t line, const std::string& message)
147 {
148 if (HasGlobalContext())
149 {
150 OrthancPluginLogMessage(GetGlobalContext(), message.c_str(), pluginName_, file, line, OrthancPluginLogCategory_Generic, level);
151 }
152 }
153
154 #define LOG_ERROR(msg) _LogMessage(OrthancPluginLogLevel_Error, __ORTHANC_FILE__, __LINE__, msg);
155 #define LOG_WARNING(msg) _LogMessage(OrthancPluginLogLevel_Warning, __ORTHANC_FILE__, __LINE__, msg);
156 #define LOG_INFO(msg) _LogMessage(OrthancPluginLogLevel_Info, __ORTHANC_FILE__, __LINE__, msg);
157
158 #else
159
160 #define LOG_ERROR(msg) LogError(msg);
161 #define LOG_WARNING(msg) LogWarning(msg);
162 #define LOG_INFO(msg) LogInfo(msg);
163
164 #endif
165 171
166 172
167 void MemoryBuffer::Check(OrthancPluginErrorCode code) 173 void MemoryBuffer::Check(OrthancPluginErrorCode code)
168 { 174 {
169 if (code != OrthancPluginErrorCode_Success) 175 if (code != OrthancPluginErrorCode_Success)
292 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 298 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
293 } 299 }
294 300
295 if (!ReadJson(target, buffer_.data, buffer_.size)) 301 if (!ReadJson(target, buffer_.data, buffer_.size))
296 { 302 {
297 LOG_ERROR("Cannot convert some memory buffer to JSON"); 303 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
298 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 304 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
299 } 305 }
300 } 306 }
301 307
302 308
324 330
325 public: 331 public:
326 explicit PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders) 332 explicit PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders)
327 { 333 {
328 for (std::map<std::string, std::string>::const_iterator 334 for (std::map<std::string, std::string>::const_iterator
329 it = httpHeaders.begin(); it != httpHeaders.end(); ++it) 335 it = httpHeaders.begin(); it != httpHeaders.end(); ++it)
330 { 336 {
331 headersKeys_.push_back(it->first.c_str()); 337 headersKeys_.push_back(it->first.c_str());
332 headersValues_.push_back(it->second.c_str()); 338 headersValues_.push_back(it->second.c_str());
333 } 339 }
334 } 340 }
463 { 469 {
464 return true; 470 return true;
465 } 471 }
466 else 472 else
467 { 473 {
468 LOG_ERROR("Cannot parse JSON: " + std::string(err)); 474 ORTHANC_PLUGINS_LOG_ERROR("Cannot parse JSON: " + std::string(err));
469 return false; 475 return false;
470 } 476 }
471 #endif 477 #endif
472 } 478 }
473 479
624 630
625 void OrthancString::ToJson(Json::Value& target) const 631 void OrthancString::ToJson(Json::Value& target) const
626 { 632 {
627 if (str_ == NULL) 633 if (str_ == NULL)
628 { 634 {
629 LOG_ERROR("Cannot convert an empty memory buffer to JSON"); 635 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert an empty memory buffer to JSON");
630 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 636 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
631 } 637 }
632 638
633 if (!ReadJson(target, str_)) 639 if (!ReadJson(target, str_))
634 { 640 {
635 LOG_ERROR("Cannot convert some memory buffer to JSON"); 641 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
636 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 642 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
637 } 643 }
638 } 644 }
639 645
640 646
641 void OrthancString::ToJsonWithoutComments(Json::Value& target) const 647 void OrthancString::ToJsonWithoutComments(Json::Value& target) const
642 { 648 {
643 if (str_ == NULL) 649 if (str_ == NULL)
644 { 650 {
645 LOG_ERROR("Cannot convert an empty memory buffer to JSON"); 651 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert an empty memory buffer to JSON");
646 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 652 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
647 } 653 }
648 654
649 if (!ReadJsonWithoutComments(target, str_)) 655 if (!ReadJsonWithoutComments(target, str_))
650 { 656 {
651 LOG_ERROR("Cannot convert some memory buffer to JSON"); 657 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
652 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 658 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
653 } 659 }
654 } 660 }
655 661
656 662
684 { 690 {
685 Clear(); 691 Clear();
686 692
687 if (body.size() > 0xffffffffu) 693 if (body.size() > 0xffffffffu)
688 { 694 {
689 LOG_ERROR("Cannot handle body size > 4GB"); 695 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
690 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 696 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
691 } 697 }
692 698
693 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), 699 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(),
694 body.c_str(), body.size(), 700 body.c_str(), body.size(),
704 { 710 {
705 Clear(); 711 Clear();
706 712
707 if (body.size() > 0xffffffffu) 713 if (body.size() > 0xffffffffu)
708 { 714 {
709 LOG_ERROR("Cannot handle body size > 4GB"); 715 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
710 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 716 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
711 } 717 }
712 718
713 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), 719 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(),
714 body.empty() ? NULL : body.c_str(), 720 body.empty() ? NULL : body.c_str(),
754 OrthancString str; 760 OrthancString str;
755 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext())); 761 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext()));
756 762
757 if (str.GetContent() == NULL) 763 if (str.GetContent() == NULL)
758 { 764 {
759 LOG_ERROR("Cannot access the Orthanc configuration"); 765 ORTHANC_PLUGINS_LOG_ERROR("Cannot access the Orthanc configuration");
760 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 766 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
761 } 767 }
762 768
763 str.ToJsonWithoutComments(configuration_); 769 str.ToJsonWithoutComments(configuration_);
764 770
765 if (configuration_.type() != Json::objectValue) 771 if (configuration_.type() != Json::objectValue)
766 { 772 {
767 LOG_ERROR("Unable to read the Orthanc configuration"); 773 ORTHANC_PLUGINS_LOG_ERROR("Unable to read the Orthanc configuration");
768 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 774 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
769 } 775 }
770 } 776 }
771 777
772 778
830 } 836 }
831 else 837 else
832 { 838 {
833 if (configuration_[key].type() != Json::objectValue) 839 if (configuration_[key].type() != Json::objectValue)
834 { 840 {
835 LOG_ERROR("The configuration section \"" + target.path_ + 841 ORTHANC_PLUGINS_LOG_ERROR("The configuration section \"" + target.path_ +
836 "\" is not an associative array as expected"); 842 "\" is not an associative array as expected");
837 843
838 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 844 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
839 } 845 }
840 846
841 target.configuration_ = configuration_[key]; 847 target.configuration_ = configuration_[key];
853 return false; 859 return false;
854 } 860 }
855 861
856 if (configuration_[key].type() != Json::stringValue) 862 if (configuration_[key].type() != Json::stringValue)
857 { 863 {
858 LOG_ERROR("The configuration option \"" + GetPath(key) + 864 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
859 "\" is not a string as expected"); 865 "\" is not a string as expected");
860 866
861 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 867 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
862 } 868 }
863 869
864 target = configuration_[key].asString(); 870 target = configuration_[key].asString();
885 case Json::uintValue: 891 case Json::uintValue:
886 target = configuration_[key].asUInt(); 892 target = configuration_[key].asUInt();
887 return true; 893 return true;
888 894
889 default: 895 default:
890 LOG_ERROR("The configuration option \"" + GetPath(key) + 896 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
891 "\" is not an integer as expected"); 897 "\" is not an integer as expected");
892 898
893 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 899 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
894 } 900 }
895 } 901 }
896 902
904 return false; 910 return false;
905 } 911 }
906 912
907 if (tmp < 0) 913 if (tmp < 0)
908 { 914 {
909 LOG_ERROR("The configuration option \"" + GetPath(key) + 915 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
910 "\" is not a positive integer as expected"); 916 "\" is not a positive integer as expected");
911 917
912 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 918 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
913 } 919 }
914 else 920 else
915 { 921 {
929 return false; 935 return false;
930 } 936 }
931 937
932 if (configuration_[key].type() != Json::booleanValue) 938 if (configuration_[key].type() != Json::booleanValue)
933 { 939 {
934 LOG_ERROR("The configuration option \"" + GetPath(key) + 940 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
935 "\" is not a Boolean as expected"); 941 "\" is not a Boolean as expected");
936 942
937 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 943 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
938 } 944 }
939 945
940 target = configuration_[key].asBool(); 946 target = configuration_[key].asBool();
965 case Json::uintValue: 971 case Json::uintValue:
966 target = static_cast<float>(configuration_[key].asUInt()); 972 target = static_cast<float>(configuration_[key].asUInt());
967 return true; 973 return true;
968 974
969 default: 975 default:
970 LOG_ERROR("The configuration option \"" + GetPath(key) + 976 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
971 "\" is not an integer as expected"); 977 "\" is not an integer as expected");
972 978
973 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 979 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
974 } 980 }
975 } 981 }
976 982
1025 1031
1026 default: 1032 default:
1027 break; 1033 break;
1028 } 1034 }
1029 1035
1030 LOG_ERROR("The configuration option \"" + GetPath(key) + 1036 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
1031 "\" is not a list of strings as expected"); 1037 "\" is not a list of strings as expected");
1032 1038
1033 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1039 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1034 } 1040 }
1035 1041
1036 1042
1146 return; 1152 return;
1147 } 1153 }
1148 1154
1149 if (configuration_[key].type() != Json::objectValue) 1155 if (configuration_[key].type() != Json::objectValue)
1150 { 1156 {
1151 LOG_ERROR("The configuration option \"" + GetPath(key) + 1157 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
1152 "\" is not an object as expected"); 1158 "\" is not an object as expected");
1153 1159
1154 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1160 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1155 } 1161 }
1156 1162
1157 Json::Value::Members members = configuration_[key].getMemberNames(); 1163 Json::Value::Members members = configuration_[key].getMemberNames();
1164 { 1170 {
1165 target[members[i]] = value.asString(); 1171 target[members[i]] = value.asString();
1166 } 1172 }
1167 else 1173 else
1168 { 1174 {
1169 LOG_ERROR("The configuration option \"" + GetPath(key) + 1175 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
1170 "\" is not a dictionary mapping strings to strings"); 1176 "\" is not a dictionary mapping strings to strings");
1171 1177
1172 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1178 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1173 } 1179 }
1174 } 1180 }
1175 } 1181 }
1187 1193
1188 void OrthancImage::CheckImageAvailable() const 1194 void OrthancImage::CheckImageAvailable() const
1189 { 1195 {
1190 if (image_ == NULL) 1196 if (image_ == NULL)
1191 { 1197 {
1192 LOG_ERROR("Trying to access a NULL image"); 1198 ORTHANC_PLUGINS_LOG_ERROR("Trying to access a NULL image");
1193 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1199 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1194 } 1200 }
1195 } 1201 }
1196 1202
1197 1203
1213 { 1219 {
1214 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height); 1220 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height);
1215 1221
1216 if (image_ == NULL) 1222 if (image_ == NULL)
1217 { 1223 {
1218 LOG_ERROR("Cannot create an image"); 1224 ORTHANC_PLUGINS_LOG_ERROR("Cannot create an image");
1219 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1225 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1220 } 1226 }
1221 } 1227 }
1222 1228
1223 1229
1230 image_ = OrthancPluginCreateImageAccessor 1236 image_ = OrthancPluginCreateImageAccessor
1231 (GetGlobalContext(), format, width, height, pitch, buffer); 1237 (GetGlobalContext(), format, width, height, pitch, buffer);
1232 1238
1233 if (image_ == NULL) 1239 if (image_ == NULL)
1234 { 1240 {
1235 LOG_ERROR("Cannot create an image accessor"); 1241 ORTHANC_PLUGINS_LOG_ERROR("Cannot create an image accessor");
1236 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1242 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1237 } 1243 }
1238 } 1244 }
1239 1245
1240 void OrthancImage::UncompressPngImage(const void* data, 1246 void OrthancImage::UncompressPngImage(const void* data,
1244 1250
1245 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png); 1251 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png);
1246 1252
1247 if (image_ == NULL) 1253 if (image_ == NULL)
1248 { 1254 {
1249 LOG_ERROR("Cannot uncompress a PNG image"); 1255 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a PNG image");
1250 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1256 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1251 } 1257 }
1252 } 1258 }
1253 1259
1254 1260
1257 { 1263 {
1258 Clear(); 1264 Clear();
1259 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg); 1265 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg);
1260 if (image_ == NULL) 1266 if (image_ == NULL)
1261 { 1267 {
1262 LOG_ERROR("Cannot uncompress a JPEG image"); 1268 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a JPEG image");
1263 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1269 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1264 } 1270 }
1265 } 1271 }
1266 1272
1267 1273
1271 { 1277 {
1272 Clear(); 1278 Clear();
1273 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame); 1279 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame);
1274 if (image_ == NULL) 1280 if (image_ == NULL)
1275 { 1281 {
1276 LOG_ERROR("Cannot uncompress a DICOM image"); 1282 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a DICOM image");
1277 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1283 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1278 } 1284 }
1279 } 1285 }
1280 1286
1281 1287
1685 1691
1686 void ReportMinimalOrthancVersion(unsigned int major, 1692 void ReportMinimalOrthancVersion(unsigned int major,
1687 unsigned int minor, 1693 unsigned int minor,
1688 unsigned int revision) 1694 unsigned int revision)
1689 { 1695 {
1690 LOG_ERROR("Your version of the Orthanc core (" + 1696 ORTHANC_PLUGINS_LOG_ERROR("Your version of the Orthanc core (" +
1691 std::string(GetGlobalContext()->orthancVersion) + 1697 std::string(GetGlobalContext()->orthancVersion) +
1692 ") is too old to run this plugin (version " + 1698 ") is too old to run this plugin (version " +
1693 boost::lexical_cast<std::string>(major) + "." + 1699 boost::lexical_cast<std::string>(major) + "." +
1694 boost::lexical_cast<std::string>(minor) + "." + 1700 boost::lexical_cast<std::string>(minor) + "." +
1695 boost::lexical_cast<std::string>(revision) + 1701 boost::lexical_cast<std::string>(revision) +
1696 " is required)"); 1702 " is required)");
1697 } 1703 }
1698 1704
1699 bool CheckMinimalVersion(const char* version, 1705 bool CheckMinimalVersion(const char* version,
1700 unsigned int major, 1706 unsigned int major,
1701 unsigned int minor, 1707 unsigned int minor,
1715 1721
1716 // Parse the version 1722 // Parse the version
1717 int aa, bb, cc = 0; 1723 int aa, bb, cc = 0;
1718 if ((ORTHANC_SCANF(version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 && 1724 if ((ORTHANC_SCANF(version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 &&
1719 ORTHANC_SCANF(version, "%4d.%4d", &aa, &bb) != 2) || 1725 ORTHANC_SCANF(version, "%4d.%4d", &aa, &bb) != 2) ||
1720 aa < 0 || 1726 aa < 0 ||
1721 bb < 0 || 1727 bb < 0 ||
1722 cc < 0) 1728 cc < 0)
1723 { 1729 {
1724 return false; 1730 return false;
1725 } 1731 }
1726 1732
1727 unsigned int a = static_cast<unsigned int>(aa); 1733 unsigned int a = static_cast<unsigned int>(aa);
1771 unsigned int minor, 1777 unsigned int minor,
1772 unsigned int revision) 1778 unsigned int revision)
1773 { 1779 {
1774 if (!HasGlobalContext()) 1780 if (!HasGlobalContext())
1775 { 1781 {
1776 LOG_ERROR("Bad Orthanc context in the plugin"); 1782 ORTHANC_PLUGINS_LOG_ERROR("Bad Orthanc context in the plugin");
1777 return false; 1783 return false;
1778 } 1784 }
1779 1785
1780 return CheckMinimalVersion(GetGlobalContext()->orthancVersion, 1786 return CheckMinimalVersion(GetGlobalContext()->orthancVersion,
1781 major, minor, revision); 1787 major, minor, revision);
1808 { 1814 {
1809 return index; 1815 return index;
1810 } 1816 }
1811 else 1817 else
1812 { 1818 {
1813 LOG_ERROR("Inexistent peer: " + name); 1819 ORTHANC_PLUGINS_LOG_ERROR("Inexistent peer: " + name);
1814 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource); 1820 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource);
1815 } 1821 }
1816 } 1822 }
1817 1823
1818 1824
2092 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 2098 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
2093 } 2099 }
2094 2100
2095 if (body.size() > 0xffffffffu) 2101 if (body.size() > 0xffffffffu)
2096 { 2102 {
2097 LOG_ERROR("Cannot handle body size > 4GB"); 2103 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
2098 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 2104 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
2099 } 2105 }
2100 2106
2101 OrthancPlugins::MemoryBuffer answer; 2107 OrthancPlugins::MemoryBuffer answer;
2102 uint16_t status; 2108 uint16_t status;
2129 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 2135 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
2130 } 2136 }
2131 2137
2132 if (body.size() > 0xffffffffu) 2138 if (body.size() > 0xffffffffu)
2133 { 2139 {
2134 LOG_ERROR("Cannot handle body size > 4GB"); 2140 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
2135 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 2141 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
2136 } 2142 }
2137 2143
2138 OrthancPlugins::MemoryBuffer answer; 2144 OrthancPlugins::MemoryBuffer answer;
2139 uint16_t status; 2145 uint16_t status;
2495 2501
2496 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); 2502 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority);
2497 2503
2498 if (id == NULL) 2504 if (id == NULL)
2499 { 2505 {
2500 LOG_ERROR("Plugin cannot submit job"); 2506 ORTHANC_PLUGINS_LOG_ERROR("Plugin cannot submit job");
2501 OrthancPluginFreeJob(GetGlobalContext(), orthanc); 2507 OrthancPluginFreeJob(GetGlobalContext(), orthanc);
2502 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 2508 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
2503 } 2509 }
2504 else 2510 else
2505 { 2511 {
2564 { 2570 {
2565 #if HAS_ORTHANC_EXCEPTION == 1 2571 #if HAS_ORTHANC_EXCEPTION == 1
2566 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()), 2572 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()),
2567 status["ErrorDescription"].asString()); 2573 status["ErrorDescription"].asString());
2568 #else 2574 #else
2569 LOG_ERROR("Exception while executing the job: " + status["ErrorDescription"].asString()); 2575 ORTHANC_PLUGINS_LOG_ERROR("Exception while executing the job: " + status["ErrorDescription"].asString());
2570 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); 2576 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt());
2571 #endif 2577 #endif
2572 } 2578 }
2573 } 2579 }
2574 } 2580 }
2589 { 2595 {
2590 #if HAS_ORTHANC_EXCEPTION == 1 2596 #if HAS_ORTHANC_EXCEPTION == 1
2591 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2597 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2592 "Expected a JSON object in the body"); 2598 "Expected a JSON object in the body");
2593 #else 2599 #else
2594 LOG_ERROR("Expected a JSON object in the body"); 2600 ORTHANC_PLUGINS_LOG_ERROR("Expected a JSON object in the body");
2595 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2601 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2596 #endif 2602 #endif
2597 } 2603 }
2598 2604
2599 bool synchronous = true; 2605 bool synchronous = true;
2605 #if HAS_ORTHANC_EXCEPTION == 1 2611 #if HAS_ORTHANC_EXCEPTION == 1
2606 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2612 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2607 "Option \"" + std::string(KEY_SYNCHRONOUS) + 2613 "Option \"" + std::string(KEY_SYNCHRONOUS) +
2608 "\" must be Boolean"); 2614 "\" must be Boolean");
2609 #else 2615 #else
2610 LOG_ERROR("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean"); 2616 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean");
2611 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2617 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2612 #endif 2618 #endif
2613 } 2619 }
2614 else 2620 else
2615 { 2621 {
2624 #if HAS_ORTHANC_EXCEPTION == 1 2630 #if HAS_ORTHANC_EXCEPTION == 1
2625 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2631 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2626 "Option \"" + std::string(KEY_ASYNCHRONOUS) + 2632 "Option \"" + std::string(KEY_ASYNCHRONOUS) +
2627 "\" must be Boolean"); 2633 "\" must be Boolean");
2628 #else 2634 #else
2629 LOG_ERROR("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean"); 2635 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean");
2630 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2636 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2631 #endif 2637 #endif
2632 } 2638 }
2633 else 2639 else
2634 { 2640 {
2645 #if HAS_ORTHANC_EXCEPTION == 1 2651 #if HAS_ORTHANC_EXCEPTION == 1
2646 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2652 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2647 "Option \"" + std::string(KEY_PRIORITY) + 2653 "Option \"" + std::string(KEY_PRIORITY) +
2648 "\" must be an integer"); 2654 "\" must be an integer");
2649 #else 2655 #else
2650 LOG_ERROR("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer"); 2656 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer");
2651 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2657 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2652 #endif 2658 #endif
2653 } 2659 }
2654 else 2660 else
2655 { 2661 {
3166 3172
3167 MemoryBuffer answerBodyBuffer, answerHeadersBuffer; 3173 MemoryBuffer answerBodyBuffer, answerHeadersBuffer;
3168 3174
3169 if (body.size() > 0xffffffffu) 3175 if (body.size() > 0xffffffffu)
3170 { 3176 {
3171 LOG_ERROR("Cannot handle body size > 4GB"); 3177 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
3172 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 3178 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
3173 } 3179 }
3174 3180
3175 OrthancPluginErrorCode error = OrthancPluginHttpClient( 3181 OrthancPluginErrorCode error = OrthancPluginHttpClient(
3176 GetGlobalContext(), 3182 GetGlobalContext(),
3311 std::string body; 3317 std::string body;
3312 Execute(answerHeaders, body); 3318 Execute(answerHeaders, body);
3313 3319
3314 if (!ReadJson(answerBody, body)) 3320 if (!ReadJson(answerBody, body))
3315 { 3321 {
3316 LOG_ERROR("Cannot convert HTTP answer body to JSON"); 3322 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert HTTP answer body to JSON");
3317 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 3323 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
3318 } 3324 }
3319 } 3325 }
3320 3326
3321 3327