changeset 2483:9c54c40eaf25

logging primitives for WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Mar 2018 15:10:37 +0100
parents 509041cb57db
children 911e62dbb4ac
files Core/Logging.cpp Core/Logging.h Resources/CMake/OrthancFrameworkConfiguration.cmake
diffstat 3 files changed, 165 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Logging.cpp	Thu Mar 01 09:29:06 2018 +0100
+++ b/Core/Logging.cpp	Fri Mar 02 15:10:37 2018 +0100
@@ -105,22 +105,31 @@
     {
       if (context_ != NULL)
       {
-        if (level_ == "ERROR")
-        {
-          OrthancPluginLogError(context_, message_.c_str());
-        }
-        else if (level_ == "WARNING")
+        switch (level_)
         {
-          OrthancPluginLogWarning(context_, message_.c_str());
-        }
-        else if (level_ == "INFO")
-        {
-          OrthancPluginLogInfo(context_, message_.c_str());
-        }
-        else
-        {
-          std::string s = "Unknown log level (" + level_ + ") for message: " + message_;
-          OrthancPluginLogError(context_, s.c_str());
+          case ERROR:
+            OrthancPluginLogError(context_, message_.c_str());
+            break;
+
+          case WARNING:
+            OrthancPluginLogWarning(context_, message_.c_str());
+            break;
+
+          case INFO:
+            OrthancPluginLogInfo(context_, message_.c_str());
+            break;
+
+          case 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;
+          }
         }
       }
     }
@@ -146,7 +155,94 @@
 }
 
 
