Index: trunk/FACT++/src/Fits.cc
===================================================================
--- trunk/FACT++/src/Fits.cc	(revision 10954)
+++ trunk/FACT++/src/Fits.cc	(revision 10955)
@@ -84,5 +84,5 @@
 //! @param runNumber the runNumber for which this file is opened. 0 means nightly file.
 //
-void Fits::Open(const string& fileName, const string& tableName, FITS* file, int* fitsCounter, MessageImp* out, int runNumber)
+bool Fits::Open(const string& fileName, const string& tableName, FITS* file, int* fitsCounter, MessageImp* out, int runNumber)
 {		
 //	if (fMess)
@@ -104,5 +104,5 @@
 			fMess->Error(str);
 			fFile = NULL;
-			return;
+			return false;
 		}	
 		fOwner = true;
@@ -166,5 +166,6 @@
 	    {
 	        fMess->Error("The table " + factTableName + " could not be created nor loaded. skipping it");
-	        return;
+	        Close();
+	        return false;
 	    }
 	    fTable->makeThisCurrent();
@@ -177,5 +178,6 @@
 			{
 				fMess->Error("The table " + factTableName + " could not be converted to a binary table. skipping");
-				return;
+				Close();
+				return false;
 			}	
 			//read the table binary data.
@@ -192,5 +194,6 @@
 				{
 					fMess->Error("Column " + cMapIt->first + "Could not be read back from the disk");
-					return;	
+					Close();
+					return false;
 				}	
 			}
@@ -205,4 +208,6 @@
 		fMess->Error(str);
 		fTable = NULL;
+		Close();
+		return false;
 	}
 			
@@ -210,5 +215,7 @@
 	fRefMjD = 0;//* static_cast<double*>(fStandardPointers[0]);
 	if (!updating)
-		WriteHeaderKeys();
+		return WriteHeaderKeys();
+
+	return true;
 }
 // --------------------------------------------------------------------------
@@ -219,5 +226,5 @@
 //!@param a comment explaining the meaning of the key
 template <typename T>
-void Fits::WriteSingleHeaderKey(string name, T value, string comment)
+bool Fits::WriteSingleHeaderKey(string name, T value, string comment)
 {
 	try
@@ -230,5 +237,7 @@
 		str << "Could not add header keys in file " << fFileName << " reason: " << e.message();
 		fMess->Error(str);
-	}
+		return false;
+	}
+	return true;
 }
 // --------------------------------------------------------------------------
@@ -236,8 +245,8 @@
 //! This writes the standard header 
 //
-void Fits::WriteHeaderKeys()
+bool Fits::WriteHeaderKeys()
 {
 	if (!fTable)
-		return;
+		return false;
 	string name;
 	string comment;
@@ -245,16 +254,17 @@
 	string stringValue;
 
-	WriteSingleHeaderKey("EXTREL", 1.0f, "Release Number");
-	WriteSingleHeaderKey("TELESCOP", "FACT", "Telescope that acquired this data");
-	WriteSingleHeaderKey("ORIGIN", "ISDC", "Institution that wrote the file");
-	WriteSingleHeaderKey("CREATOR", "FACT++ DataLogger", "Program that wrote this file");
+	if (!WriteSingleHeaderKey("EXTREL", 1.0f, "Release Number")) return false;
+	if (!WriteSingleHeaderKey("TELESCOP", "FACT", "Telescope that acquired this data")) return false;
+	if (!WriteSingleHeaderKey("ORIGIN", "ISDC", "Institution that wrote the file")) return false;
+	if (!WriteSingleHeaderKey("CREATOR", "FACT++ DataLogger", "Program that wrote this file")) return false;
 	stringValue = Time().GetAsStr();
 	stringValue[10]= 'T';
-	WriteSingleHeaderKey("DATE", stringValue, "File creation data");
-	WriteSingleHeaderKey("TIMESYS", "TT", "Time frame system");
-	WriteSingleHeaderKey("TIMEUNIT", "d", "Time unit");
-	WriteSingleHeaderKey("TIMEREF", "UTC", "Time reference frame");
-	WriteSingleHeaderKey("MJDREF", fRefMjD, "Modified Julian Date of origin");
-    WriteSingleHeaderKey("TSTOP", fEndMjD, "Time of the last receied data");
+	if (!WriteSingleHeaderKey("DATE", stringValue, "File creation data")) return false;
+	if (!WriteSingleHeaderKey("TIMESYS", "TT", "Time frame system")) return false;
+	if (!WriteSingleHeaderKey("TIMEUNIT", "d", "Time unit")) return false;
+	if (!WriteSingleHeaderKey("TIMEREF", "UTC", "Time reference frame")) return false;
+	if (!WriteSingleHeaderKey("MJDREF", fRefMjD, "Modified Julian Date of origin")) return false;
+	if (!WriteSingleHeaderKey("TSTOP", fEndMjD, "Time of the last receied data")) return false;
+    return true;
 }
 // --------------------------------------------------------------------------
