changeset 3998:b3f09bc9734b

sharing more code between the loggers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Jun 2020 18:56:35 +0200
parents febe25d03f08
children 44e2a8e0b6ae
files Core/DicomNetworking/Internals/CommandDispatcher.cpp Core/Enumerations.cpp Core/Enumerations.h Core/Logging.cpp Core/Logging.h
diffstat 5 files changed, 132 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomNetworking/Internals/CommandDispatcher.cpp	Fri Jun 05 18:26:15 2020 +0200
+++ b/Core/DicomNetworking/Internals/CommandDispatcher.cpp	Fri Jun 05 18:56:35 2020 +0200
@@ -251,14 +251,14 @@
       OFCondition cond = ASC_dropSCPAssociation(assoc);
       if (cond.bad())
       {
-        LOG(FATAL) << cond.text();
+        LOG(ERROR) << cond.text();
         return cond;
       }
 
       cond = ASC_destroyAssociation(&assoc);
       if (cond.bad())
       {
-        LOG(FATAL) << cond.text();
+        LOG(ERROR) << cond.text();
         return cond;
       }
 
--- a/Core/Enumerations.cpp	Fri Jun 05 18:26:15 2020 +0200
+++ b/Core/Enumerations.cpp	Fri Jun 05 18:56:35 2020 +0200
@@ -755,28 +755,6 @@
   }
 
 
-  const char* EnumerationToString(LogLevel level)
-  {
-    switch (level)
-    {
-      case LogLevel_Error:
-        return "ERROR";
-
-      case LogLevel_Warning:
-        return "WARNING";
-
-      case LogLevel_Info:
-        return "INFO";
-
-      case LogLevel_Trace:
-        return "TRACE";
-
-      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-  }
-
-
   const char* EnumerationToString(PixelFormat format)
   {
     switch (format)
@@ -1349,31 +1327,6 @@
   }
 
 
-  LogLevel StringToLogLevel(const char *level)
-  {
-    if (strcmp(level, "ERROR") == 0)
-    {
-      return LogLevel_Error;
-    }
-    else if (strcmp(level, "WARNING") == 0)
-    {
-      return LogLevel_Warning;
-    }
-    else if (strcmp(level, "INFO") == 0)
-    {
-      return LogLevel_Info;
-    }
-    else if (strcmp(level, "TRACE") == 0)
-    {
-      return LogLevel_Trace;
-    }
-    else 
-    {
-      throw OrthancException(ErrorCode_InternalError);
-    }
-  }
-
-
   ValueRepresentation StringToValueRepresentation(const std::string& vr,
                                                   bool throwIfUnsupported)
   {
--- a/Core/Enumerations.h	Fri Jun 05 18:26:15 2020 +0200
+++ b/Core/Enumerations.h	Fri Jun 05 18:56:35 2020 +0200
@@ -296,14 +296,6 @@
     DicomTransferSyntax_XML    /*!< XML Encoding */
   };
 
-  enum LogLevel
-  {
-    LogLevel_Error,
-    LogLevel_Warning,
-    LogLevel_Info,
-    LogLevel_Trace
-  };
-
 
   /**
    * {summary}{The memory layout of the pixels (resp. voxels) of a 2D (resp. 3D) image.}
@@ -829,9 +821,6 @@
   const char* EnumerationToString(PhotometricInterpretation photometric);
 
   ORTHANC_PUBLIC
-  const char* EnumerationToString(LogLevel level);
-
-  ORTHANC_PUBLIC
   const char* EnumerationToString(RequestOrigin origin);
 
   ORTHANC_PUBLIC
@@ -874,9 +863,6 @@
   ImageFormat StringToImageFormat(const char* format);
 
   ORTHANC_PUBLIC
-  LogLevel StringToLogLevel(const char* level);
-
-  ORTHANC_PUBLIC
   ValueRepresentation StringToValueRepresentation(const std::string& vr,
                                                   bool throwIfUnsupported);
 
--- a/Core/Logging.cpp	Fri Jun 05 18:26:15 2020 +0200
+++ b/Core/Logging.cpp	Fri Jun 05 18:56:35 2020 +0200
@@ -34,6 +34,62 @@
 #include "PrecompiledHeaders.h"
 #include "Logging.h"
 
+#include "OrthancException.h"
+
+
+namespace Orthanc
+{
+  namespace Logging
+  {
+    const char* EnumerationToString(LogLevel level)
+    {
+      switch (level)
+      {
+        case LogLevel_ERROR:
+          return "ERROR";
+
+        case LogLevel_WARNING:
+          return "WARNING";
+
+        case LogLevel_INFO:
+          return "INFO";
+
+        case LogLevel_TRACE:
+          return "TRACE";
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
+    }
+
+
+    LogLevel StringToLogLevel(const char *level)
+    {
+      if (strcmp(level, "ERROR") == 0)
+      {
+        return LogLevel_ERROR;
+      }
+      else if (strcmp(level, "WARNING") == 0)
+      {
+        return LogLevel_WARNING;
+      }
+      else if (strcmp(level, "INFO") == 0)
+      {
+        return LogLevel_INFO;
+      }
+      else if (strcmp(level, "TRACE") == 0)
+      {
+        return LogLevel_TRACE;
+      }
+      else 
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+  }
+}
+
+
 #if ORTHANC_ENABLE_LOGGING != 1
 
 namespace Orthanc
@@ -167,7 +223,7 @@
       globalTraceLogFunc = traceLogFunc;
     }
 
-    InternalLogger::InternalLogger(Level level,
+    InternalLogger::InternalLogger(LogLevel level,
                                    const char* file  /* ignored */,
                                    int line  /* ignored */) :
       level_(level)
