Index: trunk/FACT++/src/Fits.cc
===================================================================
--- trunk/FACT++/src/Fits.cc	(revision 11404)
+++ trunk/FACT++/src/Fits.cc	(revision 11405)
@@ -73,4 +73,49 @@
 	fDataPointer = dataPointer;
 	fDataNumBytes = numDataBytes; 	
+}
+// --------------------------------------------------------------------------
+//
+//! This looks for a suitable table in the fits file, i.e. that corresponds to the name and column names. (no format check yet)
+//! @param tableName. the base table name to be obtained. If not suitable, numbers are appened to the name
+//! @param allNames. the name of all columns
+//! @param allDataTypes. the data types of all columns
+//! @param allUnits. the units of the columns
+//! @return a pointer to the newly retrieved/created table
+//
+CCfits::Table* Fits::findSuitableTableInFitsFile(const string& tableName,const vector<string>& allNames, const vector<string>& allDataTypes, const vector<string>& allUnits)
+{
+    const multimap< string, CCfits::ExtHDU * >& extMap = fFile->extension();
+    for (int i=0;i<100;i++)
+    {
+        if (i==10)
+            fMess->Error("Already 10 different tables with different formats exist in this file. Please consider re-creating the file entirely (i.e. delete it please)");
+        ostringstream cTableName;
+        cTableName << tableName;
+        if (i != 0)
+            cTableName << "-" << i;
+        //current table name does not exist yet. return its associated fits table newly created
+        if (extMap.find(cTableName.str()) == extMap.end())
+        {
+            for (multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin(); it!= extMap.end(); it++)
+                fMess->Debug(it->first);
+            return fFile->addTable(cTableName.str(), 0, allNames, allDataTypes, allUnits);
+        }
+        CCfits::Table* cTable;
+        cTable = dynamic_cast<CCfits::Table*>(extMap.find(cTableName.str())->second);
+
+        if (!cTable)
+            return NULL;//something wrong happened while getting the table pointer
+
+        //now check that the table columns are the same as the service columns
+        cTable->makeThisCurrent();
+        const map<string, Column*> cMap = cTable->column();
+        for (unsigned int j=0;j<allNames.size();j++)
+            if (cMap.find(allNames[j]) == cMap.end())
+                continue;
+
+        return cTable;
+    }
+    fMess->Error("One hundred trials for new table format failed. aborting");
+    return NULL;
 }
 // --------------------------------------------------------------------------
@@ -152,15 +197,17 @@
                 fFile->read(tryToLoadName);
 
-	    const multimap< string, CCfits::ExtHDU * >& extMap = fFile->extension();
-	    if (extMap.find(tableName) == extMap.end())
-	    {
-	        for (multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin(); it!= extMap.end(); it++)
-	            fMess->Debug(it->first);
-	        fTable = fFile->addTable(tableName, 0, allNames, allDataTypes, allUnits);
-	    }
-	    else
-	    {
-	        fTable = dynamic_cast<CCfits::Table*>(extMap.find(tableName)->second);
-	    }
+//	    const multimap< string, CCfits::ExtHDU * >& extMap = fFile->extension();
+//	    if (extMap.find(tableName) == extMap.end())
+//	    {
+//	        for (multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin(); it!= extMap.end(); it++)
+//	            fMess->Debug(it->first);
+//	        fTable = fFile->addTable(tableName, 0, allNames, allDataTypes, allUnits);
+//	    }
+//	    else
+//	    {
+//	        fTable = dynamic_cast<CCfits::Table*>(extMap.find(tableName)->second);
+//	    }
+        fTable = findSuitableTableInFitsFile(tableName, allNames, allDataTypes, allUnits);
+
 	    if (!fTable)
 	    {
@@ -188,5 +235,5 @@
 			const map<string, Column*> cMap = fTable->column();
 
-                        for (map<string, Column*>::const_iterator cMapIt = cMap.begin(); cMapIt != cMap.end(); cMapIt++)
+            for (map<string, Column*>::const_iterator cMapIt = cMap.begin(); cMapIt != cMap.end(); cMapIt++)
 			{
 				if (!cMapIt->second->isRead())
@@ -349,5 +396,7 @@
 	    delete fFile;
 
-            fMess->Info("Closed: "+fFileName);
+        fMess->Info("Closed: "+fFileName);
+        if (fNumOpenFitsFiles != NULL)
+            (*fNumOpenFitsFiles)--;
 	}
 	fFile = NULL;
@@ -355,6 +404,5 @@
 		delete [] fCopyBuffer;
 	fCopyBuffer = NULL;
-	if (fOwner && fNumOpenFitsFiles != NULL)
-		(*fNumOpenFitsFiles)--;
+
 //fMess is the MessageImp part of the dataLogger itself. Thus it should NOT be deleted by the Fits files destructor.
 //	if (fMess)
Index: trunk/FACT++/src/Fits.h
===================================================================
--- trunk/FACT++/src/Fits.h	(revision 11404)
+++ trunk/FACT++/src/Fits.h	(revision 11405)
@@ -107,4 +107,5 @@
 		template <typename T>
 		bool WriteSingleHeaderKey(const string &name, const T &value, const string &comment);
+		CCfits::Table* findSuitableTableInFitsFile(const string& tableName,const vector<string>& allNames, const vector<string>& allDataTypes, const vector<string>& allUnits);
 
 };//Fits
