diff OrthancFramework/Sources/Logging.cpp @ 4273:0034f855c023

tuning log categories from command-line, and binary compat with orthanc framework 1.7.2
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Nov 2020 12:24:50 +0100
parents 1661544ea94d
children d7a50b7b8466
line wrap: on
line diff
--- a/OrthancFramework/Sources/Logging.cpp	Tue Nov 03 07:19:33 2020 +0100
+++ b/OrthancFramework/Sources/Logging.cpp	Tue Nov 03 12:24:50 2020 +0100
@@ -193,32 +193,68 @@
     }
 
 
-    LogCategory StringToCategory(const std::string& category)
+    bool LookupCategory(LogCategory& target,
+                        const std::string& category)
     {
       if (category == "generic")
       {
-        return LogCategory_GENERIC;
+        target = LogCategory_GENERIC;
+        return true;
       }
       else if (category == "plugins")
       {
-        return LogCategory_PLUGINS;
+        target = LogCategory_PLUGINS;
+        return true;
       }
       else if (category == "rest")
       {
-        return LogCategory_REST;
+        target = LogCategory_REST;
+        return true;
       }
       else if (category == "dicom")
       {
-        return LogCategory_DICOM;
+        target = LogCategory_DICOM;
+        return true;
       }
       else if (category == "sqlite")
       {
-        return LogCategory_SQLITE;
+        target = LogCategory_SQLITE;
+        return true;
       }
       else
       {
-        throw OrthancException(ErrorCode_ParameterOutOfRange,
-                               "Unknown log category: " + category);
+        return false;
+      }
+    }
+
+
+    size_t GetCategoriesCount()
+    {
+      return 5;
+    }
+
+
+    const char* GetCategoryName(size_t i)
+    {
+      switch (i)
+      {
+        case 0:
+          return "generic";
+          
+        case 1:
+          return "plugins";
+          
+        case 2:
+          return "rest";
+          
+        case 3:
+          return "dicom";
+          
+        case 4:
+          return "sqlite";
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
       }
     }
   }
@@ -709,20 +745,16 @@
     }
 
 
-    InternalLogger::InternalLogger(LogLevel level,
-                                   LogCategory category,
-                                   const char* file,
-                                   int line) : 
-      lock_(loggingStreamsMutex_, boost::defer_lock_t()),
-      level_(level),
-      stream_(&nullStream_)  // By default, logging to "/dev/null" is simulated
+    void InternalLogger::Setup(LogCategory category,
+                               const char* file,
+                               int line)
     {
       if (pluginContext_ != NULL)
       {
         // We are logging using the Orthanc plugin SDK
 
-        if (level == LogLevel_TRACE ||
-            !IsCategoryEnabled(level, category))
+        if (level_ == LogLevel_TRACE ||
+            !IsCategoryEnabled(level_, category))
         {
           // No trace level in plugins, directly exit as the stream is
           // set to "/dev/null"
@@ -738,7 +770,7 @@
       {
         // We are logging in a standalone application, not inside an Orthanc plugin
 
-        if (!IsCategoryEnabled(level, category))
+        if (!IsCategoryEnabled(level_, category))
         {
           // This logging level is disabled, directly exit as the
           // stream is set to "/dev/null"
@@ -746,7 +778,7 @@
         }
 
         std::string prefix;
-        GetLinePrefix(prefix, level, file, line);
+        GetLinePrefix(prefix, level_, file, line);
 
         {
           // We lock the global mutex. The mutex is locked until the
@@ -760,7 +792,7 @@
             return;
           }
 
-          switch (level)
+          switch (level_)
           {
             case LogLevel_ERROR:
               stream_ = loggingStreamsContext_->error_;
@@ -805,6 +837,29 @@
     }
 
 
+    InternalLogger::InternalLogger(LogLevel level,
+                                   LogCategory category,
+                                   const char* file,
+                                   int line) :
+      lock_(loggingStreamsMutex_, boost::defer_lock_t()),
+      level_(level),
+      stream_(&nullStream_)  // By default, logging to "/dev/null" is simulated
+    {
+      Setup(category, file, line);
+    }
+
+
+    InternalLogger::InternalLogger(LogLevel level,
+                                   const char* file,
+                                   int line) :
+      lock_(loggingStreamsMutex_, boost::defer_lock_t()),
+      level_(level),
+      stream_(&nullStream_)  // By default, logging to "/dev/null" is simulated
+    {
+      Setup(LogCategory_GENERIC, file, line);
+    }
+
+
     InternalLogger::~InternalLogger()
     {
       if (pluginStream_.get() != NULL)