Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10886)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10887)
@@ -194,4 +194,19 @@
     }
 
+    void addServiceToOpenedFits(const string& fileName, const string& serviceName)
+    {//most likely I should add this service name.
+     //the only case for which I should not add it is if a service disapeared, hence the file was closed
+     //and reopened again. Unlikely to happen, but well it may
+        bool found = false;
+        for (vector<string>::iterator it=openedFits[fileName].begin(); it!=openedFits[fileName].end(); it++)
+            if (*it == serviceName)
+            {
+                found = true;
+                break;
+            }
+        if (!found)
+            openedFits[fileName].push_back(serviceName);
+    }
+
 };
 ///Dim subscription type. Stores all the relevant info to handle a Dim subscription
@@ -456,6 +471,10 @@
     ///retrieves the size of a file
     off_t GetFileSize(string&);
+    ///Form the base (without extension) name of the nightly text files (logs and reports)
+    void FormBaseNightlyTextFileName(string& name);
     ///Form the name of the nightly text files (logs and reports)
     void FormNightlyTextFileName(string& name, bool isReport);
+    ///Form the base (without extension) name of the run text files (logs and reports)
+    void FormBaseRunTextFileName(string& name, const int runNumber);
     ///Form the name of the run text files (logs and reports)
     void FormRunTextFileName(string& name, bool isReport, const int runNumber);
@@ -478,9 +497,27 @@
     ///Remembers the size of newly opened files. for statistic purposes
     bool RememberFileOrigSizePlease(string& fileName, bool nightly);
+    ///Checks if the input osftream is in error state, and if so close it.
+    void CheckForOfstreamError(ofstream& out);
 }; //DataLogger
 
 // --------------------------------------------------------------------------
 //
-//!
+//! Checks if the given ofstream is in error state and if so, close it
+//! @param out the ofstream that should be checked
+//
+void DataLogger::CheckForOfstreamError(ofstream& out)
+{
+    if (!out.good())
+    {
+        Error("An error occured while writing to a text file. Closing it");
+        if (out.is_open())
+            out.close();
+    }
+}
+// --------------------------------------------------------------------------
+//
+//! Checks the size on disk of a given size, and remembers it in the relevant member variable
+//! @param fileName the file for which the size on disk should be retrieved
+//! @param nightly whether this is a run or nightly file, so that its size is added to the correct member variable
 //
 bool DataLogger::RememberFileOrigSizePlease(string& fileName, bool nightly)
@@ -500,5 +537,7 @@
 // --------------------------------------------------------------------------
 //
-//!
+//! Checks if a given directory is simply '.' and retrieves the coresponding full path
+//! @param dir the input directory that should be checked against
+//! @return the full path corresponding to the input directory
 //
 string DataLogger::CheckIfDirIsDot(const string& dir)
@@ -525,5 +564,7 @@
 // --------------------------------------------------------------------------
 //
-//!
+//! Open a text file and checks for error code
+//! @param stream the ofstream for which the file should be opened
+//! @name the file name
 //
 void DataLogger::OpenTextFilePlease(ofstream& stream, const string& name)
@@ -644,4 +685,17 @@
 // --------------------------------------------------------------------------
 //
+//! Form the base (without extension) name of the run text files (logs and reports)
+//! @param name the full file name
+//! @param runNumber the current run number for which this file should be opened
+//
+void DataLogger::FormBaseRunTextFileName(string& name, const int runNumber)
+{
+    ostringstream sRun;
+    sRun << runNumber;
+
+    name = CheckIfDirIsDot(fRunFilePath) + '/' + sRun.str();
+}
+// --------------------------------------------------------------------------
+//
 //! Form the name of the run text files (logs and reports)
 //! @param name the full file name