Index: trunk/FACT++/src/datalogger.cc
===================================================================
--- trunk/FACT++/src/datalogger.cc	(revision 11404)
+++ trunk/FACT++/src/datalogger.cc	(revision 11405)
@@ -506,4 +506,7 @@
 void DataLogger::RemoveService(const string& server, const string& service, bool isCmd)
 {
+    if (fDestructing)//this function is called by the super class, after the destructor has deleted its own subscriptions
+        return;
+
     if (isCmd)
         return;
@@ -1052,29 +1055,59 @@
 {
     if (fDebugIsOn)
-    {
         Debug("DataLogger destruction starts");    
-    }
+
+    //this boolean should not be required anymore, because it
     fDestructing = true;
-    //first let's go to the ready state
-    GoToReadyPlease(); 
+
+    //first, let's backup the datalogger/message service subscription
+    shared_ptr<DimStampedInfo> messageBackup;
+    bool dataloggerMessageFound = false;
+    SubscriptionsListType::iterator x;
+    map<string, SubscriptionType>::iterator y;
+    x = fServiceSubscriptions.find("DATA_LOGGER");
+    if (x != fServiceSubscriptions.end())
+    {
+        y = x->second.find("MESSAGE");
+        if (y != x->second.end())
+        {
+            dataloggerMessageFound = true;
+            messageBackup = y->second.dimInfo;
+        }
+    }
+
     dim_lock();
-    //release the services subscriptions
+
+     //now clear the services subscriptions
     fServiceSubscriptions.clear();
 
     //clear any remaining run number (should remain only one)
     while (fRunNumber.size() > 0)
-     {
+    {
          RemoveOldestRunNumber();
-     }
-//    dim_unlock();
+    }
+    //go to the ready state. i.e. close all files, run-wise first
+    GoToReadyPlease();
+
+    dim_unlock();
+
+    Info("Will soon close the daily log file");
+
     delete fOpenedNightlyFiles;
     delete fOpenedRunFiles;
     delete fNumSubAndFits;
-//    delete fStatsMonitoring;
+
+    //release message service before closing nightly log file
+    if (dataloggerMessageFound)
+        messageBackup.reset();
+
+    if (fNightlyLogFile.is_open())//this file is the only one that has not been closed by GoToReadyPlease
+    {
+        dim_lock();
+        fNightlyLogFile.close();
+        dim_unlock();
+    }
 
     if (fDebugIsOn)
-    {
         Debug("DataLogger desctruction ends");    
-    }
 }
 // --------------------------------------------------------------------------
@@ -1133,8 +1166,4 @@
 void DataLogger::infoHandler()
 {
-    // Make sure getTimestamp is called _before_ getTimestampMillisecs
-    if (fDestructing)
-        return;
-
     DimInfo* I = getInfo();
 
@@ -1360,5 +1389,5 @@
     const bool isItaReport = fmt!="C";
 
-    if (!fNightlyReportFile.is_open())
+    if (!fNightlyLogFile.is_open())
         return;
 
@@ -1481,12 +1510,11 @@
             ostringstream str;
             str << "Parsing service " << sub.dimInfo->getName();
-            str << " failed: " << e.what();
+            str << " failed: " << e.what() << " removing the subscription for now.";
             Error(str);
-//            if (fDebugIsOn)
-//           {
-//              ostringstream str2;
-//             str2 << sub.dimInfo->getString() << "\n";
-//                Debug(str2);
-//            }
+            //remove this subscription from the list.
+            //because these operators use references to elements, and because they're supposed here to erase these objects on the way, I'm not too sure... so duplicate the names !
+            string server = sub.server;
+            string service = sub.service;
+            fServiceSubscriptions.find(server)->second.erase(service);
             return;
         }
@@ -1533,5 +1561,22 @@
     else
     {//write entry to both Nightly and run logs
-        vector<string> strings = sub.fConv->ToStrings(I->getData());
+        vector<string> strings;
+        try
+        {
+           strings = sub.fConv->ToStrings(I->getData());
+        }
+        catch (const runtime_error &e)
+        {
+            ostringstream str;
+            str << "Parsing service " << sub.dimInfo->getName();
+            str << " failed: " << e.what() << " removing the subscription for now.";
+            Error(str);
+            //remove this subscription from the list.
+            //because these operators use references to elements, and because they're supposed here to erase these objects on the way, I'm not too sure... so duplicate the names !
+            string server = sub.server;
+            string service = sub.service;
+            fServiceSubscriptions.find(server)->second.erase(service);
+            return;
+        }
         if (strings.size() > 1)
         {
@@ -2443,5 +2488,5 @@
 //   dim_lock();
    const string baseFileName = CompileFileNameWithPath(fNightlyFilePath, "", "");
-    if (fNightlyLogFile.is_open())
+    if (fNightlyLogFile.is_open() && !fDestructing)//in case we're destructing the datalogger, wait before closing the log file.
     {
         fNightlyLogFile.close();
