From 40337e0ac1524db754fd858af001da416de012b3 Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Wed, 29 Jan 2025 03:42:54 +0100
Subject: [PATCH] current_exception

---
 src/Fug.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 src/Fug.h   |  1 +
 2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/src/Fug.cpp b/src/Fug.cpp
index 4ca0721..a778426 100644
--- a/src/Fug.cpp
+++ b/src/Fug.cpp
@@ -127,6 +127,22 @@ void Fug::delete_device() {
 
   delete mutex;
 }
+/*
+ * timestamp() 
+ * send a timestamp to standard output
+ */
+void Fug::msg2err(const char *msg) 
+{
+	struct tm *mytime;
+	char timeLong[80];
+	time_t myTime_t;
+	myTime_t = time(NULL);
+	mytime = localtime(&myTime_t);
+	// memset(mytime, 0, sizeof(tmTemp));
+	strftime(timeLong, 79, "%Y-%m-%d %H:%M:%S ", mytime);
+	// strftime(timeShort, 79, "%H:%M:%S ", mytime);
+	cerr << timeLong << msg << endl;
+}
 
 //+----------------------------------------------------------------------------
 //
@@ -829,6 +845,11 @@ void Fug::write_Fug(string input) {
         e, static_cast<const char *>(rsn), out_stream.str(),
         static_cast<const char *>("SerialDeviceServer error"), Tango::ERR);
   }
+  catch (...) {
+    std::exception_ptr p = std::current_exception();
+    std::cerr <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
+    msg2err("DevSerWriteString write_Fug");
+  }
 
   /*printf("write %s: %s\n",device_name.c_str(),input.c_str());*/
 
@@ -879,6 +900,11 @@ string Fug::read_Fug() {
         e, static_cast<const char *>(rsn), out_stream.str(),
         static_cast<const char *>("SerialDeviceServer error"), Tango::ERR);
   }
+  catch (...) {
+    std::exception_ptr p = std::current_exception();
+    std::cerr <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
+    msg2err("read_Fug DevSerWriteString");
+  }
 
   omni_thread::sleep(
       0, sleeptime * 1000000);  // convert millisecond to microsecond
@@ -894,6 +920,11 @@ string Fug::read_Fug() {
         e, static_cast<const char *>(rsn), out_stream.str(),
         static_cast<const char *>("SerialDeviceServer error"), Tango::ERR);
   }
+  catch (...) {
+    std::exception_ptr p = std::current_exception();
+    std::cerr <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
+    msg2err("read_Fug DevSerReadRaw");
+  }
   Dout >> strout;
 
   if (strout.length() == 0) {
@@ -910,13 +941,20 @@ string Fug::read_Fug() {
 
   omni_thread::sleep(0, FUG_SER_SLEEP * 1000000);
 
-  i = 0;
-  while (strout[i] == 'E') {
-    if (strout[i+3] == '\n' || strout[i+3] == '\r' || strout[i+3] == '\0') i++;
-    i += 3;
+  try {
+    i = 0;
+    while (strout[i] == 'E') {
+      if (strout[i+3] == '\n' || strout[i+3] == '\r' || strout[i+3] == '\0') i++;
+      i += 3;
+    }
+    strout_filt.resize(strout.length() - i);
+    strncpy((char *)strout_filt.c_str(), (char *)strout.c_str() + i, strout.length() - i);
+  }
+  catch (...) {
+    std::exception_ptr p = std::current_exception();
+    std::cerr <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
+    msg2err("read_Fug misc");
   }
-  strout_filt.resize(strout.length() - i);
-  strncpy((char *)strout_filt.c_str(), (char *)strout.c_str() + i, strout.length() - i);
 
   /*printf("read %s: %s\n",device_name.c_str(),strout_filt.c_str());*/
 
diff --git a/src/Fug.h b/src/Fug.h
index ca2293d..6ad16ac 100644
--- a/src/Fug.h
+++ b/src/Fug.h
@@ -321,6 +321,7 @@ public :
 friend class fugthread;
 
 private :
+	void msg2err(const char *msg);
 	Tango::DeviceProxy *devserial;
 	int abortflag;  // flag used to abort loops (On,Off,Standby)	
 	bool FugStat[5];	
-- 
GitLab