From 30832d967a01b8052bdef5eae6f988a321a2e85a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karol=20Sieroci=C5=84ski?= <ksiero@man.poznan.pl>
Date: Thu, 20 Sep 2018 10:02:56 +0200
Subject: [PATCH] Moved logger mpi ranks setup to mpi plugin

---
 include/pdi/logger.h |  8 ++++++++
 plugins/mpi/mpi.cxx  | 32 +++++++++++++++++++++++++++++++-
 src/logger.cxx       | 42 +++++-------------------------------------
 3 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/include/pdi/logger.h b/include/pdi/logger.h
index 9b037e170..dbc54b0f3 100644
--- a/include/pdi/logger.h
+++ b/include/pdi/logger.h
@@ -30,6 +30,14 @@
 
 namespace PDI {
 
+/**
+ * Sets logger verbosity level from PC_tree
+ *
+ * \param[in] logger logger to set up
+ * \param[in] config configuration tree from config file
+ */
+void PDI_EXPORT read_log_level(Logger_sptr logger, PC_tree_t logging_tree);
+
 /**
  * Reads configuration tree and sets up the logger.
  *
diff --git a/plugins/mpi/mpi.cxx b/plugins/mpi/mpi.cxx
index 667daca8b..c456ec61a 100644
--- a/plugins/mpi/mpi.cxx
+++ b/plugins/mpi/mpi.cxx
@@ -23,19 +23,49 @@
  ******************************************************************************/
 
 #include <mpi.h>
+#include <string>
 
 #include <pdi/context.h>
+#include <pdi/logger.h>
 #include <pdi/plugin.h>
+#include <pdi/paraconf_wrapper.h>
 #include <pdi/scalar_datatype.h>
 
+#include <spdlog/spdlog.h>
 
 namespace {
 
 struct mpi_plugin: PDI::Plugin {
 
-	mpi_plugin(PDI::Context& ctx, PC_tree_t, MPI_Comm*):
+	void set_up_logger(PDI::Context& ctx, PC_tree_t logging_tree)
+	{
+		//set up format
+		int world_rank;
+		MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
+		char format[64];
+		snprintf(format, 64, "[PDI][%06d][%%T] *** %%^%%l%%$: %%v", world_rank);
+		ctx.logger()->set_pattern(std::string(format));
+		
+		//set up single ranks
+		PC_tree_t single_tree = PC_get(logging_tree, ".single");
+		if (!PC_status(single_tree)) {
+			int nb_key = PDI::len(single_tree);
+			for (int key_id = 0; key_id < nb_key; ++key_id) {
+				PC_tree_t rank_tree = PC_get(single_tree, "[%d]", key_id);
+				int selected_rank = PDI::to_long(PC_get(rank_tree, ".rank"), -1);
+				if (selected_rank == world_rank) {
+					PDI::read_log_level(ctx.logger(), rank_tree);
+					break;
+				}
+			}
+		}
+	}
+	
+	mpi_plugin(PDI::Context& ctx, PC_tree_t config, MPI_Comm*):
 		Plugin{ctx}
 	{
+		set_up_logger(ctx, PC_get(config, ".logging"));
+
 		// create the MPI_Comm datatype
 		PDI::Scalar_datatype mpi_comm_datatype{PDI::Scalar_kind::UNKNOWN, sizeof(MPI_Comm), alignof(MPI_Comm)};
 		
diff --git a/src/logger.cxx b/src/logger.cxx
index b83759119..ca958f1d6 100644
--- a/src/logger.cxx
+++ b/src/logger.cxx
@@ -22,8 +22,6 @@
  * THE SOFTWARE.
  ******************************************************************************/
 
-#include <mpi.h>
-
 #include <memory>
 #include <string>
 #include <unordered_map>
@@ -60,14 +58,6 @@ using std::string;
 using std::unordered_map;
 using std::vector;
 
-void set_up_log_format(Logger_sptr logger)
-{
-	int world_rank;
-	MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
-	char format[64];
-	snprintf(format, 64, "[PDI][%06d][%%T] *** %%^%%l%%$: %%v", world_rank);
-	logger->set_pattern(string(format));
-}
 
 Logger_sptr select_log_sinks(PC_tree_t logging_tree)
 {
@@ -97,6 +87,10 @@ Logger_sptr select_log_sinks(PC_tree_t logging_tree)
 	return make_shared<logger>("PDI_logger", sinks.begin(), sinks.end());
 }
 
+} // namespace <anonymous>
+
+namespace PDI {
+
 void read_log_level(Logger_sptr logger, PC_tree_t logging_tree)
 {
 	if (!PC_status(PC_get(logging_tree, ".level"))) {
@@ -119,29 +113,6 @@ void read_log_level(Logger_sptr logger, PC_tree_t logging_tree)
 	}
 }
 
-void configure_single_rank(Logger_sptr logger, PC_tree_t logging_tree)
-{
-	PC_tree_t single_tree = PC_get(logging_tree, ".single");
-	if (!PC_status(single_tree)) {
-		int world_rank;
-		MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
-		
-		int nb_key = len(single_tree);
-		for (int key_id = 0; key_id < nb_key; ++key_id) {
-			PC_tree_t rank_tree = PC_get(single_tree, "[%d]", key_id);
-			int selected_rank = to_long(PC_get(rank_tree, ".rank"), -1);
-			if (selected_rank == world_rank) {
-				read_log_level(logger, rank_tree);
-				break;
-			}
-		}
-	}
-}
-
-} // namespace <anonymous>
-
-namespace PDI {
-
 Logger_sptr configure_logger(PC_tree_t config)
 {
 	PC_tree_t logging_tree = PC_get(config, ".logging");
@@ -152,11 +123,8 @@ Logger_sptr configure_logger(PC_tree_t config)
 	//read default log level
 	read_log_level(logger, logging_tree);
 	
-	//configure log level of single ranks
-	configure_single_rank(logger, logging_tree);
-	
 	//set up final format of logger
-	set_up_log_format(logger);
+	logger->set_pattern("[PDI][%T] *** %^%l%$: %v");
 	
 	return logger;
 }
-- 
GitLab