comparison OrthancServer/OrthancRestApi/OrthancRestSystem.cpp @ 3672:ea8c1c0e81eb

Fix issue #65 (Logging improvements)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Feb 2020 17:22:28 +0100
parents 94f4a18a79cc
children e3b3af80732d
comparison
equal deleted inserted replaced
3671:3c4269229566 3672:ea8c1c0e81eb
40 #include "../../Plugins/Engine/PluginsManager.h" 40 #include "../../Plugins/Engine/PluginsManager.h"
41 #include "../OrthancConfiguration.h" 41 #include "../OrthancConfiguration.h"
42 #include "../ServerContext.h" 42 #include "../ServerContext.h"
43 43
44 44
45 static const char* LOG_LEVEL_DEFAULT = "default";
46 static const char* LOG_LEVEL_VERBOSE = "verbose";
47 static const char* LOG_LEVEL_TRACE = "trace";
48
49
45 namespace Orthanc 50 namespace Orthanc
46 { 51 {
47 // System information ------------------------------------------------------- 52 // System information -------------------------------------------------------
48 53
49 static void ServeRoot(RestApiGetCall& call) 54 static void ServeRoot(RestApiGetCall& call)
488 OrthancRestApi::GetContext(call).GetMetricsRegistry().SetEnabled(enabled); 493 OrthancRestApi::GetContext(call).GetMetricsRegistry().SetEnabled(enabled);
489 call.GetOutput().AnswerBuffer("", MimeType_PlainText); 494 call.GetOutput().AnswerBuffer("", MimeType_PlainText);
490 } 495 }
491 496
492 497
498 static void GetLogLevel(RestApiGetCall& call)
499 {
500 std::string s;
501
502 if (Logging::IsTraceLevelEnabled())
503 {
504 s = LOG_LEVEL_TRACE;
505 }
506 else if (Logging::IsInfoLevelEnabled())
507 {
508 s = LOG_LEVEL_VERBOSE;
509 }
510 else
511 {
512 s = LOG_LEVEL_DEFAULT;
513 }
514
515 call.GetOutput().AnswerBuffer(s, MimeType_PlainText);
516 }
517
518
519 static void PutLogLevel(RestApiPutCall& call)
520 {
521 std::string body;
522 call.BodyToString(body);
523
524 if (body == LOG_LEVEL_DEFAULT)
525 {
526 Logging::EnableInfoLevel(false);
527 Logging::EnableTraceLevel(false);
528 }
529 else if (body == LOG_LEVEL_VERBOSE)
530 {
531 Logging::EnableInfoLevel(true);
532 Logging::EnableTraceLevel(false);
533 }
534 else if (body == LOG_LEVEL_TRACE)
535 {
536 Logging::EnableInfoLevel(true);
537 Logging::EnableTraceLevel(true);
538 }
539 else
540 {
541 throw OrthancException(ErrorCode_ParameterOutOfRange,
542 "The log level must be one of the following values: \"" +
543 std::string(LOG_LEVEL_DEFAULT) + "\", \"" +
544 std::string(LOG_LEVEL_VERBOSE) + "\", of \"" +
545 std::string(LOG_LEVEL_TRACE) + "\"");
546 }
547
548 // Success
549 LOG(WARNING) << "REST API call has switched the log level to: " << body;
550 call.GetOutput().AnswerBuffer("", MimeType_PlainText);
551 }
552
553
493 void OrthancRestApi::RegisterSystem() 554 void OrthancRestApi::RegisterSystem()
494 { 555 {
495 Register("/", ServeRoot); 556 Register("/", ServeRoot);
496 Register("/system", GetSystemInformation); 557 Register("/system", GetSystemInformation);
497 Register("/statistics", GetStatistics); 558 Register("/statistics", GetStatistics);
503 Register("/tools/default-encoding", GetDefaultEncoding); 564 Register("/tools/default-encoding", GetDefaultEncoding);
504 Register("/tools/default-encoding", SetDefaultEncoding); 565 Register("/tools/default-encoding", SetDefaultEncoding);
505 Register("/tools/metrics", GetMetricsEnabled); 566 Register("/tools/metrics", GetMetricsEnabled);
506 Register("/tools/metrics", PutMetricsEnabled); 567 Register("/tools/metrics", PutMetricsEnabled);
507 Register("/tools/metrics-prometheus", GetMetricsPrometheus); 568 Register("/tools/metrics-prometheus", GetMetricsPrometheus);
569 Register("/tools/log-level", GetLogLevel);
570 Register("/tools/log-level", PutLogLevel);
508 571
509 Register("/plugins", ListPlugins); 572 Register("/plugins", ListPlugins);
510 Register("/plugins/{id}", GetPlugin); 573 Register("/plugins/{id}", GetPlugin);
511 Register("/plugins/explorer.js", GetOrthancExplorerPlugins); 574 Register("/plugins/explorer.js", GetOrthancExplorerPlugins);
512 575