changeset 3997:febe25d03f08

merge
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Jun 2020 18:26:15 +0200
parents c2b9a7a1c74a (diff) aba6b6b490ea (current diff)
children b3f09bc9734b
files
diffstat 4 files changed, 172 insertions(+), 117 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Jun 05 17:20:56 2020 +0200
+++ b/CMakeLists.txt	Fri Jun 05 18:26:15 2020 +0200
@@ -251,6 +251,7 @@
 
 
 add_definitions(
+  -DORTHANC_FRAMEWORK_BUILDING_PLUGIN=0
   -DORTHANC_BUILD_UNIT_TESTS=1
   -DORTHANC_ENABLE_LOGGING_PLUGIN=0
   
--- a/Core/Logging.cpp	Fri Jun 05 17:20:56 2020 +0200
+++ b/Core/Logging.cpp	Fri Jun 05 18:26:15 2020 +0200
@@ -85,70 +85,6 @@
 }
 
 
-#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
-
-/*********************************************************
- * Logger compatible with the Orthanc plugin SDK
- *********************************************************/
-
-#include <boost/lexical_cast.hpp>
-#include <sstream>
-
-namespace Orthanc
-{
-  namespace Logging
-  {
-    static OrthancPluginContext* context_ = NULL;
-
-    void Initialize(OrthancPluginContext* context)
-    {
-      context_ = context;
-    }
-
-    InternalLogger::InternalLogger(InternalLevel level,
-                                   const char* file  /* ignored */,
-                                   int line  /* ignored */) :
-      level_(level)
-    {
-    }
-
-    InternalLogger::~InternalLogger()
-    {
-      std::string message = messageStream_.str();
-      if (context_ != NULL)
-      {
-        switch (level_)
-        {
-          case InternalLevel_ERROR:
-            OrthancPluginLogError(context_, message.c_str());
-            break;
-
-          case InternalLevel_WARNING:
-            OrthancPluginLogWarning(context_, message.c_str());
-            break;
-
-          case InternalLevel_INFO:
-            OrthancPluginLogInfo(context_, message.c_str());
-            break;
-
-          case InternalLevel_TRACE:
-            // Not used by plugins
-            break;
-
-          default:
-          {
-            std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) +
-                             ") for message: " + message);
-            OrthancPluginLogError(context_, s.c_str());
-            break;
-          }
-        }
-      }
-    }
-  }
-}
-
-
 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1
 
 /*********************************************************
@@ -172,43 +108,43 @@
     static bool globalTrace_ = false;
     
 #ifdef __EMSCRIPTEN__
-    void defaultErrorLogFunc(const char* msg)
+    static void defaultErrorLogFunc(const char* msg)
     {
       emscripten_console_error(msg);
     }
 
-    void defaultWarningLogFunc(const char* msg)
+    static void defaultWarningLogFunc(const char* msg)
     {
       emscripten_console_warn(msg);
     }
 
-    void defaultInfoLogFunc(const char* msg)
+    static void defaultInfoLogFunc(const char* msg)
     {
       emscripten_console_log(msg);
     }
 
-    void defaultTraceLogFunc(const char* msg)
+    static void defaultTraceLogFunc(const char* msg)
     {
       emscripten_console_log(msg);
     }
 #else
 // __EMSCRIPTEN__ not #defined
-    void defaultErrorLogFunc(const char* msg)
+    static void defaultErrorLogFunc(const char* msg)
     {
       fprintf(stderr, "E: %s\n", msg);
     }
 
-    void defaultWarningLogFunc(const char*)
+    static void defaultWarningLogFunc(const char*)
     {
       fprintf(stdout, "W: %s\n", msg);
     }
 
-    void defaultInfoLogFunc(const char*)
+    static void defaultInfoLogFunc(const char*)
     {
       fprintf(stdout, "I: %s\n", msg);
     }
 
-    void defaultTraceLogFunc(const char*)
+    static void defaultTraceLogFunc(const char*)
     {
       fprintf(stdout, "T: %s\n", msg);
     }
@@ -220,11 +156,10 @@
     static LoggingFunction globalInfoLogFunc = defaultInfoLogFunc;
     static LoggingFunction globalTraceLogFunc = defaultTraceLogFunc;
 
-    void SetErrorWarnInfoTraceLoggingFunctions(
-      LoggingFunction errorLogFunc,
-      LoggingFunction warningLogfunc,
-      LoggingFunction infoLogFunc,
-      LoggingFunction traceLogFunc)
+    void SetErrorWarnInfoTraceLoggingFunctions(LoggingFunction errorLogFunc,
+                                               LoggingFunction warningLogfunc,
+                                               LoggingFunction infoLogFunc,
+                                               LoggingFunction traceLogFunc)
     {
       globalErrorLogFunc = errorLogFunc;
       globalWarningLogFunc = warningLogfunc;
@@ -232,7 +167,7 @@
       globalTraceLogFunc = traceLogFunc;
     }
 
-    InternalLogger::InternalLogger(InternalLevel level,
+    InternalLogger::InternalLogger(Level level,
                                    const char* file  /* ignored */,
                                    int line  /* ignored */) :
       level_(level)