@@ -263,5 +273,5 @@
 //! @param conv the converter corresponding to the service being logged
 //
-void Fits::Write(Converter* conv)
+bool Fits::Write(Converter* conv)
 {
     //first copy the standard variables to the copy buffer
@@ -284,5 +294,5 @@
         str << fFileName << ": " << e.what();
         fMess->Error(str);
-        return;
+        return false;
     }
 
@@ -295,5 +305,6 @@
         str << "Inserting row into " << fFileName << " failed (fits_insert_rows, rc=" << status << ")";
         fMess->Error(str);
-        // FIXME: What is a proper action here?
+        Close();
+        return false;
     }
 
@@ -310,6 +321,5 @@
 
     //data copied to buffer, can write to fits
-    fits_write_tblbytes(fFile->fitsPointer(), fNumRows, 1, fTotalNumBytes, fCopyBuffer, &status);
-    if (status)
+    if (fits_write_tblbytes(fFile->fitsPointer(), fNumRows, 1, fTotalNumBytes, fCopyBuffer, &status))
     {
         char text[30];//max length of cfitsio error strings (from doc)
@@ -318,6 +328,8 @@
         str << "Writing FITS row " << fNumRows << " in " << fFileName << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
         fMess->Error(str);
-        // FIXME: What is a proper action here?
-    }
+        Close();
+        return false;
+    }
+    return true;
 }
 
Index: trunk/FACT++/src/Fits.h
===================================================================
--- trunk/FACT++/src/Fits.h	(revision 10954)
+++ trunk/FACT++/src/Fits.h	(revision 10955)
@@ -50,5 +50,5 @@
 		double fRefMjD;
 		///Write the FITS header keys
-		void WriteHeaderKeys();
+		bool WriteHeaderKeys();
 public:
 		///Name of the openned file. For querying stats
@@ -94,8 +94,8 @@
 
 		///Opens a FITS file
-        void Open(const string& fileName, const string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out, int runNumber);//ostream& out);
+        bool Open(const string& fileName, const string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out, int runNumber);//ostream& out);
 
 		///Write one line of data. Use the given converter.
-		void Write(Converter* conv);
+		bool Write(Converter* conv);
 		
 		///Close the currently opened file.
@@ -106,5 +106,5 @@
 private:
 		template <typename T>
-		void WriteSingleHeaderKey(string name, T value, string comment);
+		bool WriteSingleHeaderKey(string name, T value, string comment);
 
 };//Fits
Index: trunk/FACT++/src/dataLogger.cc
===================================================================
--- trunk/FACT++/src/dataLogger.cc	(revision 10954)
+++ trunk/FACT++/src/dataLogger.cc	(revision 10955)
@@ -199,4 +199,5 @@
         kSM_BadNightlyConfig = 0x101, ///< the folder specified for Nightly logging does not exist or has bad permissions
         kSM_BadRunConfig = 0x102, ///<  the folder specified for the run logging does not exist or has wrong permissions or no run number
+        kSM_WriteError = 0x103, ///< Denotes that an error occured while writing a file (text or fits).
     } localstates_t;
     
@@ -297,4 +298,6 @@
     ///from NightlyOpen to waiting transition
     int NightlyToWaitRunPlease(); 
+    /// from writing to error
+    std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="");
 #ifdef HAVE_FITS
     ///Open fits files
@@ -560,4 +563,5 @@
         if (out.is_open())
             out.close();
+        SetCurrentState(kSM_WriteError);
     }
 }
@@ -928,4 +932,5 @@
     AddStateName(kSM_BadNightlyConfig, "ErrNightlyFolder", "The folder for the nighly summary files is invalid.");
     AddStateName(kSM_BadRunConfig,     "ErrRunFolder",     "The folder for the run files is invalid.");
+    AddStateName(kSM_WriteError, "ErrWrite", "An error occured while writing to a file.");
 
     /*Add the possible transitions for this machine*/
