From f9727e4019ec9b1f36bf8c9979f0e20629612e9a Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@cnes.fr>
Date: Wed, 21 Feb 2018 14:06:44 +0100
Subject: [PATCH] ENH: Make otb::Logger a singleton

---
 Modules/Core/Common/include/otbLogger.h         | 16 +++++++++++++---
 Modules/Core/Common/src/otbLogger.cxx           | 11 +++++++++++
 .../include/otbWrapperApplication.h             |  5 -----
 .../src/otbWrapperApplication.cxx               | 17 +++--------------
 .../src/otbWrapperCompositeApplication.cxx      |  2 --
 5 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/Modules/Core/Common/include/otbLogger.h b/Modules/Core/Common/include/otbLogger.h
index 047c225f76..7325a9adcd 100644
--- a/Modules/Core/Common/include/otbLogger.h
+++ b/Modules/Core/Common/include/otbLogger.h
@@ -43,14 +43,24 @@ public:
   typedef itk::SmartPointer< const Self > ConstPointer;
 
   itkTypeMacro(Logger, itk::Logger);
-  itkNewMacro(Self);
 
+  static Pointer Instance();
+
+  
   // Overwrite this to provide custom formatting of log entries
   virtual std::string BuildFormattedEntry(itk::Logger::PriorityLevelType, std::string const&) ITK_OVERRIDE;
 
 protected:
-    Logger();
-    virtual ~Logger() ITK_OVERRIDE;
+  itkNewMacro(Self);
+  Logger();
+  virtual ~Logger() ITK_OVERRIDE;
+
+private:
+  Logger(const Self &); //purposely not implemented
+  void operator =(const Self&); //purposely not implemented
+
+  static Pointer Singleton;
+
 }; // class Logger
 
 } // namespace otb
diff --git a/Modules/Core/Common/src/otbLogger.cxx b/Modules/Core/Common/src/otbLogger.cxx
index 09f08cb22e..c088a2d5a0 100644
--- a/Modules/Core/Common/src/otbLogger.cxx
+++ b/Modules/Core/Common/src/otbLogger.cxx
@@ -24,6 +24,17 @@
 namespace otb
 {
 
+Logger::Pointer Logger::Singleton = ITK_NULLPTR;
+
+Logger::Pointer Logger::Instance()
+{
+  if(!Logger::Singleton)
+    Logger::Singleton = Logger::New();
+
+  return Logger::Singleton;
+}
+
+
 Logger::Logger()
 {
 #if OTB_DEBUG
diff --git a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
index c6317be142..76ccd9bb5f 100644
--- a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
+++ b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
@@ -101,7 +101,6 @@ public:
     m_Name = name;
     GetDocExample()->SetApplicationName(name);
     this->Modified();
-    m_Logger->SetName(name);
   }
 
   itkGetStringMacro(Name);
@@ -709,9 +708,6 @@ public:
 
   otb::Logger* GetLogger() const;
 
-  /** Sets the logger instance of the application (use with caution) */
-  void SetLogger(otb::Logger *logger);
-
   itk::ProcessObject* GetProgressSource() const;
 
   std::string GetProgressDescription() const;
@@ -984,7 +980,6 @@ private:
   std::string                       m_Name;
   std::string                       m_Description;
   ParameterGroup::Pointer           m_ParameterList;
-  otb::Logger::Pointer              m_Logger;
 
   itk::ProcessObject::Pointer       m_ProgressSource;
   std::string                       m_ProgressSourceDescription;
diff --git a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
index cca32d50d3..4a7804d527 100644
--- a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
+++ b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
@@ -46,6 +46,7 @@
 #include <exception>
 #include "itkMacro.h"
 
+
 namespace otb
 {
 namespace Wrapper
@@ -54,7 +55,6 @@ namespace Wrapper
 Application::Application()
   : m_Name(""),
     m_Description(""),
-    m_Logger(otb::Logger::New()),
     m_ProgressSourceDescription(""),
     m_DocName(""),
     m_DocLongDescription(""),
@@ -66,10 +66,7 @@ Application::Application()
     m_HaveInXML(true),
     m_HaveOutXML(true),
     m_IsInXMLParsed(false)
-{
-  // Don't call Init from the constructor, since it calls a virtual method !
-  m_Logger->SetName("Application.logger");
-}
+{}
 
 Application::~Application()
 {
@@ -77,15 +74,7 @@ Application::~Application()
 
 otb::Logger* Application::GetLogger() const
 {
-  return m_Logger;
-}
-
-void Application::SetLogger(otb::Logger *logger)
-{
-  if (m_Logger != logger)
-    {
-    m_Logger = logger;
-    }
+  return otb::Logger::Instance();
 }
 
 std::vector<std::string>
diff --git a/Modules/Wrappers/ApplicationEngine/src/otbWrapperCompositeApplication.cxx b/Modules/Wrappers/ApplicationEngine/src/otbWrapperCompositeApplication.cxx
index db216594f5..7ebfa4d59a 100644
--- a/Modules/Wrappers/ApplicationEngine/src/otbWrapperCompositeApplication.cxx
+++ b/Modules/Wrappers/ApplicationEngine/src/otbWrapperCompositeApplication.cxx
@@ -62,8 +62,6 @@ CompositeApplication
   InternalApplication container;
   container.App = ApplicationRegistry::CreateApplication(appType);
   container.Desc = desc;
-  // Setup logger
-  container.App->SetLogger(this->GetLogger());
   container.App->AddObserver(AddProcessToWatchEvent(), m_AddProcessCommand.GetPointer());
   m_AppContainer[key] = container;
   return true;
-- 
GitLab