comparison OrthancServer/Plugins/Samples/ServeFolders/Plugin.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 6ce05f8b5b13
children e02cdf358905
comparison
equal deleted inserted replaced
5560:c80dbbae3f60 5561:0b18690c1935
21 21
22 22
23 #define SERVE_FOLDERS_NAME "serve-folders" 23 #define SERVE_FOLDERS_NAME "serve-folders"
24 24
25 #include "../Common/OrthancPluginCppWrapper.h" 25 #include "../Common/OrthancPluginCppWrapper.h"
26 #include "../../../OrthancFramework/Sources/Logging.h"
26 27
27 #include <json/value.h> 28 #include <json/value.h>
28 #include <boost/filesystem.hpp> 29 #include <boost/filesystem.hpp>
29 #include <boost/date_time/posix_time/posix_time.hpp> 30 #include <boost/date_time/posix_time/posix_time.hpp>
30 31
92 { 93 {
93 return found->second; 94 return found->second;
94 } 95 }
95 else 96 else
96 { 97 {
97 OrthancPlugins::LogWarning("ServeFolders: Unknown MIME type for extension \"" + extension + "\""); 98 LOG(WARNING) << "ServeFolders: Unknown MIME type for extension \"" << extension << "\"";
98 return "application/octet-stream"; 99 return "application/octet-stream";
99 } 100 }
100 } 101 }
101 102
102 103
107 const std::string uri = request->groups[0]; 108 const std::string uri = request->groups[0];
108 109
109 std::map<std::string, std::string>::const_iterator found = folders_.find(uri); 110 std::map<std::string, std::string>::const_iterator found = folders_.find(uri);
110 if (found == folders_.end()) 111 if (found == folders_.end())
111 { 112 {
112 OrthancPlugins::LogError("Unknown URI in plugin server-folders: " + uri); 113 LOG(ERROR) << "Unknown URI in plugin server-folders: " << uri;
113 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); 114 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404);
114 return false; 115 return false;
115 } 116 }
116 else 117 else
117 { 118 {
263 264
264 static void ConfigureFolders(const Json::Value& folders) 265 static void ConfigureFolders(const Json::Value& folders)
265 { 266 {
266 if (folders.type() != Json::objectValue) 267 if (folders.type() != Json::objectValue)
267 { 268 {
268 OrthancPlugins::LogError("The list of folders to be served is badly formatted (must be a JSON object)"); 269 LOG(ERROR) << "The list of folders to be served is badly formatted (must be a JSON object)";
269 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 270 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
270 } 271 }
271 272
272 Json::Value::Members members = folders.getMemberNames(); 273 Json::Value::Members members = folders.getMemberNames();
273 274
275 for (Json::Value::Members::const_iterator 276 for (Json::Value::Members::const_iterator
276 it = members.begin(); it != members.end(); ++it) 277 it = members.begin(); it != members.end(); ++it)
277 { 278 {
278 if (folders[*it].type() != Json::stringValue) 279 if (folders[*it].type() != Json::stringValue)
279 { 280 {
280 OrthancPlugins::LogError("The folder to be served \"" + *it + 281 LOG(ERROR) << "The folder to be served \"" << *it <<
281 "\" must be associated with a string value (its mapped URI)"); 282 "\" must be associated with a string value (its mapped URI)";
282 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 283 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
283 } 284 }
284 285
285 std::string baseUri = *it; 286 std::string baseUri = *it;
286 287
297 baseUri.resize(baseUri.size() - 1); 298 baseUri.resize(baseUri.size() - 1);
298 } 299 }
299 300
300 if (baseUri.empty()) 301 if (baseUri.empty())
301 { 302 {
302 OrthancPlugins::LogError("The URI of a folder to be served cannot be empty"); 303 LOG(ERROR) << "The URI of a folder to be served cannot be empty";
303 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 304 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
304 } 305 }
305 306
306 // Check whether the source folder exists and is indeed a directory 307 // Check whether the source folder exists and is indeed a directory
307 const std::string folder = folders[*it].asString(); 308 const std::string folder = folders[*it].asString();
308 if (!boost::filesystem::is_directory(folder)) 309 if (!boost::filesystem::is_directory(folder))
309 { 310 {
310 OrthancPlugins::LogError("Trying to serve an inexistent folder: " + folder); 311 LOG(ERROR) << "Trying to serve an inexistent folder: " + folder;
311 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); 312 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
312 } 313 }
313 314
314 folders_[baseUri] = folder; 315 folders_[baseUri] = folder;
315 316
324 325
325 static void ConfigureExtensions(const Json::Value& extensions) 326 static void ConfigureExtensions(const Json::Value& extensions)
326 { 327 {
327 if (extensions.type() != Json::objectValue) 328 if (extensions.type() != Json::objectValue)
328 { 329 {
329 OrthancPlugins::LogError("The list of extensions is badly formatted (must be a JSON object)"); 330 LOG(ERROR) << "The list of extensions is badly formatted (must be a JSON object)";
330 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 331 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
331 } 332 }
332 333
333 Json::Value::Members members = extensions.getMemberNames(); 334 Json::Value::Members members = extensions.getMemberNames();
334 335
335 for (Json::Value::Members::const_iterator 336 for (Json::Value::Members::const_iterator
336 it = members.begin(); it != members.end(); ++it) 337 it = members.begin(); it != members.end(); ++it)
337 { 338 {
338 if (extensions[*it].type() != Json::stringValue) 339 if (extensions[*it].type() != Json::stringValue)
339 { 340 {
340 OrthancPlugins::LogError("The file extension \"" + *it + 341 LOG(ERROR) << "The file extension \"" << *it <<
341 "\" must be associated with a string value (its MIME type)"); 342 "\" must be associated with a string value (its MIME type)";
342 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 343 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
343 } 344 }
344 345
345 const std::string& mime = extensions[*it].asString(); 346 const std::string& mime = extensions[*it].asString();
346 347
354 355
355 extensions_[name] = mime; 356 extensions_[name] = mime;
356 357
357 if (mime.empty()) 358 if (mime.empty())
358 { 359 {
359 OrthancPlugins::LogWarning("ServeFolders: Removing MIME type for file extension \"." + 360 LOG(WARNING) << "ServeFolders: Removing MIME type for file extension \"." << name << "\"";
360 name + "\"");
361 } 361 }
362 else 362 else
363 { 363 {
364 OrthancPlugins::LogWarning("ServeFolders: Associating file extension \"." + name + 364 LOG(WARNING) << "ServeFolders: Associating file extension \"." << name << "\" with MIME type \"" << mime << "\"";
365 "\" with MIME type \"" + mime + "\"");
366 } 365 }
367 } 366 }
368 } 367 }
369 368
370 369
390 bool tmp; 389 bool tmp;
391 390
392 if (configuration.LookupBooleanValue(tmp, "AllowCache")) 391 if (configuration.LookupBooleanValue(tmp, "AllowCache"))
393 { 392 {
394 allowCache_ = tmp; 393 allowCache_ = tmp;
395 OrthancPlugins::LogWarning("ServeFolders: Requesting the HTTP client to " + 394 LOG(WARNING) << "ServeFolders: Requesting the HTTP client to " << (tmp ? "enable" : "disable") << " its caching mechanism";
396 std::string(tmp ? "enable" : "disable") +
397 " its caching mechanism");
398 } 395 }
399 396
400 if (configuration.LookupBooleanValue(tmp, "GenerateETag")) 397 if (configuration.LookupBooleanValue(tmp, "GenerateETag"))
401 { 398 {
402 generateETag_ = tmp; 399 generateETag_ = tmp;
403 OrthancPlugins::LogWarning("ServeFolders: The computation of an ETag for the " 400 LOG(WARNING) << "ServeFolders: The computation of an ETag for the served resources is " << (tmp ? "enabled" : "disabled");
404 "served resources is " +
405 std::string(tmp ? "enabled" : "disabled"));
406 } 401 }
407 402
408 OrthancPlugins::OrthancConfiguration extensions; 403 OrthancPlugins::OrthancConfiguration extensions;
409 configuration.GetSection(extensions, "Extensions"); 404 configuration.GetSection(extensions, "Extensions");
410 ConfigureExtensions(extensions.GetJson()); 405 ConfigureExtensions(extensions.GetJson());
411 } 406 }
412 407
413 if (folders_.empty()) 408 if (folders_.empty())
414 { 409 {
415 OrthancPlugins::LogWarning("ServeFolders: Empty configuration file: " 410 LOG(WARNING) << "ServeFolders: Empty configuration file: No additional folder will be served!";
416 "No additional folder will be served!");
417 } 411 }
418 } 412 }
419 413
420 414
421 extern "C" 415 extern "C"
442 { 436 {
443 ReadConfiguration(); 437 ReadConfiguration();
444 } 438 }
445 catch (OrthancPlugins::PluginException& e) 439 catch (OrthancPlugins::PluginException& e)
446 { 440 {
447 OrthancPlugins::LogError("Error while initializing the ServeFolders plugin: " + 441 LOG(ERROR) << "Error while initializing the ServeFolders plugin: " << e.What(context);
448 std::string(e.What(context)));
449 } 442 }
450 443
451 return 0; 444 return 0;
452 } 445 }
453 446