Changeset 11089
- Timestamp:
- 06/21/11 13:29:48 (13 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/Fits.cc
r11015 r11089 299 299 { 300 300 ostringstream str; 301 str << "Inserting row into " << fFileName << " failed (fits_insert_rows, rc=" << status << ")"; 301 char text[30]; 302 fits_get_errstatus(status, text); 303 str << "Inserting row into " << fFileName << " failed (fits_insert_rows, rc=" << status << "), Message: " << text; 302 304 fMess->Error(str); 303 305 Close(); -
trunk/FACT++/src/datalogger.cc
r11074 r11089 221 221 ///run numbers 222 222 list<RunNumberType> fRunNumber; 223 ///old run numbers time-out delay (in minutes)223 ///old run numbers time-out delay (in seconds) 224 224 long fRunNumberTimeout; 225 225 ///previous run number. to check if changed while logging … … 362 362 void AppendYearMonthDaytoPath(string& path); 363 363 ///Form the files path 364 string CompileFileName(const string &path, const string &service, const string & extension, const Time &time=Time()); 364 string CompileFileNameWithPath(const string &path, const string &service, const string & extension, const Time &time=Time()); 365 ///Form the file names only 366 string CompileFileName(const string& service, const string& extension, const Time& time=Time()); 365 367 ///Form the files path 366 string CompileFileName(const string &path, uint32_t run, const string &service, const string & extension, const Time &time=Time()); 368 string CompileFileNameWithPath(const string &path, const uint32_t run, const string &service, const string & extension, const Time &time=Time()); 369 ///Form the file names only 370 string CompileFileName(const uint32_t run, const string& service, const string& extension);//, const Time& time=Time()); 367 371 ///Check whether service is in black and/or white list 368 372 bool ShouldSubscribe(const string& server, const string& service); … … 376 380 bool RememberFileOrigSizePlease(string& fileName, bool nightly); 377 381 ///Checks if the input osftream is in error state, and if so close it. 378 void CheckForOfstreamError(ofstream& out); 382 bool CheckForOfstreamError(ofstream& out); 383 ///Goes to Write error state and also closes all opened files 384 void GoToWriteErrorState(); 379 385 ///Checks if a given path exist 380 386 bool DoesPathExist(string path); … … 550 556 //! @param out the ofstream that should be checked 551 557 // 552 voidDataLogger::CheckForOfstreamError(ofstream& out)558 bool DataLogger::CheckForOfstreamError(ofstream& out) 553 559 { 554 560 if (out.good()) 555 return ;561 return true; 556 562 557 563 Error("An error occured while writing to a text file. Closing it"); 558 if (out.is_open()) 559 out.close(); 560 SetCurrentState(kSM_WriteError); 564 // if (out.is_open()) 565 // out.close(); 566 GoToWriteErrorState(); 567 568 return false; 569 // SetCurrentState(kSM_WriteError); 561 570 } 562 571 // -------------------------------------------------------------------------- … … 648 657 //! @param extension the extension to add, if any 649 658 // 650 string DataLogger::CompileFileName(const string &path, const string &service, const string & extension, const Time &time) 651 { 652 ostringstream str; 653 //calculate time suitable for naming files. 654 const Time ftime(time-boost::posix_time::time_duration(12,0,0)); 655 656 //output it 657 str << path << Time::fmt("/%Y/%m/%d") << ftime; 658 659 //check if target directory exist 660 if (!DoesPathExist(str.str())) 661 CreateDirectory(str.str()); 662 663 //output base of file name 664 str << Time::fmt("/%Y_%m_%d") << ftime; 665 666 //output service name 667 if (!service.empty()) 668 str << "_" << service; 669 670 //output appropriate extension 671 if (!extension.empty()) 672 str << "." << extension; 673 674 return str.str(); 659 //string DataLogger::CompileFileName(const string &path, const string &service, const string & extension, const Time &time) 660 string DataLogger::CompileFileName(const string& service, const string& extension, const Time& time) 661 { 662 ostringstream str; 663 //calculate time suitable for naming path. 664 const Time ftime(time-boost::posix_time::time_duration(12,0,0)); 665 666 //output base of file name 667 str << Time::fmt("%Y_%m_%d") << ftime; 668 669 //output service name 670 if (!service.empty()) 671 str << "_" << service; 672 673 //output appropriate extension 674 if (!extension.empty()) 675 str << "." << extension; 676 677 return str.str(); 678 } 679 680 string DataLogger::CompileFileNameWithPath(const string& path, const string& service, const string& extension, const Time& time) 681 { 682 ostringstream str; 683 //calculate time suitable for naming files. 684 const Time ftime(time-boost::posix_time::time_duration(12,0,0)); 685 686 //output it 687 str << path << Time::fmt("/%Y/%m/%d") << ftime; 688 689 //check if target directory exist 690 if (!DoesPathExist(str.str())) 691 CreateDirectory(str.str()); 692 693 str << '/' << CompileFileName(service, extension, time); 694 695 return str.str(); 696 697 675 698 } 676 699 // -------------------------------------------------------------------------- … … 683 706 //! @param extension the extension to add, if any 684 707 // 685 string DataLogger::CompileFileName(const string &path, uint32_t run, const string &service, const string & extension, const Time &time) 708 //string DataLogger::CompileFileName(const string &path, uint32_t run, const string &service, const string & extension, const Time &time) 709 string DataLogger::CompileFileName(const uint32_t run, const string& service, const string& extension)//, const Time& time) 686 710 { 687 711 ostringstream str; 688 //calculate suitable time for naming files and output it689 str << path << Time::fmt("/%Y/%m/%d") << (time-boost::posix_time::time_duration(12,0,0));690 691 //check if target directory exist692 if (!DoesPathExist(str.str()))693 CreateDirectory(str.str());694 695 712 //output base of file name 696 str << '/' <<setfill('0') << setw(8) << run;713 str << setfill('0') << setw(8) << run; 697 714 698 715 //output service name … … 706 723 } 707 724 725 string DataLogger::CompileFileNameWithPath(const string& path, const uint32_t run, const string& service, const string& extension, const Time& time) 726 { 727 ostringstream str; 728 //calculate suitable time for naming files and output it 729 str << path << Time::fmt("/%Y/%m/%d") << (time-boost::posix_time::time_duration(12,0,0)); 730 731 //check if target directory exist 732 if (!DoesPathExist(str.str())) 733 CreateDirectory(str.str()); 734 735 str << '/' << CompileFileName(run, service, extension);//, time); 736 737 return str.str(); 738 739 } 708 740 // -------------------------------------------------------------------------- 709 741 // … … 1037 1069 fBaseSizeRun = 0; 1038 1070 fDailyFileDayChangedAlready = true; 1039 fRunNumberTimeout = 1;1071 fRunNumberTimeout = 60; //default run-timeout set to 1 minute 1040 1072 if(fDebugIsOn) 1041 1073 { … … 1114 1146 const Time cTime = Time(); 1115 1147 1116 if ((cTime - fPreviousOldRunNumberCheck).total_seconds() < fRunNumberTimeout *60)1148 if ((cTime - fPreviousOldRunNumberCheck).total_seconds() < fRunNumberTimeout) 1117 1149 return; 1118 1150 1119 while (fRunNumber.size() > 1 && (cTime - fRunNumber.back().time) > boost::posix_time:: minutes(fRunNumberTimeout))1151 while (fRunNumber.size() > 1 && (cTime - fRunNumber.back().time) > boost::posix_time::seconds(fRunNumberTimeout)) 1120 1152 { 1121 1153 RemoveOldestRunNumber(); … … 1230 1262 1231 1263 // open report file 1232 run.reportName = CompileFileName (fRunFilePath, run.runNumber, "", "rep");1264 run.reportName = CompileFileNameWithPath(fRunFilePath, run.runNumber, "", "rep"); 1233 1265 if (!OpenStream(run.reportFile, run.reportName)) 1234 1266 return -1; … … 1241 1273 1242 1274 //TODO this notification scheme might be messed up now.... fix it ! 1243 const string baseFileName = CompileFileName (fRunFilePath, run.runNumber, "", "");1275 const string baseFileName = CompileFileNameWithPath(fRunFilePath, run.runNumber, "", ""); 1244 1276 NotifyOpenedFile(baseFileName, 3, fOpenedRunFiles); 1245 1277 run.openedFits.clear(); … … 1278 1310 return; 1279 1311 //open the log and report files 1280 OpenRunFile(fRunNumber.back()); 1312 if (OpenRunFile(fRunNumber.back()) != 0) 1313 {//an error occured. close current run files and go to error state 1314 for (list<RunNumberType>::iterator it=fRunNumber.begin(); it != fRunNumber.end(); it++) 1315 { 1316 if (it->reportFile->is_open()) 1317 it->reportFile->close(); 1318 #ifdef RUN_LOGS 1319 if (it->logFile->is_open()) 1320 it->logFile->close(); 1321 #endif 1322 } 1323 StopRunPlease(); 1324 SetCurrentState(kSM_BadRunConfig); 1325 } 1281 1326 } 1282 1327 // -------------------------------------------------------------------------- … … 1330 1375 { 1331 1376 if (fDebugIsOn) 1332 Debug("Its Noon! Closing and reopening daily text files");1377 Debug("Its Noon! Closing and reopening nightly text files"); 1333 1378 1334 1379 fNightlyLogFile.close(); … … 1338 1383 Info("Closed: "+fFullNightlyReportFileName); 1339 1384 1340 fFullNightlyLogFileName = CompileFileName(fNightlyFilePath, "", "log"); 1341 OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName); 1342 //FIXME: Handle return code properly! 1343 1344 fFullNightlyReportFileName = CompileFileName(fNightlyFilePath, "", "rep"); 1345 OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName); 1346 //FIXME: Handle return code properly! 1385 fFullNightlyLogFileName = CompileFileNameWithPath(fNightlyFilePath, "", "log"); 1386 if (!OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName)) 1387 { 1388 GoToReadyPlease(); 1389 SetCurrentState(kSM_BadNightlyConfig); 1390 return; 1391 } 1392 1393 fFullNightlyReportFileName = CompileFileNameWithPath(fNightlyFilePath, "", "rep"); 1394 if (!OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName)) 1395 { 1396 GoToReadyPlease(); 1397 SetCurrentState(kSM_BadNightlyConfig); 1398 return; 1399 } 1347 1400 1348 1401 fDailyFileDayChangedAlready = true; … … 1354 1407 if ((!sub.fConv.get()) && isItaReport) 1355 1408 { 1356 //trick the converter in case of 'C'. why do I do this ? well simple: the converter checks that the right number1357 //of bytes was written. because I skip 'C' with fits, the bytes will not be allocated, hence the "size copied ckeck"1358 //of the converter will fail, hence throwing an exception.1359 string fakeFormat(I->getFormat());1360 if (fakeFormat[fakeFormat.size()-1] == 'C')1361 fakeFormat = fakeFormat.substr(0, fakeFormat.size()-1);1362 1409 sub.fConv = shared_ptr<Converter>(new Converter(Out(), I->getFormat())); 1363 1410 if (!sub.fConv) … … 1449 1496 { 1450 1497 fNightlyReportFile << header.str() << text << endl; 1451 CheckForOfstreamError(fNightlyReportFile); 1498 if (!CheckForOfstreamError(fNightlyReportFile)) 1499 return; 1452 1500 } 1453 1501 //write entry to run-report … … 1455 1503 { 1456 1504 *targetRunFile << header.str() << text << endl; 1457 CheckForOfstreamError(*targetRunFile); 1505 if (!CheckForOfstreamError(*targetRunFile)) 1506 return; 1458 1507 } 1459 1508 } … … 1466 1515 { 1467 1516 MessageImp(fNightlyLogFile).Write(cTime, msg.str().c_str(), fQuality); 1468 CheckForOfstreamError(fNightlyLogFile); 1517 if (!CheckForOfstreamError(fNightlyLogFile)) 1518 return; 1469 1519 } 1470 1520 if (targetRunFile && targetRunFile->is_open()) 1471 1521 { 1472 1522 MessageImp(*targetRunFile).Write(cTime, msg.str().c_str(), fQuality); 1473 CheckForOfstreamError(*targetRunFile); 1523 if (!CheckForOfstreamError(*targetRunFile)) 1524 return; 1474 1525 } 1475 1526 } … … 1522 1573 //timeout value 1523 1574 str.str(""); 1524 str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " minute(s)";1575 str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " seconds"; 1525 1576 Message(str); 1526 1577 … … 1731 1782 1732 1783 ostringstream str; 1733 str << "Timeout delay for old run numbers is now " << fRunNumberTimeout ;1784 str << "Timeout delay for old run numbers is now " << fRunNumberTimeout << " seconds"; 1734 1785 Message(str); 1735 1786 … … 1846 1897 Debug("Starting..."); 1847 1898 } 1848 fFullNightlyLogFileName = CompileFileName (fNightlyFilePath, "", "log");1899 fFullNightlyLogFileName = CompileFileNameWithPath(fNightlyFilePath, "", "log"); 1849 1900 if (!OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName)) 1850 1901 return kSM_BadNightlyConfig; 1851 1902 1852 1903 1853 fFullNightlyReportFileName = CompileFileName (fNightlyFilePath, "", "rep");1904 fFullNightlyReportFileName = CompileFileNameWithPath(fNightlyFilePath, "", "rep"); 1854 1905 if (!OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName)) 1906 { 1907 fNightlyLogFile.close(); 1855 1908 return kSM_BadNightlyConfig; 1856 1909 } 1857 1910 //get the size of the newly opened file. 1858 1911 fBaseSizeNightly = GetFileSize(fFullNightlyLogFileName); … … 1863 1916 1864 1917 //notify that a new file has been opened. 1865 const string baseFileName = CompileFileName (fNightlyFilePath, "", "");1918 const string baseFileName = CompileFileNameWithPath(fNightlyFilePath, "", ""); 1866 1919 NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles); 1867 1920 … … 1901 1954 } 1902 1955 } 1903 hasGrouping = true;1904 1956 for (unsigned int i=0;i<serviceName.size(); i++) 1905 1957 { … … 1913 1965 if (!sub.nightlyFile.IsOpen()) 1914 1966 { 1915 string partialName = CompileFileName (fNightlyFilePath, serviceName, "fits");1967 string partialName = CompileFileNameWithPath(fNightlyFilePath, serviceName, "fits"); 1916 1968 1917 1969 const string fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size()); … … 1928 1980 if (!sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, 0)) 1929 1981 { 1930 SetCurrentState(kSM_WriteError); 1982 GoToWriteErrorState(); 1983 //SetCurrentState(kSM_WriteError); 1931 1984 return; 1932 1985 } 1933 1986 //notify the opening 1934 const string baseFileName = CompileFileName (fNightlyFilePath, "", "");1987 const string baseFileName = CompileFileNameWithPath(fNightlyFilePath, "", ""); 1935 1988 NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles); 1936 1989 if (fNumSubAndFitsIsOn) … … 1944 1997 if (hasGrouping) 1945 1998 { 1946 partialName = CompileFileName (fRunFilePath, sub.runNumber, "", "fits");1999 partialName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, "", "fits"); 1947 2000 fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size()); 1948 2001 } 1949 2002 else 1950 2003 { 1951 partialName = CompileFileName (fRunFilePath, sub.runNumber, serviceName, "fits");2004 partialName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, serviceName, "fits"); 1952 2005 fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size()); 1953 2006 } … … 1973 2026 Error(str); 1974 2027 cRunNumber->runFitsFile = shared_ptr<CCfits::FITS>();//NULL; 2028 GoToWriteErrorState(); 2029 //SetCurrentState(kSM_WriteError); 2030 return; 1975 2031 } 1976 2032 1977 const string baseFileName = CompileFileName (fRunFilePath, sub.runNumber, "", "");2033 const string baseFileName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, "", ""); 1978 2034 NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4); 1979 2035 … … 1986 2042 if (!sub.runFile.Open(partialName, serviceName, (cRunNumber->runFitsFile).get(), &fNumSubAndFitsData.numOpenFits, this, sub.runNumber)) 1987 2043 { 1988 SetCurrentState(kSM_WriteError); 2044 GoToWriteErrorState(); 2045 //SetCurrentState(kSM_WriteError); 1989 2046 return; 1990 2047 } … … 1994 2051 if (sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber)) 1995 2052 { 1996 SetCurrentState(kSM_WriteError); 2053 GoToWriteErrorState(); 2054 //SetCurrentState(kSM_WriteError); 1997 2055 return; 1998 2056 } … … 2069 2127 { 2070 2128 if (!sub.nightlyFile.Write(sub.fConv.get())) 2071 SetCurrentState(kSM_WriteError); 2129 { 2130 GoToWriteErrorState(); 2131 return; 2132 //SetCurrentState(kSM_WriteError); 2133 } 2072 2134 } 2073 2135 … … 2075 2137 { 2076 2138 if (!sub.runFile.Write(sub.fConv.get())) 2077 SetCurrentState(kSM_WriteError); 2139 { 2140 GoToWriteErrorState(); 2141 return; 2142 //SetCurrentState(kSM_WriteError); 2143 } 2078 2144 } 2079 2145 } 2080 2146 #endif //if has_fits 2081 2147 2148 void DataLogger::GoToWriteErrorState() 2149 { 2150 GoToReadyPlease(); 2151 SetCurrentState(kSM_WriteError); 2152 } 2153 2082 2154 std::string DataLogger::SetCurrentState(int state, const char *txt, const std::string &cmd) 2083 2155 { 2084 if (state == kSM_WriteError && GetCurrentState() == kSM_WriteError)2085 return "";2156 // if (state == kSM_WriteError && GetCurrentState() == kSM_WriteError) 2157 // return ""; 2086 2158 return StateMachineImp::SetCurrentState(state, txt, cmd); 2087 2159 } … … 2101 2173 //open all the relevant run-files. i.e. all the files associated with run numbers. 2102 2174 for (list<RunNumberType>::iterator it=fRunNumber.begin(); it != fRunNumber.end(); it++) 2103 OpenRunFile(*it); 2175 if (OpenRunFile(*it) != 0) 2176 { 2177 StopRunPlease(); 2178 return kSM_BadRunConfig; 2179 } 2104 2180 2105 2181 return kSM_Logging; … … 2129 2205 CCfits::FITS* groupFile; 2130 2206 unsigned int numFilesToGroup = 0; 2207 unsigned int maxCharLength = 0; 2131 2208 for (map<string, vector<string> >::const_iterator it=filesToGroup.begin(); it != filesToGroup.end(); it++) 2132 2209 { 2210 //add the number of tables in this file to the total number to group 2133 2211 numFilesToGroup += it->second.size(); 2134 } 2212 //check the length of all the strings to be written, to determine the max string length to write 2213 if (it->first.size() > maxCharLength) 2214 maxCharLength = it->first.size(); 2215 for (vector<string>::const_iterator jt=it->second.begin(); jt != it->second.end(); jt++) 2216 if (jt->size() > maxCharLength) 2217 maxCharLength = jt->size(); 2218 } 2219 2135 2220 if (fDebugIsOn) 2136 2221 { … … 2146 2231 string groupName; 2147 2232 if (runNumber != 0) 2148 groupName = CompileFileName (fRunFilePath, runNumber, "", "fits");2233 groupName = CompileFileNameWithPath(fRunFilePath, runNumber, "", "fits"); 2149 2234 else 2150 groupName = CompileFileName (fNightlyFilePath, "", "fits");2235 groupName = CompileFileNameWithPath(fNightlyFilePath, "", "fits"); 2151 2236 2152 2237 Info("Creating FITS group in: "+groupName); 2153 2238 2154 2239 CCfits::Table* groupTable; 2155 const int maxCharLength = 50;//FILENAME_MAX;2240 // const int maxCharLength = FILENAME_MAX; 2156 2241 try 2157 2242 { … … 2224 2309 str << "Writing FITS row " << i << " in " << groupName << ": " << text << " (file_write_tblbytes, rc=" << status << ")"; 2225 2310 Error(str); 2226 // FIXME: What to do in case of error? 2311 GoToWriteErrorState(); 2312 return; 2227 2313 } 2228 2314 } … … 2320 2406 return kSM_Ready; 2321 2407 } 2408 2322 2409 // -------------------------------------------------------------------------- 2323 2410 //
Note:
See TracChangeset
for help on using the changeset viewer.