From 11d8b87bc49854d7a813ca722e28f059ae7980dd Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 24 Feb 2016 09:56:57 +0100
Subject: [PATCH] Use unique_ptr in LogoSetup, use LogogSetup in testrunner.

---
 Applications/ApplicationsLib/LogogSetup.h | 24 +++++---
 Tests/testrunner.cpp                      | 71 +++++++++++------------
 2 files changed, 51 insertions(+), 44 deletions(-)

diff --git a/Applications/ApplicationsLib/LogogSetup.h b/Applications/ApplicationsLib/LogogSetup.h
index 7f4bd524a42..782c3766b51 100644
--- a/Applications/ApplicationsLib/LogogSetup.h
+++ b/Applications/ApplicationsLib/LogogSetup.h
@@ -12,6 +12,9 @@
 
 #include <logog/include/logog.hpp>
 
+#include <map>
+#include <memory>
+
 #include "BaseLib/LogogSimpleFormatter.h"
 
 namespace ApplicationsLib
@@ -25,21 +28,28 @@ public:
 	LogogSetup()
 	{
 		LOGOG_INITIALIZE();
-		fmt = new BaseLib::LogogSimpleFormatter;
-		logog_cout = new logog::Cout;
-		logog_cout->SetFormatter(*fmt);
+		logog_cout = std::unique_ptr<logog::Cout>(new logog::Cout);
+		SetFormatter(std::unique_ptr<BaseLib::LogogSimpleFormatter>
+			(new BaseLib::LogogSimpleFormatter));
 	}
 
 	~LogogSetup()
 	{
-		delete fmt;
-		delete logog_cout;
+		// Objects have to be deleted before shutdown
+		fmt.reset(nullptr);
+		logog_cout.reset(nullptr);
 		LOGOG_SHUTDOWN();
 	}
 
+	void SetFormatter(std::unique_ptr<logog::Formatter> formatter)
+	{
+		fmt = std::move(formatter);
+		logog_cout->SetFormatter(*fmt);
+	}
+
 private:
-	BaseLib::LogogSimpleFormatter* fmt;
-	logog::Cout* logog_cout;
+	std::unique_ptr<logog::Formatter> fmt;
+	std::unique_ptr<logog::Cout> logog_cout;
 };
 
 }	// ApplicationsLib
diff --git a/Tests/testrunner.cpp b/Tests/testrunner.cpp
index 2c233a8b4cb..baafff31009 100644
--- a/Tests/testrunner.cpp
+++ b/Tests/testrunner.cpp
@@ -16,7 +16,6 @@
 #include <clocale>
 
 #include "gtest/gtest.h"
-#include "logog/include/logog.hpp"
 
 #ifdef USE_MPI
 #include <mpi.h>
@@ -30,7 +29,7 @@
 #include <petscksp.h>
 #endif
 
-#include "BaseLib/LogogCustomCout.h"
+#include "Applications/ApplicationsLib/LogogSetup.h"
 #include "BaseLib/TemplateLogogFormatterSuppressedGCC.h"
 #ifdef OGS_BUILD_GUI
 #include <QApplication>
@@ -44,57 +43,55 @@ int main(int argc, char* argv[])
     QApplication app(argc, argv, false);
 #endif
     int ret = 0;
-    LOGOG_INITIALIZE();
-    {
 #ifdef USE_MPI
-        MPI_Init(&argc, &argv);
+    MPI_Init(&argc, &argv);
 #endif
-        BaseLib::LogogCustomCout out;
-        BaseLib::TemplateLogogFormatterSuppressedGCC<TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG> custom_format;
-        out.SetFormatter(custom_format);
+    ApplicationsLib::LogogSetup logog_setup;
+    logog_setup.SetFormatter(std::unique_ptr<BaseLib::TemplateLogogFormatterSuppressedGCC
+        <TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG> >
+            (new BaseLib::TemplateLogogFormatterSuppressedGCC
+            <TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG>()));
+
 
 #ifdef USE_PETSC
-        char help[] = "ogs6 with PETSc \n";
-        PetscInitialize(&argc,&argv,(char *)0,help);
+    char help[] = "ogs6 with PETSc \n";
+    PetscInitialize(&argc,&argv,(char *)0,help);
 #endif
 
-        try
-        {
-            // initialize libraries which will be used while testing
+    try
+    {
+        // initialize libraries which will be used while testing
 #ifdef USE_LIS
-            lis_initialize(&argc, &argv);
+        lis_initialize(&argc, &argv);
 #endif
-            // start google test
-            testing::InitGoogleTest ( &argc, argv );
-            ret = RUN_ALL_TESTS();
-        }
-        catch (char* e)
-        {
-            ERR(e);
-        }
-        catch (std::exception& e)
-        {
-            ERR(e.what());
-        }
-        catch (...)
-        {
-            ERR("Unknown exception occurred!");
-        }
-        // finalize libraries
+        // start google test
+        testing::InitGoogleTest ( &argc, argv );
+        ret = RUN_ALL_TESTS();
+    }
+    catch (char* e)
+    {
+        ERR(e);
+    }
+    catch (std::exception& e)
+    {
+        ERR(e.what());
+    }
+    catch (...)
+    {
+        ERR("Unknown exception occurred!");
+    }
+    // finalize libraries
 #ifdef USE_LIS
-        lis_finalize();
+    lis_finalize();
 #endif
 
 #ifdef USE_PETSC
-        PetscFinalize();
+    PetscFinalize();
 #endif
 
 #ifdef USE_MPI
-        MPI_Finalize();
+    MPI_Finalize();
 #endif
 
-    } // make sure no logog objects exist when LOGOG_SHUTDOWN() is called.
-    LOGOG_SHUTDOWN();
-
     return ret;
 }
-- 
GitLab