From 2d4fbb987d3499695dc314b8328e3d76901a7210 Mon Sep 17 00:00:00 2001
From: Julien Bigot <julien.bigot@cea.fr>
Date: Thu, 12 Apr 2018 09:36:23 +0200
Subject: [PATCH] Improved and unified logging / error reporting to stderr

---
 plugins/fti/fti.cxx             |  4 ++--
 plugins/user_code/user_code.cxx |  4 ++--
 src/context.cxx                 |  4 ++--
 src/data_descriptor.cxx         | 17 +++++++++--------
 src/pdi.cxx                     |  6 +++---
 5 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/plugins/fti/fti.cxx b/plugins/fti/fti.cxx
index 53820d130..1d920e70c 100644
--- a/plugins/fti/fti.cxx
+++ b/plugins/fti/fti.cxx
@@ -164,7 +164,7 @@ struct fti_plugin: Plugin {
 					FTI_Protect(static_cast<int>(protected_var.second), const_cast<void*>(ref.get()), static_cast<long>(size), FTI_CHAR);
 				} else {
 					FTI_Protect(static_cast<int>(protected_var.second), NULL, 0, FTI_CHAR);
-					cerr << "** Warning: [PDI/FTI] Protected variable "<<protected_var.first<<" unavailable"<<endl;
+					cerr << " *** [PDI/FTI] Warning: Protected variable "<<protected_var.first<<" unavailable"<<endl;
 				}
 			} else {
 				if ( Ref_w ref = context().desc(protected_var.first).ref() ) {
@@ -173,7 +173,7 @@ struct fti_plugin: Plugin {
 					FTI_Protect(static_cast<int>(protected_var.second), ref.get(), static_cast<long>(size), FTI_CHAR);
 				} else {
 					FTI_Protect(static_cast<int>(protected_var.second), NULL, 0, FTI_CHAR);
-					cerr << "** Warning: [PDI/FTI] Protected variable "<<protected_var.first<<" unavailable"<<endl;
+					cerr << " *** [PDI/FTI] Warning: Protected variable "<<protected_var.first<<" unavailable"<<endl;
 				}
 			}
 		}
diff --git a/plugins/user_code/user_code.cxx b/plugins/user_code/user_code.cxx
index a4bebcfe8..bfddf919b 100644
--- a/plugins/user_code/user_code.cxx
+++ b/plugins/user_code/user_code.cxx
@@ -180,9 +180,9 @@ public:
 		try {
 			m_fct();
 		} catch ( const std::exception& e ) {
-			cerr << "while calling user code, caught exception: "<<e.what()<<endl;
+			cerr << " *** [PDI/User-code] Error: while calling user code, caught exception: "<<e.what()<<endl;
 		} catch (...) {
-			cerr << "while calling user code, caught exception"<<endl;
+			cerr << " *** [PDI/User-code] Error: while calling user code, caught exception"<<endl;
 		}
 	}
 	
diff --git a/src/context.cxx b/src/context.cxx
index d5df9ad46..6f480dbb3 100644
--- a/src/context.cxx
+++ b/src/context.cxx
@@ -180,9 +180,9 @@ void Context::event(const char* name)
 			//TODO: concatenate errors in some way
 			//TODO: remove the faulty plugin in case of error?
 		} catch (const exception& e) {
-			cerr << "Error while triggering named event `" << name << "' for plugin `" << elmnt.first << "': " << e.what() << endl;
+			cerr << " *** [PDI] Error: While triggering named event `" << name << "' for plugin `" << elmnt.first << "': " << e.what() << endl;
 		} catch (...) {
-			cerr << "Error while triggering named event `" << name << "' for plugin `" << elmnt.first << "'"<<endl;
+			cerr << " *** [PDI] Error: While triggering named event `" << name << "' for plugin `" << elmnt.first << "'"<<endl;
 		}
 	}
 }
diff --git a/src/data_descriptor.cxx b/src/data_descriptor.cxx
index 22b2fddc1..e2ad704ba 100644
--- a/src/data_descriptor.cxx
+++ b/src/data_descriptor.cxx
@@ -87,16 +87,17 @@ Data_descriptor::Data_descriptor(Data_descriptor&&) = default;
 
 Data_descriptor::~Data_descriptor()
 {
-	if (metadata()) {
-		// release metadata we kept
-		if (!m_refs.empty()) m_refs.pop();
-	}
+	// release metadata copies we kept
+	if ( metadata() && m_refs.size() == 1) m_refs.pop();
+	
 	/* on error, we might be destroyed while not empty. In that case, don't keep
 	 * ownership
 	 */
-	while (!m_refs.empty()) {
-		reclaim();
+	if (!m_refs.empty()) {
+		std::cerr<<" *** [PDI] Warning: Remaining "<<m_refs.size()<<" reference(s) to `"<<m_name<<"' in PDI after program end"<<std::endl;
 	}
+	if ( metadata()  ) while (!m_refs.empty()) m_refs.pop();
+	if ( !metadata() ) while (!m_refs.empty()) reclaim();
 }
 
 void Data_descriptor::creation_template(PC_tree_t config)
@@ -178,9 +179,9 @@ void* Data_descriptor::share(Ref data_ref, bool read, bool write)
 			//TODO: concatenate errors in some way
 			//TODO: remove the faulty plugin in case of error?
 		} catch (const std::exception& e) {
-			cerr << "Error while triggering data event `" << m_name << "' for plugin `" << elmnt.first << "': " << e.what() << endl;
+			cerr << " *** [PDI] Error: While triggering data event `" << m_name << "' for plugin `" << elmnt.first << "': " << e.what() << endl;
 		} catch (...) {
-			cerr << "Error while triggering data event `" << m_name << "' for plugin `" << elmnt.first <<"'"<< endl;
+			cerr << " *** [PDI] Error: While triggering data event `" << m_name << "' for plugin `" << elmnt.first <<"'"<< endl;
 		}
 	}
 	
diff --git a/src/pdi.cxx b/src/pdi.cxx
index d27112673..5ebbf5209 100644
--- a/src/pdi.cxx
+++ b/src/pdi.cxx
@@ -29,7 +29,6 @@
 #include "config.h"
 
 #include <cstddef>
-#include <cstring>
 #include <exception>
 #include <iostream>
 #include <string>
@@ -125,7 +124,7 @@ PDI_inout_t operator&(PDI_inout_t a, PDI_inout_t b)
 void assert_status(PDI_status_t status, const char* message, void*)
 {
 	if (status) {
-		fprintf(stderr, "FATAL ERROR, in PDI: %s\n", message);
+		cerr<<" *** [PDI] Error: "<<message<<endl;
 		abort();
 	}
 }
@@ -135,7 +134,7 @@ void assert_status(PDI_status_t status, const char* message, void*)
 void warn_status(PDI_status_t status, const char* message, void*)
 {
 	if (status) {
-		fprintf(stderr, "Warning, in PDI: %s\n", message);
+		cerr<<" *** [PDI] Error: "<<message<<endl;
 	}
 }
 
@@ -343,6 +342,7 @@ try
 {
 	Paraconf_wrapper fw;
 	if (PDI_status_t status = PDI_share(name, data, access)) return status;
+	
 	if (! g_transaction.empty()) {   // defer the reclaim
 		g_transaction_data.emplace(name);
 	} else { // do the reclaim now
-- 
GitLab