From b968961e6683cb0facff8b48768b3dea2c34e56e Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Mon, 10 Oct 2016 20:29:13 +0200
Subject: [PATCH] Fix externalization problems.

---
 .../include/DD4hep/ComponentProperties_inl.h  | 23 +----
 DDCore/include/DD4hep/Parsers.h               | 11 +++
 DDCore/include/DD4hep/Printout.h              |  8 +-
 DDCore/src/BasicGrammar.cpp                   |  2 +-
 DDCore/src/Plugins.cpp                        |  3 +-
 DDCore/src/Printout.cpp                       | 97 +++++++++++--------
 6 files changed, 78 insertions(+), 66 deletions(-)

diff --git a/DDCore/include/DD4hep/ComponentProperties_inl.h b/DDCore/include/DD4hep/ComponentProperties_inl.h
index d9a5b547f..c918c5842 100644
--- a/DDCore/include/DD4hep/ComponentProperties_inl.h
+++ b/DDCore/include/DD4hep/ComponentProperties_inl.h
@@ -26,9 +26,7 @@
 #include <set>
 #include <stdexcept>
 
-/*
- *   DD4hep namespace declaration
- */
+/// DD4hep namespace declaration
 namespace DD4hep {
 
   /// Setup property
@@ -71,25 +69,6 @@ namespace DD4hep {
 
 }      // End namespace DD4hep
 
-#ifndef DD4HEP_PARSERS_NO_ROOT
-// These operators do not really belong here, but also nowhere else.....
-// ....except the proper ROOT headers perhaps?
-#include "Math/Point3D.h"
-#include "Math/Vector3D.h"
-#include "Math/Vector4D.h"
-
-namespace ROOT {
-  namespace Math {
-    /// Allow point insertion of a point in maps
-    inline bool operator<(const XYZPoint& a, const XYZPoint& b);
-    /// Allow 3-vector insertion of a  in maps
-    inline bool operator<(const XYZVector& a, const XYZVector& b);
-    /// Allow 4-vector insertion of a  in maps
-    inline bool operator<(const PxPyPzEVector& a, const PxPyPzEVector& b);
-  }
-}
-#endif
-
 // Instantiate single property
 #define DD4HEP_DEFINE_PROPERTY_TYPE(x)                    \
   template x Property::value() const;                     \
diff --git a/DDCore/include/DD4hep/Parsers.h b/DDCore/include/DD4hep/Parsers.h
index 87f6479c2..b8b4213a4 100644
--- a/DDCore/include/DD4hep/Parsers.h
+++ b/DDCore/include/DD4hep/Parsers.h
@@ -565,6 +565,17 @@ namespace DD4hep {
 #include "Math/Vector3D.h"
 #include "Math/Vector4D.h"
 
+namespace ROOT {
+  namespace Math {
+    /// Allow point insertion of a point in maps
+    bool operator<(const XYZPoint& a, const XYZPoint& b);
+    /// Allow 3-vector insertion of a  in maps
+    bool operator<(const XYZVector& a, const XYZVector& b);
+    /// Allow 4-vector insertion of a  in maps
+    bool operator<(const PxPyPzEVector& a, const PxPyPzEVector& b);
+  }
+}
+
 /// Namespace for the AIDA detector description toolkit
 namespace DD4hep {
   /// Namespace for the AIDA detector for utilities using boost::spirit parsers
diff --git a/DDCore/include/DD4hep/Printout.h b/DDCore/include/DD4hep/Printout.h
index 858a65793..f1d03e19a 100644
--- a/DDCore/include/DD4hep/Printout.h
+++ b/DDCore/include/DD4hep/Printout.h
@@ -50,7 +50,8 @@ namespace DD4hep {
   };
 
 #ifndef __CINT__
-  typedef size_t (*output_function_t)(void*, PrintLevel severity, const char*, const char*);
+  typedef size_t (*output_function1_t)(void*, PrintLevel severity, const char*, const char*);
+  typedef size_t (*output_function2_t)(void*, PrintLevel severity, const char*, const char*, va_list& args);
 
   /** Calls the display action
    *  @arg severity   [int,read-only]      Display severity flag (see enum)
@@ -181,7 +182,10 @@ namespace DD4hep {
   std::string format(const char* src, const char* fmt, va_list& args);
 
   /// Customize printer function
-  void setPrinter(void* print_arg, output_function_t fcn);
+  void setPrinter(void* print_arg, output_function1_t fcn);
+
+  /// Customize printer function
+  void setPrinter2(void* print_arg, output_function2_t fcn);
 
 #endif // __CINT__
 
diff --git a/DDCore/src/BasicGrammar.cpp b/DDCore/src/BasicGrammar.cpp
index b66123a56..21570df2a 100644
--- a/DDCore/src/BasicGrammar.cpp
+++ b/DDCore/src/BasicGrammar.cpp
@@ -32,7 +32,7 @@ DD4hep::BasicGrammar::~BasicGrammar()   {
 void DD4hep::BasicGrammar::invalidConversion(const std::string& value, const std::type_info& to) {
   std::string to_name = typeName(to);
   throw unrelated_value_error(to,
-                              "Data conversion of '" + value + "' to type '" + 
+                              "Data conversion of " + value + " to type '" + 
                               to_name + "' is not defined.");
 }
 
diff --git a/DDCore/src/Plugins.cpp b/DDCore/src/Plugins.cpp
index 143d1b0dc..7b429c462 100644
--- a/DDCore/src/Plugins.cpp
+++ b/DDCore/src/Plugins.cpp
@@ -95,6 +95,7 @@ namespace   {
 #if !defined(DD4HEP_PARSERS_NO_ROOT)
     PluginService::FuncPointer<Func_t> fun(gSystem->DynFindSymbol(plugin,entry));
     PluginService::FuncPointer<T> fp(fun.fptr.ptr);
+    if ( handle ) {}
 #else
     PluginService::FuncPointer<T> fp(::dlsym(handle, entry));
     if ( !fp.fptr.ptr ) fp.fptr.ptr = ::dlsym(0, entry);
@@ -102,7 +103,7 @@ namespace   {
     if ( 0 == fp.fptr.ptr )      {
       string err = "DD4hep:PluginService: Failed to access symbol "
         "\""+string(entry)+"\" in plugin library "+string(plugin)+
-	" ["+string(::strerror(errno))+"]";
+        " ["+string(::strerror(errno))+"]";
       throw runtime_error(err);
     }
     return fp.fptr.fcn;
diff --git a/DDCore/src/Printout.cpp b/DDCore/src/Printout.cpp
index 02af783b3..1eb7899a9 100644
--- a/DDCore/src/Printout.cpp
+++ b/DDCore/src/Printout.cpp
@@ -21,42 +21,58 @@
 
 using namespace std;
 
-static std::string print_fmt = "%-16s %5s %s";
+namespace {
+  size_t _the_printer_1(void*, DD4hep::PrintLevel lvl, const char* src, const char* text);
+  size_t _the_printer_2(void* par, DD4hep::PrintLevel lvl, const char* src, const char* fmt, va_list& args);
 
-static size_t _the_printer(void*, DD4hep::PrintLevel lvl, const char* src, const char* text) {
-  const char* p_lvl = "?????";
-  if ( lvl> DD4hep::ALWAYS ) lvl = DD4hep::ALWAYS;
-  if ( lvl< DD4hep::NOLOG  ) lvl = DD4hep::NOLOG;
-  switch(lvl)   {
-  case DD4hep::NOLOG:     p_lvl = "NOLOG"; break;
-  case DD4hep::VERBOSE:   p_lvl = "VERB "; break;
-  case DD4hep::DEBUG:     p_lvl = "DEBUG"; break;
-  case DD4hep::INFO:      p_lvl = "INFO "; break;
-  case DD4hep::WARNING:   p_lvl = "WARN "; break;
-  case DD4hep::ERROR:     p_lvl = "ERROR"; break;
-  case DD4hep::FATAL:     p_lvl = "FATAL"; break;
-  case DD4hep::ALWAYS:    p_lvl = "     "; break;
-  default:                                 break;
+  std::string print_fmt = "%-16s %5s %s";
+  DD4hep::PrintLevel print_lvl = DD4hep::INFO;
+  void* print_arg = 0;
+  DD4hep::output_function1_t print_func_1 = 0;
+  DD4hep::output_function2_t print_func_2 = _the_printer_2;
+
+  const char* print_level(DD4hep::PrintLevel lvl)   {
+    switch(lvl)   {
+    case DD4hep::NOLOG:     return "NOLOG";
+    case DD4hep::VERBOSE:   return "VERB ";
+    case DD4hep::DEBUG:     return "DEBUG";
+    case DD4hep::INFO:      return "INFO ";
+    case DD4hep::WARNING:   return "WARN ";
+    case DD4hep::ERROR:     return "ERROR";
+    case DD4hep::FATAL:     return "FATAL";
+    case DD4hep::ALWAYS:    return "     ";
+    default:
+      if ( lvl> DD4hep::ALWAYS )
+	return print_level(DD4hep::ALWAYS);
+      return print_level(DD4hep::NOLOG);
+    }
   }
 
-  size_t len = ::fprintf(stdout, print_fmt.c_str(), src, p_lvl, text);
-  // size_t len = ::fputs(src, stdout);
-  // len += fputs(": ", stdout);
-  // len += fputs(text, stdout);
-  ::fflush(stdout);
-  return len;
-}
+  size_t _the_printer_1(void*, DD4hep::PrintLevel lvl, const char* src, const char* text) {
+    size_t len = ::fprintf(stdout, print_fmt.c_str(), src, print_level(lvl), text);
+    ::fflush(stdout);
+    return len;
+  }
 
-static string __format(const char* fmt, va_list& args) {
-  char str[4096];
-  ::vsnprintf(str, sizeof(str), fmt, args);
-  va_end(args);
-  return string(str);
-}
+  size_t _the_printer_2(void* par, DD4hep::PrintLevel lvl, const char* src, const char* fmt, va_list& args) {
+    if ( !print_func_1 )  {
+      char text[4096];
+      ::snprintf(text,sizeof(text),print_fmt.c_str(),src,print_level(lvl),fmt);
+      size_t len = ::vfprintf(stdout, text, args);
+      ::fflush(stdout);
+      return len;
+    }
+    char str[4096];
+    ::vsnprintf(str, sizeof(str), fmt, args);
+    return print_func_1(par, lvl, src, str);
+  }
 
-static DD4hep::PrintLevel print_lvl = DD4hep::INFO;
-static void* print_arg = 0;
-static DD4hep::output_function_t print_func = _the_printer;
+  string __format(const char* fmt, va_list& args) {
+    char str[4096];
+    ::vsnprintf(str, sizeof(str), fmt, args);
+    return string(str);
+  }
+}
 
 /** Calls the display action
  *  @arg severity   [int,read-only]      Display severity flag
@@ -130,12 +146,7 @@ int DD4hep::printout(PrintLevel severity, const string& src, const string& fmt,
  */
 int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, va_list& args) {
   if (severity >= print_lvl) {
-    char str[4096];
-    size_t len = vsnprintf(str, sizeof(str) - 2, fmt, args);
-    if ( len>sizeof(str)-2 ) len = sizeof(str) - 2;
-    str[len] = '\n';
-    str[len + 1] = '\0';
-    print_func(print_arg, severity, src, str);
+    print_func_2(print_arg, severity,src,fmt,args);
   }
   return 1;
 }
@@ -288,12 +299,18 @@ DD4hep::PrintLevel DD4hep::printLevel()  {
 /// Set new printout format for the 3 fields: source-level-message. All 3 are strings
 string DD4hep::setPrintFormat(const string& new_format) {
   string old = print_fmt;
-  print_fmt = new_format;
+  print_fmt  = new_format;
   return old;
 }
 
 /// Customize printer function
-void DD4hep::setPrinter(void* arg, output_function_t fcn) {
+void DD4hep::setPrinter(void* arg, output_function1_t fcn) {
+  print_arg  = arg;
+  print_func_1 = fcn ? fcn : _the_printer_1;
+}
+
+/// Customize printer function
+void DD4hep::setPrinter2(void* arg, output_function2_t fcn) {
   print_arg = arg;
-  print_func = fcn;
+  print_func_2 = fcn ? fcn : _the_printer_2;
 }
-- 
GitLab