@@ -934,5 +939,5 @@
             ("Start the nightly logging. Nightly file location must be specified already");
 
-    AddEvent(kSM_Ready, fTransStop, kSM_NightlyOpen, kSM_WaitingRun, kSM_Logging)
+    AddEvent(kSM_Ready, fTransStop, kSM_NightlyOpen, kSM_WaitingRun, kSM_Logging, kSM_WriteError)
             (boost::bind(&DataLogger::GoToReadyPlease, this))
             ("Stop all data logging, close all files.");
@@ -946,5 +951,5 @@
             ("Wait for a run to be started, open run-files as soon as a run number arrives.");
 
-    AddEvent(kSM_Ready, fTransReset, kSM_Error, kSM_BadNightlyConfig, kSM_BadRunConfig, kSM_Error)
+    AddEvent(kSM_Ready, fTransReset, kSM_Error, kSM_BadNightlyConfig, kSM_BadRunConfig, kSM_WriteError)
             (boost::bind(&DataLogger::GoToReadyPlease, this))
             ("Transition to exit error states. Closes the nightly file if already opened.");
@@ -970,5 +975,5 @@
      //Provide a print command
      ostringstream str;
-     str <<     kSM_Ready << " " << kSM_NightlyOpen << " " << kSM_WaitingRun << " " << kSM_Logging << " " << kSM_BadNightlyConfig;
+     str <<     kSM_Ready << " " << kSM_NightlyOpen << " " << kSM_WaitingRun << " " << kSM_Logging << " " << kSM_BadNightlyConfig << " " << kSM_WriteError;
      str << " " << kSM_BadRunConfig;
      AddEvent(fPrintCommand, str.str().c_str(), "")
@@ -1309,5 +1314,5 @@
 
     //create the converter for that service
-    if (sub.fConv && isItaReport)
+    if ((!sub.fConv.get()) && isItaReport)
     {
         //trick the converter in case of 'C'. why do I do this ? well simple: the converter checks that the right number
@@ -1851,6 +1856,9 @@
             fOpenedNightlyFits[fileNameOnly].push_back(serviceName);
 
-        sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, 0);//Out());
-
+        if (!sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, 0))
+        {
+            SetCurrentState(kSM_WriteError);
+            return;
+        }
         //notify the opening
         string baseFileName = CompileFileName(fNightlyFilePath, "", "");
@@ -1889,5 +1897,5 @@
             }
 
-        if (hasGrouping && cRunNumber->runFitsFile)
+        if (hasGrouping && (!cRunNumber->runFitsFile.get()))
             try
             {
@@ -1906,8 +1914,19 @@
         NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4);
         if (hasGrouping)
-            sub.runFile.Open(partialName, serviceName, (cRunNumber->runFitsFile).get(), &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
+        {
+            if (!sub.runFile.Open(partialName, serviceName, (cRunNumber->runFitsFile).get(), &fNumSubAndFitsData.numOpenFits, this, sub.runNumber))
+            {
+                SetCurrentState(kSM_WriteError);
+                return;
+            }
+        }
         else
-            sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
-
+        {
+            if (sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber))
+            {
+                SetCurrentState(kSM_WriteError);
+                return;
+            }
+        }
        if (fNumSubAndFitsIsOn)
            fNumSubAndFits->updateService();
@@ -2010,20 +2029,21 @@
         if (sub.nightlyFile.IsOpen())
         {
-            sub.nightlyFile.Write(sub.fConv.get());
-            if (fDebugIsOn)
-            {
-                Debug("Writing to nightly FITS " + sub.nightlyFile.fFileName);
-            }
-        }
+            if (!sub.nightlyFile.Write(sub.fConv.get()))
+                SetCurrentState(kSM_WriteError);
+         }
         if (sub.runFile.IsOpen())
         {
-            sub.runFile.Write(sub.fConv.get());
-            if (fDebugIsOn)
-            {
-                Debug("Writing to Run FITS " + sub.runFile.fFileName);    
-            }
+            if (!sub.runFile.Write(sub.fConv.get()))
+                SetCurrentState(kSM_WriteError);
         }
 }
 #endif //if has_fits
+
+std::string DataLogger::SetCurrentState(int state, const char *txt, const std::string &cmd)
+{
+    if (state == kSM_WriteError && GetCurrentState() == kSM_WriteError)
+        return "";
+    return StateMachineImp::SetCurrentState(state, txt, cmd);
+}
 // --------------------------------------------------------------------------
 //
