diff 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
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Fri Feb 14 14:56:06 2020 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Fri Feb 14 17:22:28 2020 +0100
@@ -42,6 +42,11 @@
 #include "../ServerContext.h"
 
 
+static const char* LOG_LEVEL_DEFAULT = "default";
+static const char* LOG_LEVEL_VERBOSE = "verbose";
+static const char* LOG_LEVEL_TRACE = "trace";
+
+
 namespace Orthanc
 {
   // System information -------------------------------------------------------
@@ -490,6 +495,62 @@
   }
 
 
+  static void GetLogLevel(RestApiGetCall& call)
+  {
+    std::string s;
+    
+    if (Logging::IsTraceLevelEnabled())
+    {
+      s = LOG_LEVEL_TRACE;
+    }
+    else if (Logging::IsInfoLevelEnabled())
+    {
+      s = LOG_LEVEL_VERBOSE;
+    }
+    else
+    {
+      s = LOG_LEVEL_DEFAULT;
+    }
+    
+    call.GetOutput().AnswerBuffer(s, MimeType_PlainText);
+  }
+
+
+  static void PutLogLevel(RestApiPutCall& call)
+  {
+    std::string body;
+    call.BodyToString(body);
+
+    if (body == LOG_LEVEL_DEFAULT)
+    {
+      Logging::EnableInfoLevel(false);
+      Logging::EnableTraceLevel(false);
+    }
+    else if (body == LOG_LEVEL_VERBOSE)
+    {
+      Logging::EnableInfoLevel(true);
+      Logging::EnableTraceLevel(false);
+    }
+    else if (body == LOG_LEVEL_TRACE)
+    {
+      Logging::EnableInfoLevel(true);
+      Logging::EnableTraceLevel(true);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange,
+                             "The log level must be one of the following values: \"" +
+                             std::string(LOG_LEVEL_DEFAULT) + "\", \"" +
+                             std::string(LOG_LEVEL_VERBOSE) + "\", of \"" +
+                             std::string(LOG_LEVEL_TRACE) + "\"");
+    }
+
+    // Success
+    LOG(WARNING) << "REST API call has switched the log level to: " << body;
+    call.GetOutput().AnswerBuffer("", MimeType_PlainText);
+  }
+
+
   void OrthancRestApi::RegisterSystem()
   {
     Register("/", ServeRoot);
@@ -505,6 +566,8 @@
     Register("/tools/metrics", GetMetricsEnabled);
     Register("/tools/metrics", PutMetricsEnabled);
     Register("/tools/metrics-prometheus", GetMetricsPrometheus);
+    Register("/tools/log-level", GetLogLevel);
+    Register("/tools/log-level", PutLogLevel);
 
     Register("/plugins", ListPlugins);
     Register("/plugins/{id}", GetPlugin);