-#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */
+#elif ORTHANC_ENABLE_LOGGING_STDIO == 1
+
+/*********************************************************
+ * Logger compatible with <stdio.h>
+ *********************************************************/
+
+#include <stdio.h>
+#include <boost/lexical_cast.hpp>
+
+namespace Orthanc
+{
+  namespace Logging
+  {
+    static bool globalVerbose_ = false;
+    static bool globalTrace_ = false;
+    
+    InternalLogger::InternalLogger(Level level,
+                                   const char* file  /* ignored */,
+                                   int line  /* ignored */) :
+      level_(level)
+    {
+    }
+
+    InternalLogger::~InternalLogger()
+    {
+      switch (level_)
+      {
+        case ERROR:
+          fprintf(stderr, "E: %s\n", message_.c_str());
+          break;
+
+        case WARNING:
+          fprintf(stdout, "W: %s\n", message_.c_str());
+          break;
+
+        case INFO:
+          if (globalVerbose_)
+          {
+            fprintf(stdout, "I: %s\n", message_.c_str());
+          }
+          break;
+
+        case TRACE:
+          if (globalTrace_)
+          {
+            fprintf(stdout, "T: %s\n", message_.c_str());
+          }
+          break;
+
+        default:
+          fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, 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;
+    }
+
+    void EnableInfoLevel(bool enabled)
+    {
+      globalVerbose_ = enabled;
+    }
+
+    void EnableTraceLevel(bool enabled)
+    {
+      globalTrace_ = enabled;
+    }
+  }
+}
+
+
+#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && 
+          ORTHANC_ENABLE_LOGGING_STDIO == 0 && 
+          ORTHANC_ENABLE_LOGGING == 1 */
 
 /*********************************************************
  * Internal logger of Orthanc, that mimics some
@@ -156,7 +252,12 @@
 #include "OrthancException.h"
 #include "Enumerations.h"
 #include "Toolbox.h"
-#include "SystemToolbox.h"
+
+#if ORTHANC_SANDBOXED == 1
+#  include <stdio.h>
+#else
+#  include "SystemToolbox.h"
+#endif
 
 #include <fstream>
 #include <boost/filesystem.hpp>
@@ -232,9 +333,9 @@
               static_cast<int>(now.date().year()),
               now.date().month().as_number(),
               now.date().day().as_number(),
-              now.time_of_day().hours(),
-              now.time_of_day().minutes(),
-              now.time_of_day().seconds(),
+              static_cast<int>(now.time_of_day().hours()),
+              static_cast<int>(now.time_of_day().minutes()),
+              static_cast<int>(now.time_of_day().seconds()),
               SystemToolbox::GetProcessId());
 
       std::string programName = exe.filename().replace_extension("").string();
@@ -436,9 +537,9 @@
                   level[0],
                   now.date().month().as_number(),
                   now.date().day().as_number(),
-                  duration.hours(),
-                  duration.minutes(),
-                  duration.seconds(),
+                  static_cast<int>(duration.hours()),
+                  static_cast<int>(duration.minutes()),
+                  static_cast<int>(duration.seconds()),
                   static_cast<int>(duration.fractional_seconds()));
 
           header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] ";
--- a/Core/Logging.h	Thu Mar 01 09:29:06 2018 +0100
+++ b/Core/Logging.h	Fri Mar 02 15:10:37 2018 +0100
@@ -47,6 +47,14 @@
 #  endif
 #endif
 
+#if !defined(ORTHANC_ENABLE_LOGGING_STDIO)
+#  if ORTHANC_ENABLE_LOGGING == 1
+#    error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined
+#  else
+#    define ORTHANC_ENABLE_LOGGING_STDIO 0
+#  endif
+#endif
+
 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
 #  include <orthanc/OrthancCPlugin.h>
 #endif
@@ -104,24 +112,35 @@
 #  define VLOG(level)  ::Orthanc::Logging::NullStream()
 
 
-#elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
+#elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 ||    \
+       ORTHANC_ENABLE_LOGGING_STDIO == 1)
 
 #  include <boost/noncopyable.hpp>
-#  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)
-#  define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__)
+#  define LOG(level)  ::Orthanc::Logging::InternalLogger \
+  (::Orthanc::Logging::level, __FILE__, __LINE__)
+#  define VLOG(level) ::Orthanc::Logging::InternalLogger \
+  (::Orthanc::Logging::TRACE, __FILE__, __LINE__)
 
 namespace Orthanc
 {
   namespace Logging
   {
+    enum Level
+    {
+      ERROR,
+      WARNING,
+      INFO,
+      TRACE
+    };
+    
     class InternalLogger : public boost::noncopyable
     {
     private:
-      std::string level_;
+      Level       level_;
       std::string message_;
 
     public:
-      InternalLogger(const char* level,
+      InternalLogger(Level level,
                      const char* file,
                      int line);
 
@@ -137,7 +156,11 @@
 }
 
 
-#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */
+
+
+#else  /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && 
+          ORTHANC_ENABLE_LOGGING_STDIO == 0 && 
+          ORTHANC_ENABLE_LOGGING == 1 */
 
 #  include <boost/thread/mutex.hpp>
 #  define LOG(level)  ::Orthanc::Logging::InternalLogger(#level,  __FILE__, __LINE__)
--- a/Resources/CMake/OrthancFrameworkConfiguration.cmake	Thu Mar 01 09:29:06 2018 +0100
+++ b/Resources/CMake/OrthancFrameworkConfiguration.cmake	Fri Mar 02 15:10:37 2018 +0100
@@ -428,15 +428,26 @@
 if (ORTHANC_SANDBOXED)
   add_definitions(
     -DORTHANC_SANDBOXED=1
-    -DORTHANC_ENABLE_LOGGING=0
     -DORTHANC_ENABLE_LOGGING_PLUGIN=0
     )
+
+  if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
+    add_definitions(
+      -DORTHANC_ENABLE_LOGGING=1
+      -DORTHANC_ENABLE_LOGGING_STDIO=1
+      )
+  else()
+    add_definitions(
+      -DORTHANC_ENABLE_LOGGING=0
+      )
+  endif()
   
 else()
   add_definitions(
     -DORTHANC_SANDBOXED=0
     -DORTHANC_ENABLE_LOGGING=1
     -DORTHANC_ENABLE_LOGGING_PLUGIN=0
+    -DORTHANC_ENABLE_LOGGING_STDIO=0
     )
   
   list(APPEND ORTHANC_CORE_SOURCES_INTERNAL