Changeset 11089 for trunk/FACT++/src


Ignore:
Timestamp:
06/21/11 13:29:48 (13 years ago)
Author:
lyard
Message:
Various dataLogger changes
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Fits.cc

    r11015 r11089  
    299299    {
    300300        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;
    302304        fMess->Error(str);
    303305        Close();
  • trunk/FACT++/src/datalogger.cc

    r11074 r11089  
    221221    ///run numbers
    222222    list<RunNumberType> fRunNumber;
    223     ///old run numbers time-out delay (in minutes)
     223    ///old run numbers time-out delay (in seconds)
    224224    long fRunNumberTimeout;
    225225    ///previous run number. to check if changed while logging
     
    362362    void AppendYearMonthDaytoPath(string& path);
    363363    ///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());
    365367    ///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());
    367371    ///Check whether service is in black and/or white list
    368372    bool ShouldSubscribe(const string& server, const string& service);
     
    376380    bool RememberFileOrigSizePlease(string& fileName, bool nightly);
    377381    ///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();
    379385    ///Checks if a given path exist
    380386    bool DoesPathExist(string path);
     
    550556//! @param out the ofstream that should be checked
    551557//
    552 void DataLogger::CheckForOfstreamError(ofstream& out)
     558bool DataLogger::CheckForOfstreamError(ofstream& out)
    553559{
    554560    if (out.good())
    555         return;
     561        return true;
    556562
    557563    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);
    561570}
    562571// --------------------------------------------------------------------------
     
    648657//! @param extension the extension to add, if any
    649658//
    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)
     660string 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
     680string 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
    675698}
    676699// --------------------------------------------------------------------------
     
    683706//! @param extension the extension to add, if any
    684707//
    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)
     709string DataLogger::CompileFileName(const uint32_t run, const string& service, const string& extension)//, const Time& time)
    686710{
    687711       ostringstream str;
    688        //calculate suitable time for naming files and output it
    689        str << path << Time::fmt("/%Y/%m/%d") << (time-boost::posix_time::time_duration(12,0,0));
    690 
    691        //check if target directory exist
    692        if (!DoesPathExist(str.str()))
    693            CreateDirectory(str.str());
    694 
    695712       //output base of file name
    696        str << '/' << setfill('0') << setw(8) << run;
     713       str << setfill('0') << setw(8) << run;
    697714
    698715       //output service name
     
    706723}
    707724
     725string 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}
    708740// --------------------------------------------------------------------------
    709741//
     
    10371069     fBaseSizeRun = 0;
    10381070     fDailyFileDayChangedAlready = true;
    1039      fRunNumberTimeout = 1;
     1071     fRunNumberTimeout = 60; //default run-timeout set to 1 minute
    10401072     if(fDebugIsOn)
    10411073     {
     
    11141146    const Time cTime = Time();
    11151147
    1116     if ((cTime - fPreviousOldRunNumberCheck).total_seconds() < fRunNumberTimeout*60)
     1148    if ((cTime - fPreviousOldRunNumberCheck).total_seconds() < fRunNumberTimeout)
    11171149        return;
    11181150
    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))
    11201152    {
    11211153         RemoveOldestRunNumber();
     
    12301262
    12311263    // open report file
    1232     run.reportName = CompileFileName(fRunFilePath, run.runNumber, "", "rep");
     1264    run.reportName = CompileFileNameWithPath(fRunFilePath, run.runNumber, "", "rep");
    12331265    if (!OpenStream(run.reportFile, run.reportName))
    12341266        return -1;
     
    12411273
    12421274    //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, "", "");
    12441276    NotifyOpenedFile(baseFileName, 3, fOpenedRunFiles);
    12451277    run.openedFits.clear();
     
    12781310        return;
    12791311    //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    }
    12811326}
    12821327// --------------------------------------------------------------------------
     
    13301375    {
    13311376        if (fDebugIsOn)
    1332             Debug("Its Noon! Closing and reopening daily text files");
     1377            Debug("Its Noon! Closing and reopening nightly text files");
    13331378
    13341379        fNightlyLogFile.close();
     
    13381383        Info("Closed: "+fFullNightlyReportFileName);
    13391384
    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        }
    13471400
    13481401        fDailyFileDayChangedAlready = true;
     
    13541407    if ((!sub.fConv.get()) && isItaReport)
    13551408    {
    1356         //trick the converter in case of 'C'. why do I do this ? well simple: the converter checks that the right number
    1357         //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);
    13621409        sub.fConv = shared_ptr<Converter>(new Converter(Out(), I->getFormat()));
    13631410        if (!sub.fConv)
     
    14491496        {
    14501497            fNightlyReportFile << header.str() << text << endl;
    1451             CheckForOfstreamError(fNightlyReportFile);
     1498            if (!CheckForOfstreamError(fNightlyReportFile))
     1499                return;
    14521500        }
    14531501        //write entry to run-report
     
    14551503        {
    14561504            *targetRunFile << header.str() << text << endl;
    1457             CheckForOfstreamError(*targetRunFile);
     1505            if (!CheckForOfstreamError(*targetRunFile))
     1506                return;
    14581507        }
    14591508    }
     
    14661515        {
    14671516            MessageImp(fNightlyLogFile).Write(cTime, msg.str().c_str(), fQuality);
    1468             CheckForOfstreamError(fNightlyLogFile);
     1517            if (!CheckForOfstreamError(fNightlyLogFile))
     1518                return;
    14691519        }
    14701520        if (targetRunFile && targetRunFile->is_open())
    14711521        {
    14721522            MessageImp(*targetRunFile).Write(cTime, msg.str().c_str(), fQuality);
    1473             CheckForOfstreamError(*targetRunFile);
     1523            if (!CheckForOfstreamError(*targetRunFile))
     1524                return;
    14741525        }
    14751526    }
     
    15221573    //timeout value
    15231574    str.str("");
    1524     str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " minute(s)";
     1575    str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " seconds";
    15251576    Message(str);
    15261577
     
    17311782
    17321783    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";
    17341785    Message(str);
    17351786
     
    18461897        Debug("Starting...");   
    18471898    }
    1848     fFullNightlyLogFileName = CompileFileName(fNightlyFilePath, "", "log");
     1899    fFullNightlyLogFileName = CompileFileNameWithPath(fNightlyFilePath, "", "log");
    18491900    if (!OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName))
    18501901        return kSM_BadNightlyConfig;
    18511902
    18521903
    1853     fFullNightlyReportFileName = CompileFileName(fNightlyFilePath, "", "rep");
     1904    fFullNightlyReportFileName = CompileFileNameWithPath(fNightlyFilePath, "", "rep");
    18541905    if (!OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName))
     1906    {
     1907        fNightlyLogFile.close();
    18551908        return kSM_BadNightlyConfig;
    1856 
     1909    }
    18571910    //get the size of the newly opened file.
    18581911    fBaseSizeNightly = GetFileSize(fFullNightlyLogFileName);
     
    18631916
    18641917    //notify that a new file has been opened.
    1865     const string baseFileName = CompileFileName(fNightlyFilePath, "", "");
     1918    const string baseFileName = CompileFileNameWithPath(fNightlyFilePath, "", "");
    18661919    NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles);
    18671920
     
    19011954        }
    19021955    }
    1903     hasGrouping = true;
    19041956    for (unsigned int i=0;i<serviceName.size(); i++)
    19051957    {
     
    19131965    if (!sub.nightlyFile.IsOpen())
    19141966    {
    1915         string partialName = CompileFileName(fNightlyFilePath, serviceName, "fits");
     1967        string partialName = CompileFileNameWithPath(fNightlyFilePath, serviceName, "fits");
    19161968
    19171969        const string fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
     
    19281980        if (!sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, 0))
    19291981        {
    1930             SetCurrentState(kSM_WriteError);
     1982            GoToWriteErrorState();
     1983            //SetCurrentState(kSM_WriteError);
    19311984            return;
    19321985        }
    19331986        //notify the opening
    1934         const string baseFileName = CompileFileName(fNightlyFilePath, "", "");
     1987        const string baseFileName = CompileFileNameWithPath(fNightlyFilePath, "", "");
    19351988        NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles);
    19361989        if (fNumSubAndFitsIsOn)
     
    19441997        if (hasGrouping)
    19451998        {
    1946             partialName = CompileFileName(fRunFilePath, sub.runNumber, "", "fits");
     1999            partialName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, "", "fits");
    19472000            fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
    19482001        }
    19492002        else
    19502003        {
    1951             partialName = CompileFileName(fRunFilePath, sub.runNumber, serviceName, "fits");
     2004            partialName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, serviceName, "fits");
    19522005            fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
    19532006        }
     
    19732026                Error(str);
    19742027                cRunNumber->runFitsFile = shared_ptr<CCfits::FITS>();//NULL;
     2028                GoToWriteErrorState();
     2029                //SetCurrentState(kSM_WriteError);
     2030                return;
    19752031            }
    19762032
    1977         const string baseFileName = CompileFileName(fRunFilePath, sub.runNumber, "", "");
     2033        const string baseFileName = CompileFileNameWithPath(fRunFilePath, sub.runNumber, "", "");
    19782034        NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4);
    19792035
     
    19862042            if (!sub.runFile.Open(partialName, serviceName, (cRunNumber->runFitsFile).get(), &fNumSubAndFitsData.numOpenFits, this, sub.runNumber))
    19872043            {
    1988                 SetCurrentState(kSM_WriteError);
     2044                GoToWriteErrorState();
     2045                //SetCurrentState(kSM_WriteError);
    19892046                return;
    19902047            }
     
    19942051            if (sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber))
    19952052            {
    1996                 SetCurrentState(kSM_WriteError);
     2053                GoToWriteErrorState();
     2054                //SetCurrentState(kSM_WriteError);
    19972055                return;
    19982056            }
     
    20692127        {
    20702128            if (!sub.nightlyFile.Write(sub.fConv.get()))
    2071                 SetCurrentState(kSM_WriteError);
     2129            {
     2130                GoToWriteErrorState();
     2131                return;
     2132              //SetCurrentState(kSM_WriteError);
     2133            }
    20722134         }
    20732135
     
    20752137        {
    20762138            if (!sub.runFile.Write(sub.fConv.get()))
    2077                 SetCurrentState(kSM_WriteError);
     2139            {
     2140                GoToWriteErrorState();
     2141                return;
     2142                //SetCurrentState(kSM_WriteError);
     2143            }
    20782144        }
    20792145}
    20802146#endif //if has_fits
    20812147
     2148void DataLogger::GoToWriteErrorState()
     2149{
     2150    GoToReadyPlease();
     2151    SetCurrentState(kSM_WriteError);
     2152}
     2153
    20822154std::string DataLogger::SetCurrentState(int state, const char *txt, const std::string &cmd)
    20832155{
    2084     if (state == kSM_WriteError && GetCurrentState() == kSM_WriteError)
    2085         return "";
     2156//    if (state == kSM_WriteError && GetCurrentState() == kSM_WriteError)
     2157//        return "";
    20862158    return StateMachineImp::SetCurrentState(state, txt, cmd);
    20872159}
     
    21012173    //open all the relevant run-files. i.e. all the files associated with run numbers.
    21022174    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        }
    21042180
    21052181    return kSM_Logging;
     
    21292205    CCfits::FITS* groupFile;
    21302206    unsigned int numFilesToGroup = 0;
     2207    unsigned int maxCharLength = 0;
    21312208    for (map<string, vector<string> >::const_iterator it=filesToGroup.begin(); it != filesToGroup.end(); it++)
    21322209    {
     2210        //add the number of tables in this file to the total number to group
    21332211        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
    21352220    if (fDebugIsOn)
    21362221    {
     
    21462231    string groupName;
    21472232    if (runNumber != 0)
    2148         groupName = CompileFileName(fRunFilePath, runNumber, "", "fits");
     2233        groupName = CompileFileNameWithPath(fRunFilePath, runNumber, "", "fits");
    21492234    else
    2150         groupName = CompileFileName(fNightlyFilePath, "", "fits");
     2235        groupName = CompileFileNameWithPath(fNightlyFilePath, "", "fits");
    21512236
    21522237    Info("Creating FITS group in: "+groupName);
    21532238
    21542239    CCfits::Table* groupTable;
    2155     const int maxCharLength = 50;//FILENAME_MAX;
     2240//    const int maxCharLength = FILENAME_MAX;
    21562241    try
    21572242    {
     
    22242309                str << "Writing FITS row " << i << " in " << groupName << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
    22252310                Error(str);
    2226                 // FIXME: What to do in case of error?
     2311                GoToWriteErrorState();
     2312                return;
    22272313            }
    22282314        }
     
    23202406    return kSM_Ready;
    23212407}
     2408
    23222409// --------------------------------------------------------------------------
    23232410//
Note: See TracChangeset for help on using the changeset viewer.