@@ -180,15 +236,15 @@
 
       switch (level_)
       {
-        case Level_ERROR:
+        case LogLevel_ERROR:
           globalErrorLogFunc(message.c_str());
           break;
 
-        case Level_WARNING:
+        case LogLevel_WARNING:
           globalWarningLogFunc(message.c_str());
           break;
 
-        case Level_INFO:
+        case LogLevel_INFO:
           if (globalVerbose_)
           {
             globalInfoLogFunc(message.c_str());
@@ -196,7 +252,7 @@
           }
           break;
 
-        case Level_TRACE:
+        case LogLevel_TRACE:
           if (globalTrace_)
           {
             globalTraceLogFunc(message.c_str());
@@ -310,7 +366,7 @@
       context_ = reinterpret_cast<OrthancPluginContext*>(context);
     }
 
-    InternalLogger::InternalLogger(Level level,
+    InternalLogger::InternalLogger(LogLevel level,
                                    const char* file  /* ignored */,
                                    int line  /* ignored */) :
       level_(level)
@@ -324,19 +380,19 @@
       {
         switch (level_)
         {
-          case Level_ERROR:
+          case LogLevel_ERROR:
             context_->InvokeService(context_, _OrthancPluginService_LogError, message.c_str());
             break;
 
-          case Level_WARNING:
+          case LogLevel_WARNING:
             context_->InvokeService(context_, _OrthancPluginService_LogWarning, message.c_str());
             break;
 
-          case Level_INFO:
+          case LogLevel_INFO:
             context_->InvokeService(context_, _OrthancPluginService_LogInfo, message.c_str());
             break;
 
-          case Level_TRACE:
+          case LogLevel_TRACE:
             // Not used by plugins
             break;
 
@@ -364,7 +420,6 @@
  *********************************************************/
 
 #include "Compatibility.h"
-#include "OrthancException.h"
 #include "Enumerations.h"
 #include "Toolbox.h"
 
@@ -678,7 +733,7 @@
     }
 
 
-    InternalLogger::InternalLogger(const char* level,
+    InternalLogger::InternalLogger(LogLevel level,
                                    const char* file,
                                    int line) : 
       lock_(loggingMutex_), 
@@ -692,10 +747,8 @@
 
       try
       {
-        LogLevel l = StringToLogLevel(level);
-      
-        if ((l == LogLevel_Info  && !loggingContext_->infoEnabled_) ||
-            (l == LogLevel_Trace && !loggingContext_->traceEnabled_))
+        if ((level == LogLevel_INFO  && !loggingContext_->infoEnabled_) ||
+            (level == LogLevel_TRACE && !loggingContext_->traceEnabled_))
         {
           // This logging level is disabled, directly exit and unlock
           // the mutex to speed-up things. The stream is set to "/dev/null"
@@ -734,9 +787,32 @@
              In this implementation, "threadid" is not printed.
           **/
 
+          char prefix;
+          switch (level)
+          {
+            case LogLevel_ERROR:
+              prefix = 'E';
+              break;
+
+            case LogLevel_WARNING:
+              prefix = 'W';
+              break;
+
+            case LogLevel_INFO:
+              prefix = 'I';
+              break;
+
+            case LogLevel_TRACE:
+              prefix = 'T';
+              break;
+
+            default:
+              throw OrthancException(ErrorCode_InternalError);            
+          }
+
           char date[64];
           sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ",
-                  level[0],
+                  prefix,
                   now.date().month().as_number(),
                   now.date().day().as_number(),
                   static_cast<int>(duration.hours()),
@@ -760,17 +836,17 @@
           return;
         }
 
-        switch (l)
+        switch (level)
         {
-          case LogLevel_Error:
+          case LogLevel_ERROR:
             stream_ = loggingContext_->error_;
             break;
 
-          case LogLevel_Warning:
+          case LogLevel_WARNING:
             stream_ = loggingContext_->warning_;
             break;
 
-          case LogLevel_Info:
+          case LogLevel_INFO:
             if (loggingContext_->infoEnabled_)
             {
               stream_ = loggingContext_->info_;
@@ -778,7 +854,7 @@
 
             break;
 
-          case LogLevel_Trace:
+          case LogLevel_TRACE:
             if (loggingContext_->traceEnabled_)
             {
               stream_ = loggingContext_->info_;
--- a/Core/Logging.h	Fri Jun 05 18:26:15 2020 +0200
+++ b/Core/Logging.h	Fri Jun 05 18:56:35 2020 +0200
@@ -64,14 +64,18 @@
 {
   namespace Logging
   {
-    enum Level
+    enum LogLevel
     {
-      Level_ERROR,
-      Level_WARNING,
-      Level_INFO,
-      Level_TRACE
+      LogLevel_ERROR,
+      LogLevel_WARNING,
+      LogLevel_INFO,
+      LogLevel_TRACE
     };
     
+    ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level);
+
+    ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level);
+
 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
     // "pluginContext" must be of type "OrthancPluginContext"
     ORTHANC_PUBLIC void Initialize(void* pluginContext);
@@ -122,19 +126,23 @@
   }
 }
 
+
 #if ORTHANC_ENABLE_LOGGING != 1
-
 #  define LOG(level)   ::Orthanc::Logging::NullStream()
 #  define VLOG(level)  ::Orthanc::Logging::NullStream()
-
-#elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 ||    \
-       ORTHANC_ENABLE_LOGGING_STDIO == 1)
+#else
+#  define LOG(level)  ::Orthanc::Logging::InternalLogger        \
+  (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__)
+#  define VLOG(level) ::Orthanc::Logging::InternalLogger        \
+  (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__)
+#endif
 
-#  include <boost/noncopyable.hpp>
-#  define LOG(level)  ::Orthanc::Logging::InternalLogger                \
-  (::Orthanc::Logging::Level_ ## level, __FILE__, __LINE__)
-#  define VLOG(level) ::Orthanc::Logging::InternalLogger        \
-  (::Orthanc::Logging::Level_TRACE, __FILE__, __LINE__)
+
+#if (ORTHANC_ENABLE_LOGGING == 1 &&             \
+     (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 ||     \
+      ORTHANC_ENABLE_LOGGING_STDIO == 1))
+
+#include <boost/noncopyable.hpp>
 
 namespace Orthanc
 {
@@ -143,11 +151,11 @@
     class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable
     {
     private:
-      Level       level_;
+      LogLevel       level_;
       std::stringstream   messageStream_;
 
     public:
-      InternalLogger(Level level,
+      InternalLogger(LogLevel level,
                      const char* file,
                      int line);
 
@@ -163,17 +171,16 @@
   }
 }
 
+#endif
+
 
 
 
-#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && 
-          ORTHANC_ENABLE_LOGGING_STDIO == 0 && 
-          ORTHANC_ENABLE_LOGGING == 1 */
+#if (ORTHANC_ENABLE_LOGGING == 1 &&             \
+     ORTHANC_ENABLE_LOGGING_PLUGIN == 0 &&      \
+     ORTHANC_ENABLE_LOGGING_STDIO == 0)
 
-#  include <boost/thread/mutex.hpp>
-#  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)
-#  define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__)
-
+#include <boost/thread/mutex.hpp>
 
 namespace Orthanc
 {
@@ -187,7 +194,7 @@
       std::ostream*             stream_;
 
     public:
-      InternalLogger(const char* level,
+      InternalLogger(LogLevel level,
                      const char* file,
                      int line);
 
@@ -266,4 +273,4 @@
   }
 }
 
-#endif  // ORTHANC_ENABLE_LOGGING
+#endif