@@ -245,15 +180,15 @@
 
       switch (level_)
       {
-        case InternalLevel_ERROR:
+        case Level_ERROR:
           globalErrorLogFunc(message.c_str());
           break;
 
-        case InternalLevel_WARNING:
+        case Level_WARNING:
           globalWarningLogFunc(message.c_str());
           break;
 
-        case InternalLevel_INFO:
+        case Level_INFO:
           if (globalVerbose_)
           {
             globalInfoLogFunc(message.c_str());
@@ -261,7 +196,7 @@
           }
           break;
 
-        case InternalLevel_TRACE:
+        case Level_TRACE:
           if (globalTrace_)
           {
             globalTraceLogFunc(message.c_str());
@@ -282,6 +217,18 @@
     {
     }
 
+    void Finalize()
+    {
+    }
+
+    void Reset()
+    {
+    }
+
+    void Flush()
+    {
+    }
+
     void EnableInfoLevel(bool enabled)
     {
       globalVerbose_ = enabled;
@@ -302,6 +249,107 @@
       return globalTrace_;
     }
 
+    void SetTargetFile(const std::string& path)
+    {
+    }
+
+    void SetTargetFolder(const std::string& path)
+    {
+    }
+  }
+}
+
+
+#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+
+/*********************************************************
+ * Logger compatible with the Orthanc plugin SDK
+ *********************************************************/
+
+#include <cassert>
+
+namespace
+{
+  /**
+   * This is minimal implementation of the context for an Orthanc
+   * plugin, limited to the logging facilities, and that is binary
+   * compatible with the definitions of "OrthancCPlugin.h"
+   **/
+  typedef enum 
+  {
+    _OrthancPluginService_LogInfo = 1,
+    _OrthancPluginService_LogWarning = 2,
+    _OrthancPluginService_LogError = 3,
+    _OrthancPluginService_INTERNAL = 0x7fffffff
+  } _OrthancPluginService;
+
+  typedef struct _OrthancPluginContext_t
+  {
+    void*          pluginsManager;
+    const char*    orthancVersion;
+    void         (*Free) (void* buffer);
+    int32_t      (*InvokeService) (struct _OrthancPluginContext_t* context,
+                                   _OrthancPluginService service,
+                                   const void* params);
+  } OrthancPluginContext;
+}
+  
+
+#include <boost/lexical_cast.hpp>
+#include <sstream>
+
+namespace Orthanc
+{
+  namespace Logging
+  {
+    static OrthancPluginContext* context_ = NULL;
+
+    void Initialize(void* context)
+    {
+      assert(sizeof(_OrthancPluginService) == sizeof(int32_t));
+      context_ = reinterpret_cast<OrthancPluginContext*>(context);
+    }
+
+    InternalLogger::InternalLogger(Level level,
+                                   const char* file  /* ignored */,
+                                   int line  /* ignored */) :
+      level_(level)
+    {
+    }
+
+    InternalLogger::~InternalLogger()
+    {
+      std::string message = messageStream_.str();
+      if (context_ != NULL)
+      {
+        switch (level_)
+        {
+          case Level_ERROR:
+            context_->InvokeService(context_, _OrthancPluginService_LogError, message.c_str());
+            break;
+
+          case Level_WARNING:
+            context_->InvokeService(context_, _OrthancPluginService_LogWarning, message.c_str());
+            break;
+
+          case Level_INFO:
+            context_->InvokeService(context_, _OrthancPluginService_LogInfo, message.c_str());
+            break;
+
+          case Level_TRACE:
+            // Not used by plugins
+            break;
+
+          default:
+          {
+            std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) +
+                             ") for message: " + message);
+            context_->InvokeService(context_, _OrthancPluginService_LogInfo, s.c_str());
+            break;
+          }
+        }
+      }
+    }
   }
 }
 
--- a/Core/Logging.h	Fri Jun 05 17:20:56 2020 +0200
+++ b/Core/Logging.h	Fri Jun 05 18:26:15 2020 +0200
@@ -58,18 +58,23 @@
 #  endif
 #endif
 
