changeset 2243:2dbfdafc2512

Logger compatible with the Orthanc plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Dec 2016 16:02:00 +0100
parents 4e8e0ad2001c
children a3a65de1840f
files CMakeLists.txt Core/Logging.cpp Core/Logging.h
diffstat 3 files changed, 120 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Dec 19 15:31:01 2016 +0100
+++ b/CMakeLists.txt	Fri Dec 23 16:02:00 2016 +0100
@@ -412,6 +412,7 @@
   -DORTHANC_BUILD_UNIT_TESTS=1
   -DORTHANC_ENABLE_BASE64=1
   -DORTHANC_ENABLE_LOGGING=1
+  -DORTHANC_ENABLE_LOGGING_PLUGIN=0
   -DORTHANC_ENABLE_LUA=1
   -DORTHANC_ENABLE_MD5=1
   -DORTHANC_MAXIMUM_TAG_LENGTH=256
--- a/Core/Logging.cpp	Mon Dec 19 15:31:01 2016 +0100
+++ b/Core/Logging.cpp	Fri Dec 23 16:02:00 2016 +0100
@@ -73,7 +73,74 @@
   }
 }
 
-#else
+
+#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+
+/*********************************************************
+ * Logger compatible with the Orthanc plugin SDK
+ *********************************************************/
+
+#include <boost/lexical_cast.hpp>
+
+namespace Orthanc
+{
+  namespace Logging
+  {
+    static OrthancPluginContext* context_ = NULL;
+
+    void Initialize(OrthancPluginContext* context)
+    {
+      context_ = context_;
+    }
+
+    InternalLogger::InternalLogger(const char* level,
+                                   const char* file  /* ignored */,
+                                   int line  /* ignored */) :
+      level_(level)
+    {
+    }
+
+    InternalLogger::~InternalLogger()
+    {
+      if (context_ != NULL)
+      {
+        if (level_ == "ERROR")
+        {
+          OrthancPluginLogError(context_, message_.c_str());
+        }
+        else if (level_ == "WARNING")
+        {
+          OrthancPluginLogWarning(context_, message_.c_str());
+        }
+        else if (level_ == "INFO")
+        {
+          OrthancPluginLogInfo(context_, message_.c_str());
+        }
+      }
+    }
+
+    InternalLogger& InternalLogger::operator<< (const std::string& message)
+    {
+      message_ += message;
+      return *this;
+    }
+
+    InternalLogger& InternalLogger::operator<< (const char* message)
+    {
+      message_ += std::string(message);
+      return *this;
+    }
+
+    InternalLogger& InternalLogger::operator<< (int message)
+    {
+      message_ += boost::lexical_cast<std::string>(message);
+      return *this;
+    }
+  }
+}
+
+
+#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */
 
 /*********************************************************
  * Internal logger of Orthanc, that mimics some
--- a/Core/Logging.h	Mon Dec 19 15:31:01 2016 +0100
+++ b/Core/Logging.h	Fri Dec 23 16:02:00 2016 +0100
@@ -38,11 +38,27 @@
 #  error The macro ORTHANC_ENABLE_LOGGING must be defined
 #endif
 
+#if !defined(ORTHANC_ENABLE_LOGGING_PLUGIN)
+#  if ORTHANC_ENABLE_LOGGING == 1
+#    error The macro ORTHANC_ENABLE_LOGGING_PLUGIN must be defined
+#  else
+#    define ORTHANC_ENABLE_LOGGING_PLUGIN 0
+#  endif
+#endif
+
+#if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+#  include <orthanc/OrthancCPlugin.h>
+#endif
+
 namespace Orthanc
 {
   namespace Logging
   {
+#if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+    void Initialize(OrthancPluginContext* context);
+#else
     void Initialize();
+#endif
 
     void Finalize();
 
@@ -86,7 +102,41 @@
 #  define LOG(level)   ::Orthanc::Logging::NullStream()
 #  define VLOG(level)  ::Orthanc::Logging::NullStream()
 
-#else  /* ORTHANC_ENABLE_LOGGING == 1 */
+
+#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+
+#  include <boost/noncopyable.hpp>
+#  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)
+#  define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__)
+
+namespace Orthanc
+{
+  namespace Logging
+  {
+    class InternalLogger : public boost::noncopyable
+    {
+    private:
+      std::string level_;
+      std::string message_;
+
+    public:
+      InternalLogger(const char* level,
+                     const char* file,
+                     int line);
+
+      ~InternalLogger();
+      
+      InternalLogger& operator<< (const std::string& message);
+
+      InternalLogger& operator<< (const char* message);
+
+      InternalLogger& operator<< (int message);
+    };
+  }
+}
+
+
+#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */
 
 #  include <boost/thread/mutex.hpp>
 #  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)