@@ -651,8 +705,5 @@
 void DataLogger::FormRunTextFileName(string& name, bool isReport, const int runNumber)
 {
-    ostringstream sRun;
-    sRun << runNumber;
-
-    name = fRunFilePath + '/' + sRun.str();
+    FormBaseRunTextFileName(name, runNumber);
 
     if (isReport)
@@ -663,4 +714,17 @@
 // --------------------------------------------------------------------------
 //
+//! Form the base (without extension) name of the nightly text files (logs and reports)
+//! @param name the full file name
+//
+void DataLogger::FormBaseNightlyTextFileName(string& name)
+{
+    Time time;
+    ostringstream sTime;
+    sTime << time.Y() << "_" << time.M() << "_" << time.D();
+
+    name = CheckIfDirIsDot(fNightlyFilePath) + '/' + sTime.str();
+}
+// --------------------------------------------------------------------------
+//
 //! Form the name of the nightly text files (logs and reports)
 //! @param name the full file name
@@ -669,9 +733,6 @@
 void DataLogger::FormNightlyTextFileName(string& name, bool isReport)
 {
-    Time time;
-    ostringstream sTime;
-    sTime << time.Y() << "_" << time.M() << "_" << time.D();
-
-    name = fNightlyFilePath + '/' + sTime.str();
+    FormBaseNightlyTextFileName(name);
+
     if (isReport)
         name += ".rep";
@@ -1267,7 +1328,7 @@
 
     //TODO this notification scheme might be messed up now.... fix it !
-    ostringstream sRun;
-    sRun << run.runNumber;
-    NotifyOpenedFile(CheckIfDirIsDot(fRunFilePath) + '/' + sRun.str(), 3, fOpenedRunFiles);
+    string baseFileName;
+    FormBaseRunTextFileName(baseFileName, run.runNumber);
+    NotifyOpenedFile(baseFileName, 3, fOpenedRunFiles);
     run.openedFits.clear();
     return 0;
@@ -1426,11 +1487,5 @@
         {
             fNightlyReportFile << header.str() << text << endl;
-            //check if either eof, bailbit or batbit are set
-            if (!fNightlyReportFile.good())
-            {
-                Error("An error occured while writing to the nightly report file. Closing it");
-                if (fNightlyReportFile.is_open())
-                    fNightlyReportFile.close();
-            }
+            CheckForOfstreamError(fNightlyReportFile);
         }
         //write entry to run-report
@@ -1438,10 +1493,5 @@
         {
             *targetRunFile << header.str() << text << endl;
-            if (!targetRunFile->good())
-            {
-                Error("An error occured while writing to the run report file. Closing it.");
-                if (targetRunFile->is_open())
-                    targetRunFile->close();
-            }
+            CheckForOfstreamError(*targetRunFile);
         }
     }
@@ -1456,10 +1506,5 @@
             MessageImp nightlyMess(fNightlyLogFile);
             nightlyMess.Write(cTime, msg.str().c_str(), fQuality);
-            if (!fNightlyLogFile.good())
-            {
-                Error("An error occured while writing to the nightly log file. Closing it.");
-                if (fNightlyLogFile.is_open())
-                    fNightlyLogFile.close();    
-            }
+            CheckForOfstreamError(fNightlyLogFile);
         }
         if (targetRunFile && targetRunFile->is_open())
@@ -1467,10 +1512,5 @@
             MessageImp runMess(*targetRunFile);
             runMess.Write(cTime, msg.str().c_str(), fQuality);
-            if (!targetRunFile->good())
-            {
-                Error("An error occured while writing to the run log file. Closing it.");
-                if (targetRunFile->is_open())
-                    targetRunFile->close();
-            }
+            CheckForOfstreamError(*targetRunFile);
         }
     }
@@ -1864,4 +1904,8 @@
             Debug(str);
         }
+        if (name.size()+1 > FILENAME_MAX)
+        {
+            Error("Provided file name \"" + name + "\" is longer than allowed file name length");
+        }
         OpenFileToDim fToDim;
         fToDim.code = type;
@@ -1907,8 +1951,7 @@
 
     //notify that a new file has been opened.
-    Time time;
-    ostringstream sTime;
-    sTime << time.Y() << "_" << time.M() << "_" << time.D();
-    NotifyOpenedFile(CheckIfDirIsDot(fNightlyFilePath) + '/' + sTime.str(), 3, fOpenedNightlyFiles);
+    string baseFileName;
+    FormBaseNightlyTextFileName(baseFileName);
+    NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles);
 
     fOpenedNightlyFits.clear();
@@ -1968,8 +2011,7 @@
 
         //notify the opening
-        Time time;
-        ostringstream sTime;
-        sTime << time.Y() << "_" << time.M() << "_" << time.D();
-        NotifyOpenedFile(CheckIfDirIsDot(fNightlyFilePath) + '/' + sTime.str(), 7, fOpenedNightlyFiles);
+        string baseFileName;
+        FormBaseNightlyTextFileName(baseFileName);
+        NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles);
         if (fNumSubAndFitsIsOn)
             fNumSubAndFits->updateService();
@@ -2000,16 +2042,6 @@
         else
             if (hasGrouping)
-            {//most likely I should add this service name.
-             //the only case for which I should not add it is if a service disapeared, hence the file was closed
-             //and reopened again. Unlikely to happen, but well it may
-                bool found = false;
-                for (vector<string>::iterator it=cRunNumber->openedFits[fileNameOnly].begin(); it!=cRunNumber->openedFits[fileNameOnly].end(); it++)
-                    if (*it == serviceName)
-                    {
-                        found = true;
-                        break;
-                    }
-                if (!found)
-                    cRunNumber->openedFits[fileNameOnly].push_back(serviceName);
+            {
+             cRunNumber->addServiceToOpenedFits(fileNameOnly, serviceName);
             }
 
@@ -2028,7 +2060,7 @@
             }
 
-        ostringstream sRun;
-        sRun << sub.runNumber;
-        NotifyOpenedFile(CheckIfDirIsDot(fRunFilePath) + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
+        string baseFileName;
+        FormBaseRunTextFileName(baseFileName, sub.runNumber);
+        NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4);
         if (hasGrouping)
             sub.runFile.Open(partialName, serviceName, cRunNumber->runFitsFile, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
@@ -2588,11 +2620,4 @@
 int main(int argc, const char* argv[])
 {
-
-    float salut1 = 1.0f/0.0f;
-    if (salut1 != salut1)
-        cout << "NaN !";
-        else
-            cout << "regular number";
-
     Configuration conf(argv[0]);
     conf.SetPrintUsage(PrintUsage);