-#if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
-#  include <orthanc/OrthancCPlugin.h>
-#endif
-
 #include <boost/lexical_cast.hpp>
 
 namespace Orthanc
 {
   namespace Logging
   {
+    enum Level
+    {
+      Level_ERROR,
+      Level_WARNING,
+      Level_INFO,
+      Level_TRACE
+    };
+    
 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
-    ORTHANC_PUBLIC void Initialize(OrthancPluginContext* context);
+    // "pluginContext" must be of type "OrthancPluginContext"
+    ORTHANC_PUBLIC void Initialize(void* pluginContext);
 #else
     ORTHANC_PUBLIC void Initialize();
 #endif
@@ -94,14 +99,12 @@
 
 #if ORTHANC_ENABLE_LOGGING_STDIO == 1
     typedef void (*LoggingFunction)(const char*);
-    void SetErrorWarnInfoTraceLoggingFunctions(
-      LoggingFunction errorLogFunc,
-      LoggingFunction warningLogfunc,
-      LoggingFunction infoLogFunc,
-      LoggingFunction traceLogFunc);
+    ORTHANC_PUBLIC void SetErrorWarnInfoTraceLoggingFunctions(LoggingFunction errorLogFunc,
+                                                              LoggingFunction warningLogfunc,
+                                                              LoggingFunction infoLogFunc,
+                                                              LoggingFunction traceLogFunc);
 #endif
 
-
     struct NullStream : public std::ostream 
     {
       NullStream() : 
@@ -129,30 +132,22 @@
 
 #  include <boost/noncopyable.hpp>
 #  define LOG(level)  ::Orthanc::Logging::InternalLogger                \
-  (::Orthanc::Logging::InternalLevel_ ## level, __FILE__, __LINE__)
+  (::Orthanc::Logging::Level_ ## level, __FILE__, __LINE__)
 #  define VLOG(level) ::Orthanc::Logging::InternalLogger        \
-  (::Orthanc::Logging::InternalLevel_TRACE, __FILE__, __LINE__)
+  (::Orthanc::Logging::Level_TRACE, __FILE__, __LINE__)
 
 namespace Orthanc
 {
   namespace Logging
   {
-    enum InternalLevel
-    {
-      InternalLevel_ERROR,
-      InternalLevel_WARNING,
-      InternalLevel_INFO,
-      InternalLevel_TRACE
-    };
-    
     class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable
     {
     private:
-      InternalLevel       level_;
+      Level       level_;
       std::stringstream   messageStream_;
 
     public:
-      InternalLogger(InternalLevel level,
+      InternalLogger(Level level,
                      const char* file,
                      int line);
 
@@ -179,6 +174,7 @@
 #  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)
 #  define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__)
 
+
 namespace Orthanc
 {
   namespace Logging
--- a/Core/OrthancFramework.h	Fri Jun 05 17:20:56 2020 +0200
+++ b/Core/OrthancFramework.h	Fri Jun 05 18:26:15 2020 +0200
@@ -41,23 +41,33 @@
 #ifndef __ORTHANC_FRAMEWORK_H
 #define __ORTHANC_FRAMEWORK_H
 
+#if !defined(ORTHANC_FRAMEWORK_BUILDING_PLUGIN)
+#  error Macro ORTHANC_FRAMEWORK_BUILDING_PLUGIN must be defined
+#endif
+
 /**
  * It is implied that if this file is used, we're building the Orthanc
- * framework (not using it): We don't use the common "BUILDING_DLL"
+ * framework (not using it as a shared library): We don't use the
+ * common "BUILDING_DLL"
  * construction. https://gcc.gnu.org/wiki/Visibility
  **/
-#if defined(_WIN32) || defined (__CYGWIN__)
-#  define ORTHANC_PUBLIC __declspec(dllexport)
-#  define ORTHANC_LOCAL
+#if ORTHANC_FRAMEWORK_BUILDING_PLUGIN == 0
+#  if defined(_WIN32) || defined (__CYGWIN__)
+#    define ORTHANC_PUBLIC __declspec(dllexport)
+#    define ORTHANC_LOCAL
+#  else
+#    if __GNUC__ >= 4
+#      define ORTHANC_PUBLIC __attribute__((visibility ("default")))
+#      define ORTHANC_LOCAL  __attribute__((visibility ("hidden")))
+#    else
+#      define ORTHANC_PUBLIC
+#      define ORTHANC_LOCAL
+#      pragma warning Unknown dynamic link import/export semantics
+#    endif
+#  endif
 #else
-#  if __GNUC__ >= 4
-#    define ORTHANC_PUBLIC __attribute__((visibility ("default")))
-#    define ORTHANC_LOCAL  __attribute__((visibility ("hidden")))
-#  else
-#    define ORTHANC_PUBLIC
-#    define ORTHANC_LOCAL
-#    pragma warning Unknown dynamic link import/export semantics
-#  endif
+#  define ORTHANC_PUBLIC
+#  define ORTHANC_LOCAL
 #endif
 
 #endif /* __ORTHANC_FRAMEWORK_H */