comparison OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp @ 4275:d7a50b7b8466

Dynamically access and/or change the verbosity of logging categories with the REST API
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Nov 2020 14:41:27 +0100
parents 0034f855c023
children 9e2fc6911ac8
comparison
equal deleted inserted replaced
4274:09ed936fd381 4275:d7a50b7b8466
40 #include "../../Plugins/Engine/PluginsManager.h" 40 #include "../../Plugins/Engine/PluginsManager.h"
41 #include "../OrthancConfiguration.h" 41 #include "../OrthancConfiguration.h"
42 #include "../OrthancInitialization.h" 42 #include "../OrthancInitialization.h"
43 #include "../ServerContext.h" 43 #include "../ServerContext.h"
44 44
45 #include <boost/algorithm/string/predicate.hpp>
46
45 47
46 namespace Orthanc 48 namespace Orthanc
47 { 49 {
48 // System information ------------------------------------------------------- 50 // System information -------------------------------------------------------
49 51
505 507
506 SetGlobalVerbosity(StringToVerbosity(body)); 508 SetGlobalVerbosity(StringToVerbosity(body));
507 509
508 // Success 510 // Success
509 LOG(WARNING) << "REST API call has switched the log level to: " << body; 511 LOG(WARNING) << "REST API call has switched the log level to: " << body;
512 call.GetOutput().AnswerBuffer("", MimeType_PlainText);
513 }
514
515
516 static Logging::LogCategory GetCategory(const RestApiCall& call)
517 {
518 static const std::string PREFIX = "log-level-";
519
520 if (call.GetFullUri().size() == 2 &&
521 call.GetFullUri() [0] == "tools" &&
522 boost::starts_with(call.GetFullUri() [1], PREFIX))
523 {
524 Logging::LogCategory category;
525 if (Logging::LookupCategory(category, call.GetFullUri() [1].substr(PREFIX.size())))
526 {
527 return category;
528 }
529 }
530
531 throw OrthancException(ErrorCode_InternalError);
532 }
533
534
535 static void GetLogLevelCategory(RestApiGetCall& call)
536 {
537 const std::string s = EnumerationToString(GetCategoryVerbosity(GetCategory(call)));
538 call.GetOutput().AnswerBuffer(s, MimeType_PlainText);
539 }
540
541
542 static void PutLogLevelCategory(RestApiPutCall& call)
543 {
544 std::string body;
545 call.BodyToString(body);
546
547 Verbosity verbosity = StringToVerbosity(body);
548 Logging::LogCategory category = GetCategory(call);
549 SetCategoryVerbosity(category, verbosity);
550
551 // Success
552 LOG(WARNING) << "REST API call has switched the log level of category \""
553 << Logging::GetCategoryName(category) << "\" to \""
554 << EnumerationToString(verbosity) << "\"";
510 call.GetOutput().AnswerBuffer("", MimeType_PlainText); 555 call.GetOutput().AnswerBuffer("", MimeType_PlainText);
511 } 556 }
512 557
513 558
514 void OrthancRestApi::RegisterSystem() 559 void OrthancRestApi::RegisterSystem()
527 Register("/tools/metrics", PutMetricsEnabled); 572 Register("/tools/metrics", PutMetricsEnabled);
528 Register("/tools/metrics-prometheus", GetMetricsPrometheus); 573 Register("/tools/metrics-prometheus", GetMetricsPrometheus);
529 Register("/tools/log-level", GetLogLevel); 574 Register("/tools/log-level", GetLogLevel);
530 Register("/tools/log-level", PutLogLevel); 575 Register("/tools/log-level", PutLogLevel);
531 576
577 for (size_t i = 0; i < Logging::GetCategoriesCount(); i++)
578 {
579 const std::string name(Logging::GetCategoryName(i));
580 Register("/tools/log-level-" + name, GetLogLevelCategory);
581 Register("/tools/log-level-" + name, PutLogLevelCategory);
582 }
583
532 Register("/plugins", ListPlugins); 584 Register("/plugins", ListPlugins);
533 Register("/plugins/{id}", GetPlugin); 585 Register("/plugins/{id}", GetPlugin);
534 Register("/plugins/explorer.js", GetOrthancExplorerPlugins); 586 Register("/plugins/explorer.js", GetOrthancExplorerPlugins);
535 587
536 Register("/jobs", ListJobs); 588 Register("/jobs", ListJobs);