From 36c4a81a815bff5782b896df7cadf5da80c2a33c Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Mon, 23 Mar 2020 13:51:19 +0100
Subject: [PATCH] Set log pattern for mpi

---
 Applications/CLI/ogs.cpp | 12 +++++++-----
 BaseLib/Logging.cpp      | 12 ++++++++++++
 Tests/testrunner.cpp     | 36 ++++++++++++++++--------------------
 3 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp
index ea98eae5508..3ea15928f05 100644
--- a/Applications/CLI/ogs.cpp
+++ b/Applications/CLI/ogs.cpp
@@ -121,7 +121,8 @@ int main(int argc, char* argv[])
         std::cout.setf(std::ios::unitbuf);
     }
 
-    logog_setup.setLevel(log_level_arg.getValue());
+    BaseLib::setConsoleLogLevel(log_level_arg.getValue());
+    spdlog::set_pattern("%^%l:%$ %v");
 
     INFO("This is OpenGeoSys-6 version {:s}.",
          GitInfoLib::GitInfo::ogs_version);
@@ -166,10 +167,11 @@ int main(int argc, char* argv[])
             controller->Initialize(&argc, &argv, 1);
             vtkMPIController::SetGlobalController(controller);
 
-            logog_setup.setFormatter(
-                std::make_unique<BaseLib::TemplateLogogFormatterSuppressedGCC<
-                    TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG |
-                    TOPIC_LINE_NUMBER_FLAG>>());
+            {   // Can be called only after MPI_INIT.
+                int mpi_rank;
+                MPI_Comm_rank(PETSC_COMM_WORLD, &mpi_rank);
+                spdlog::set_pattern(fmt::format("[{}] %^%l:%$ %v", mpi_rank));
+            }
 #endif
             run_time.start();
 
diff --git a/BaseLib/Logging.cpp b/BaseLib/Logging.cpp
index ebc3829b86e..518964c509d 100644
--- a/BaseLib/Logging.cpp
+++ b/BaseLib/Logging.cpp
@@ -10,13 +10,25 @@
  */
 
 #include "Logging.h"
+
 #include <spdlog/sinks/stdout_color_sinks.h>
+
 #include <map>
 
+#ifdef USE_PETSC
+#include <mpi.h>
+#include <petscsys.h>
+#endif
+
 #include "Error.h"
+
 namespace BaseLib
 {
+#ifdef USE_PETSC
+std::shared_ptr<spdlog::logger> console = spdlog::stdout_color_mt("ogs");
+#else   // USE_PETSC
 std::shared_ptr<spdlog::logger> console = spdlog::stdout_color_st("ogs");
+#endif  // USE_PETSC
 
 void setConsoleLogLevel(std::string const& level_string)
 {
diff --git a/Tests/testrunner.cpp b/Tests/testrunner.cpp
index 8f90a1d6948..664eed2140d 100644
--- a/Tests/testrunner.cpp
+++ b/Tests/testrunner.cpp
@@ -12,15 +12,13 @@
  *
  */
 
-// ** INCLUDES **
 #include <clocale>
 
-#include "gtest/gtest.h"
-
-#include "Applications/ApplicationsLib/LogogSetup.h"
 #include "Applications/ApplicationsLib/LinearSolverLibrarySetup.h"
+#include "BaseLib/Error.h"
+#include "BaseLib/Logging.h"
 #include "NumLib/DOF/GlobalMatrixProviders.h"
-#include "BaseLib/TemplateLogogFormatterSuppressedGCC.h"
+#include "gtest/gtest.h"
 
 #ifdef OGS_BUILD_GUI
 #include <QCoreApplication>
@@ -51,24 +49,22 @@ int main(int argc, char* argv[])
     QCoreApplication app(argc, argv, false);
 #endif
 
-    // Attention: Order matters!
-    // logog_setup must be created first, then the linear_solver_library_setup,
-    // because destruction order is the reverse of the creation order and the
-    // destructor of linear_solver_library_setup might print log messages.
-    // The methods on logog_setup must be called after the construction of
-    // linear_solver_library_setup since they require, e.g., that MPI_Init()
-    // has been called before.
-
-    ApplicationsLib::LogogSetup logog_setup;
-
     ApplicationsLib::LinearSolverLibrarySetup linear_solver_library_setup(
                 argc, argv);
 
-    logog_setup.setFormatter(
-        std::make_unique<BaseLib::TemplateLogogFormatterSuppressedGCC<
-            TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG |
-            TOPIC_LINE_NUMBER_FLAG>>());
-    logog_setup.setLevel(logLevel);
+    BaseLib::setConsoleLogLevel(logLevel);
+    spdlog::set_pattern("%^%l:%$ %v");
+    spdlog::set_error_handler([](const std::string& msg) {
+        std::cerr << "spdlog error: " << msg << std::endl;
+        OGS_FATAL("spdlog logger error occured.");
+    });
+#ifdef USE_PETSC
+    {  // Can be called only after MPI_INIT.
+        int mpi_rank;
+        MPI_Comm_rank(PETSC_COMM_WORLD, &mpi_rank);
+        spdlog::set_pattern(fmt::format("[{}] %^%l:%$ %v", mpi_rank));
+    }
+#endif
 
     try
     {
-- 
GitLab