Changeset 10906 for trunk


Ignore:
Timestamp:
06/03/11 15:50:06 (13 years ago)
Author:
lyard
Message:
changed the file name forming scheme
File:
1 edited

Legend:

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

    r10899 r10906  
    182182            openedFits[fileName].push_back(serviceName);
    183183    }
    184 
    185184};
    186185///Dim subscription type. Stores all the relevant info to handle a Dim subscription
     
    423422    ///Appends the relevant year month day to a given path
    424423    void AppendYearMonthDaytoPath(string& path);
    425     ///Form the base nightly path
    426     void FormBaseNightlyPath(string& path);
    427     ///Form the base run path
    428     void FormBaseRunPath(string& path, const int runNumber);
    429     ///Form the base (without extension) name of the nightly text files (logs and reports)
    430     void FormBaseNightlyTextFileName(string& name);
    431     ///Form the name of the nightly text files (logs and reports)
    432     void FormNightlyTextFileName(string& name, bool isReport);
    433     ///Form the base (without extension) name of the run text files (logs and reports)
    434     void FormBaseRunTextFileName(string& name, const int runNumber);
    435     ///Form the name of the run text files (logs and reports)
    436     void FormRunTextFileName(string& name, bool isReport, const int runNumber);
    437     ///Form the name of the nightly Fits files
    438     void FormNightlyFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName);
    439     ///Form the name of the run Fits files
    440     void FormRunFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName, const int runNumber);
    441     ///Form the name of the grouped fits file (i.e. the file where several services are written)
    442     void FormGroupedRunFitsFileName(string& fileName, string& fileNameWithPath, const int runNumber);
    443     ///Form the name of the grouping fits file (i.e. the file that is created after a run ends)
    444     void FormFitsGroupingFileName(string& fileNameWithPath, const int runNumber);
     424    ///Form the files path
     425    string CompileFileName(const string &path, const Time &time, const string &service, const string & extension);
     426    ///Form the files path
     427    string CompileFileName(const string &path, const Time &time, uint32_t run, const string &service, const string & extension);
    445428    ///Check whether service is in black and/or white list
    446429    bool ShouldSubscribe(const string& server, const string& service);
     
    450433    void OpenTextFilePlease(ofstream& stream, const string& name);
    451434    ///Check if a dir is . and returns the actual string corresponding to .
    452     string CheckIfDirIsDot(const string& dir);
     435//    string CheckIfDirIsDot(const string& dir);
    453436    ///Remembers the size of newly opened files. for statistic purposes
    454437    bool RememberFileOrigSizePlease(string& fileName, bool nightly);
     
    457440    ///Checks if a given path exist
    458441    bool DoesPathExist(string path);
     442public:
    459443    ///Create a given directory
    460444    bool CreateDirectory(string path);
     
    478462bool DataLogger::CreateDirectory(string path)
    479463{
     464    //remove last '/', if present
     465    if (path[path.size()-1] == '/')
     466        path = path.substr(0, path.size()-1);
     467    //create boost path
     468    boost::filesystem::path fullPath =  boost::filesystem::system_complete(boost::filesystem::path(path));
     469
     470    //if path does not exist, check if upper levels exist already
     471    if (!boost::filesystem::exists(fullPath))
     472    {
     473        if (path.size() <= 1)
     474        {//we're hitting "/", which SHOULD have existed...
     475            Error("Something unexpected happened while creating a path");
     476        }
     477        CreateDirectory(path.substr(0, path.find_last_of('/')));
     478    }
     479    else
     480    {//if path already exist, make sure it does not designate a file (filenames cannot be checked if they do not exist)
     481        if (!boost::filesystem::is_directory(fullPath))
     482        {
     483            Error("Path to be created contains a file name: \"" + path + "\"");
     484            return false;
     485        }
     486        else
     487            return true;
     488    }
     489    //path does not exist, and upper level have been created already by recusrion.
    480490    mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes
    481491
     
    500510bool DataLogger::DoesPathExist(string path)
    501511{
    502     struct stat st;
    503     if(stat(path.c_str(),&st) == 0)
    504         return true;
    505 
    506     return false;
     512    boost::filesystem::path fullPath = boost::filesystem::system_complete(boost::filesystem::path(path));
     513
     514
     515    if (!boost::filesystem::exists(fullPath))
     516       return false;
     517
     518    if (!boost::filesystem::is_directory(fullPath))
     519    {
     520        Error("Path given for checking \"" + path + "\" designate a file name. Please provide a path name only");
     521        return false;
     522    }
     523
     524    if (access(path.c_str(), R_OK|W_OK|X_OK) != 0)
     525    {
     526        Error("Missing read, write or execute permissions on directory\"" + path + "\"");
     527        return false;
     528    }
     529
     530    return true;
    507531}
    508532// --------------------------------------------------------------------------
     
    603627    return false;
    604628}
    605 // --------------------------------------------------------------------------
    606 //
    607 //! Checks if a given directory is simply '.' and retrieves the coresponding full path
    608 //! @param dir the input directory that should be checked against
    609 //! @return the full path corresponding to the input directory
    610 //
    611 string DataLogger::CheckIfDirIsDot(const string& dir)
    612 {
    613     errno = 0;
    614     if (dir == ".")
    615     {
    616         char currentPath[FILENAME_MAX];
    617         if (!getcwd(currentPath, sizeof(currentPath)))
    618         {
    619             if (errno != 0)
    620             {
    621                 ostringstream str;
    622                 str << "Unable to retrieve the current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
    623                 Error(str);
    624             }
    625         }
    626         return string(currentPath);
    627     }
    628     else
    629     {
    630         return string(dir);
    631     }
    632 }
     629
    633630// --------------------------------------------------------------------------
    634631//
     
    687684// --------------------------------------------------------------------------
    688685//
    689 //!Form the name of the grouped fits file (i.e. the file where several services are written)
    690 //! @param fileName only the file name itself (no path)
    691 //! @param fileNameWithPath fileName, augmented with the prefix path
    692 //! @param runNumber the current run number for which the file should be opened.
    693 //
    694 void DataLogger::FormGroupedRunFitsFileName(string& fileName, string& fileNameWithPath, const int runNumber)
    695 {
    696     ostringstream sRun;
    697     sRun << runNumber;
    698     fileName = sRun.str() + "_group.fits";
    699     string path;
    700     FormBaseRunPath(path, runNumber);
    701     fileNameWithPath = path + '/' + fileName;
    702 }
    703 
    704 // --------------------------------------------------------------------------
    705 //
    706 //! Form the name of the grouping fits file (i.e. the file that is created after a run ends)
    707 //! This file is meant to link the various fits files related to a particular run from a single file
    708 //! @param fileNameWithPath the full filename
    709 //! @param runNumber the current run number for which the file should be opened. -1 means nightly grouping
    710 //
    711 void DataLogger::FormFitsGroupingFileName(string& fileNameWithPath, const int runNumber)
    712 {
    713     ostringstream groupName;
    714     string path;
    715     if (runNumber != 0)
    716     {
    717         FormBaseRunPath(path, runNumber);
    718         groupName << path << '/' << runNumber << ".fits";
    719     }
    720     else
    721     {
    722         ostringstream sTime;
    723         unsigned short year, month, day;
    724         GetYearMonthDayForFiles(year, month, day);
    725         sTime << year << "_" << month << "_" << day;
    726         FormBaseNightlyPath(path);
    727 
    728         groupName << path << '/' << sTime.str() << ".fits";
    729     }
    730     fileNameWithPath = groupName.str();
    731 
    732 }
    733 // --------------------------------------------------------------------------
    734 //
    735 //! Form the name of the nightly Fits files
    736 //! @param fileName the file name only
    737 //! @param fileNameWithPath the fileName augmented by the prefix path
    738 //! @param serviceName the service for which this file should be opened
    739 //
    740 void DataLogger::FormNightlyFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName)
    741 {
    742     ostringstream sTime;
    743     unsigned short year, month, day;
    744     GetYearMonthDayForFiles(year, month, day);
    745     sTime << year << "_" << month << "_" << day;
    746     fileName = sTime.str() + '_' + serviceName + ".fits";
    747     string path;
    748     FormBaseNightlyPath(path);
    749     fileNameWithPath = path + '/' + fileName;
    750 }
    751 // --------------------------------------------------------------------------
    752 //
    753 //! Form the name of the run Fits files
    754 //! @param fileName the file name only
    755 //! @param fileNameWithPath the fileName augmented by the prefix path
    756 //! @param serviceName the service for which this file should be opened
    757 //! @param runNumber the current run number for which this file should be opened
    758 //
    759 void DataLogger::FormRunFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName, const int runNumber)
    760 {
    761     ostringstream sRun;
    762     sRun << runNumber;
    763     fileName = sRun.str() + '_' + serviceName + ".fits";
    764     string path;
    765     FormBaseRunPath(path, runNumber);
    766     fileNameWithPath = path + '/' + fileName;
    767 }
    768 // --------------------------------------------------------------------------
    769 //
    770 //! Form the base (without extension) name of the run text files (logs and reports)
    771 //! @param name the full file name
    772 //! @param runNumber the current run number for which this file should be opened
    773 //
    774 void DataLogger::FormBaseRunTextFileName(string& name, const int runNumber)
    775 {
    776     ostringstream sRun;
    777     sRun << runNumber;
    778     string path;
    779     FormBaseRunPath(path, runNumber);
    780     name = path + '/' + sRun.str();
    781 }
    782 // --------------------------------------------------------------------------
    783 //
    784 //! Form the name of the run text files (logs and reports)
    785 //! @param name the full file name
    786 //! @param isReport whether a log or report name should be formed
    787 //! @param runNumber the current run number for which this file should be opened
    788 //
    789 void DataLogger::FormRunTextFileName(string& name, bool isReport, const int runNumber)
    790 {
    791     FormBaseRunTextFileName(name, runNumber);
    792 
    793     if (isReport)
    794         name += ".rep";
    795     else
    796         name += ".log";
    797 }
    798 // --------------------------------------------------------------------------
    799 //
    800 //! Form the base (without extension) name of the nightly text files (logs and reports)
    801 //! @param name the full file name
    802 //
    803 void DataLogger::FormBaseNightlyTextFileName(string& name)
    804 {
    805     ostringstream sTime;
    806     unsigned short year, month, day;
    807     GetYearMonthDayForFiles(year, month, day);
    808     sTime << year << "_" << month << "_" << day;
    809 
    810     FormBaseNightlyPath(name);
    811 
    812     name += '/' + sTime.str();
    813 }
    814 // --------------------------------------------------------------------------
    815 //
    816 //! Form the name of the nightly text files (logs and reports)
    817 //! @param name the full file name
    818 //! @param isReport whether a log or report name should be formed
    819 //
    820 void DataLogger::FormNightlyTextFileName(string& name, bool isReport)
    821 {
    822     FormBaseNightlyTextFileName(name);
    823 
    824     if (isReport)
    825         name += ".rep";
    826     else
    827         name += ".log";
    828 }
    829 // --------------------------------------------------------------------------
    830 //
    831 //!Fills in the correct values for naming files based on current date.
    832 //! It fills in the same values throughout a whole night for consistency
    833 //! @param year the year to be used for file names
    834 //! @param month the month to be used for file names
    835 //! @param day the day to be used for file names
    836 //
    837 void DataLogger::GetYearMonthDayForFiles(unsigned short& year, unsigned short& month, unsigned short& day)
    838 {
    839     Time time;
    840     year = time.Y();
    841     month = time.M();
    842     day = time.D();
    843     bool shouldGoBackOneDay = false;
    844     if (time.h() < 12) //we are currently before noon. go back one day
    845         shouldGoBackOneDay = true;
    846 
    847     if (shouldGoBackOneDay && (month == 1) && (day == 1))
    848         year--;
    849 
    850     if (shouldGoBackOneDay && (day == 1))
    851     {
    852         if (month != 1)
    853             month--;
    854         else
    855             month = 12;
    856     }
    857 
    858     if (shouldGoBackOneDay)
    859     {
    860         if (day != 1)
    861             day--;
    862         else
    863             switch (month)
    864             {
    865                 case 1:
    866                 case 3:
    867                 case 5:
    868                 case 7:
    869                 case 8:
    870                 case 10:
    871                 case 12:
    872                     day = 31;
    873                 break;
    874                 case 4:
    875                 case 6:
    876                 case 9:
    877                 case 11:
    878                     day = 30;
    879                 break;
    880                 case 2:
    881                     if (year == 2012 || year == 2016 || year == 2020 || year == 2024 || year == 2028 || year == 2032 || year == 2036 || year == 2040)
    882                         day = 29;
    883                     else
    884                         day = 28;
    885                 break;
    886             };
    887     }
    888 
    889 
    890 }
    891 // --------------------------------------------------------------------------
    892 //
    893 //! Appends the appropriate year, month and day to a given path.
    894 //! creates the required directories if they do not already exist
    895 //! @param path the path to be augmented
    896 //
    897 void DataLogger::AppendYearMonthDaytoPath(string& path)
    898 {
    899     ostringstream basePath;
    900     unsigned short year, month, day;
    901     GetYearMonthDayForFiles(year, month, day);
    902 
    903     basePath << CheckIfDirIsDot(path) << '/' << year;
    904     if (!DoesPathExist(basePath.str()))
    905          CreateDirectory(basePath.str());
    906 
    907      basePath << '/' << month;
    908      if (!DoesPathExist(basePath.str()))
    909          CreateDirectory(basePath.str());
    910 
    911      basePath << '/' << day;
    912      if (!DoesPathExist(basePath.str()))
    913          CreateDirectory(basePath.str());
    914 
    915      path = basePath.str();
    916 }
    917 // --------------------------------------------------------------------------
    918 //
    919 //! Forms the base path for putting nightly files in it
    920 //! @param the variable that should receive the path
    921 //
    922 void DataLogger::FormBaseNightlyPath(string& path)
    923 {
    924     path = fNightlyFilePath;
    925     AppendYearMonthDaytoPath(path);
    926 }
    927 // --------------------------------------------------------------------------
    928 //
    929 //! Forms the base path for putting run files in it
    930 //! @param the variable that should receive the path
    931 //! @runNumber the run number for which to create the path
    932 //
    933 void DataLogger::FormBaseRunPath(string& path, const int runNumber)
    934 {
    935     path = fRunFilePath;
    936     AppendYearMonthDaytoPath(path);
    937 
    938     ostringstream basePath;
    939     basePath << path << '/' << runNumber;
    940     if (!DoesPathExist(basePath.str()))
    941         CreateDirectory(basePath.str());
    942 
    943     path = basePath.str();
    944 }
     686//! Compiles a file name
     687//! @param path the base path where to put the file
     688//! @param time the time at which the file is created
     689//! @param service the service name, if any
     690//! @param extension the extension to add, if any
     691//
     692string DataLogger::CompileFileName(const string &path, const Time &time, const string &service, const string & extension)
     693{
     694       ostringstream str;
     695       //calculate time suitable for naming files.
     696       Time fTime(time-boost::posix_time::time_duration(12,0,0));
     697
     698       //output it
     699       str << path << Time::fmt("/%Y/%m/%d") << fTime;
     700
     701       //check if target directory exist
     702       if (!DoesPathExist(str.str()))
     703           CreateDirectory(str.str());
     704
     705       //output base of file name
     706       str << Time::fmt("/%Y_%m_%d") << fTime;
     707
     708       //output service name
     709       if (!service.empty())
     710           str  << "_" << service;
     711
     712       //output appropriate extension
     713       if (!extension.empty())
     714           str << "." << extension;
     715
     716       return str.str();
     717}
     718// --------------------------------------------------------------------------
     719//
     720//! Compiles a file name
     721//! @param path the base path where to put the file
     722//! @param time the time at which the file is created
     723//! @param run the run number
     724//! @param service the service name, if any
     725//! @param extension the extension to add, if any
     726//
     727string DataLogger::CompileFileName(const string &path, const Time &time, uint32_t run, const string &service, const string & extension)
     728{
     729       ostringstream str;
     730       //calculate suitable time for naming files and output it
     731       str << path << Time::fmt("/%Y/%m/%d") << (time-boost::posix_time::time_duration(12,0,0));
     732
     733       //check if target directory exist
     734       if (!DoesPathExist(str.str()))
     735           CreateDirectory(str.str());
     736
     737       //output base of file name
     738       str << '/' << setfill('0') << setw(8) << run;
     739
     740       //output service name
     741       if (!service.empty())
     742           str  << "_" << service;
     743
     744       //output appropriate extension
     745       if (!extension.empty())
     746           str << "." << extension;
     747       return str.str();
     748}
     749
    945750// --------------------------------------------------------------------------
    946751//
     
    13821187        return -1;
    13831188    }
    1384     FormRunTextFileName(run.logName, false, run.runNumber);
     1189    run.logName = CompileFileName(fRunFilePath, Time(), run.runNumber, "", "log");
    13851190    errno = 0;
    13861191    run.logFile->open(run.logName.c_str(), ios_base::out | ios_base::app);
     
    13931198#endif
    13941199    //open report file
    1395     FormRunTextFileName(run.reportName, true, run.runNumber);
     1200    run.reportName = CompileFileName(fRunFilePath, Time(), run.runNumber, "", "rep");
    13961201    if (run.reportFile->is_open())
    13971202    {
     
    14321237
    14331238    //TODO this notification scheme might be messed up now.... fix it !
    1434     string baseFileName;
    1435     FormBaseRunTextFileName(baseFileName, run.runNumber);
     1239    string baseFileName = CompileFileName(fRunFilePath, Time(), run.runNumber, "", "");
    14361240    NotifyOpenedFile(baseFileName, 3, fOpenedRunFiles);
    14371241    run.openedFits.clear();
     
    16581462
    16591463    //print the path configuration
    1660     Message("Nightly Path: " + CheckIfDirIsDot(fNightlyFilePath));
    1661     Message("Run Path: " + CheckIfDirIsDot(fRunFilePath));
     1464    Message("Nightly Path: " + boost::filesystem::system_complete(boost::filesystem::path(fNightlyFilePath)).directory_string());
     1465    Message("Run Path: " + boost::filesystem::system_complete(boost::filesystem::path(fRunFilePath)).directory_string());
    16621466
    16631467    //print active run numbers
     
    18631667        if (!DoesPathExist(givenPath))
    18641668        {
    1865             Error("Given Nightly folder \"" + givenPath + "\" does not exist. Please specify a valid path");
     1669            Error("Provided path is not a valid folder. Ignored");
    18661670            return GetCurrentState();
    18671671        }
     
    18881692        if (!DoesPathExist(givenPath))
    18891693        {
    1890             Error("Given Run folder \"" + givenPath + "\" does not exist. Please specify a valid path");
     1694            Error("Provided path is not a valid folder. Ignored");
    18911695            return GetCurrentState();
    18921696        }
     
    19571761        Debug("Starting...");   
    19581762    }
    1959 
    1960     FormNightlyTextFileName(fFullNightlyLogFileName, false);
     1763    fFullNightlyLogFileName = CompileFileName(fNightlyFilePath, Time(), "", "log");
    19611764    OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName);
    19621765
    1963     FormNightlyTextFileName(fFullNightlyReportFileName, true);
     1766    fFullNightlyReportFileName = CompileFileName(fNightlyFilePath, Time(), "", "rep");
    19641767    OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName);
    19651768
     
    19791782
    19801783    //notify that a new file has been opened.
    1981     string baseFileName;
    1982     FormBaseNightlyTextFileName(baseFileName);
     1784    string baseFileName = CompileFileName(fNightlyFilePath, Time(), "", "");
    19831785    NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles);
    19841786
     
    20301832    {
    20311833        string fileNameOnly, partialName;
    2032         FormNightlyFitsFileName(fileNameOnly, partialName, serviceName);
     1834        partialName = CompileFileName(fNightlyFilePath, Time(), serviceName, "fits");
     1835        fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
    20331836        AllocateFITSBuffers(sub);
    20341837        //get the size of the file we're about to open
     
    20391842
    20401843        //notify the opening
    2041         string baseFileName;
    2042         FormBaseNightlyTextFileName(baseFileName);
     1844        string baseFileName = CompileFileName(fNightlyFilePath, Time(), "", "");
    20431845        NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles);
    20441846        if (fNumSubAndFitsIsOn)
     
    20561858        string fileNameOnly;
    20571859        string partialName;
    2058 
    20591860        if (hasGrouping)
    20601861        {
    2061             FormGroupedRunFitsFileName(fileNameOnly, partialName, sub.runNumber);
     1862            partialName = CompileFileName(fRunFilePath, Time(), sub.runNumber, "group", "fits");
     1863            fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
    20621864        }
    20631865        else
    20641866        {
    2065             FormRunFitsFileName(fileNameOnly, partialName, serviceName, sub.runNumber);
     1867            partialName = CompileFileName(fRunFilePath, Time(), sub.runNumber, serviceName, "fits");
     1868            fileNameOnly = partialName.substr(partialName.find_last_of('/')+1, partialName.size());
    20661869        }
    20671870        //get the size of the file we're about to open
     
    20881891            }
    20891892
    2090         string baseFileName;
    2091         FormBaseRunTextFileName(baseFileName, sub.runNumber);
     1893        string baseFileName = CompileFileName(fRunFilePath, Time(), sub.runNumber, "", "");
    20921894        NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4);
    20931895        if (hasGrouping)
     
    22692071        return;
    22702072    }
    2271     string groupName;
    2272     FormFitsGroupingFileName(groupName, runNumber);
     2073    string groupName = CompileFileName(fRunFilePath, Time(), runNumber, "", "fits");
    22732074    CCfits::Table* groupTable;
    22742075    int maxCharLength = 50;//FILENAME_MAX;
     
    25322333
    25332334    DataLogger logger(wout);
     2335
    25342336    if (!logger.SetConfiguration(conf))
    25352337        return -1;
Note: See TracChangeset for help on using the changeset viewer.