- Timestamp:
- 06/03/11 15:50:06 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/dataLogger.cc
r10899 r10906 182 182 openedFits[fileName].push_back(serviceName); 183 183 } 184 185 184 }; 186 185 ///Dim subscription type. Stores all the relevant info to handle a Dim subscription … … 423 422 ///Appends the relevant year month day to a given path 424 423 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); 445 428 ///Check whether service is in black and/or white list 446 429 bool ShouldSubscribe(const string& server, const string& service); … … 450 433 void OpenTextFilePlease(ofstream& stream, const string& name); 451 434 ///Check if a dir is . and returns the actual string corresponding to . 452 string CheckIfDirIsDot(const string& dir);435 // string CheckIfDirIsDot(const string& dir); 453 436 ///Remembers the size of newly opened files. for statistic purposes 454 437 bool RememberFileOrigSizePlease(string& fileName, bool nightly); … … 457 440 ///Checks if a given path exist 458 441 bool DoesPathExist(string path); 442 public: 459 443 ///Create a given directory 460 444 bool CreateDirectory(string path); … … 478 462 bool DataLogger::CreateDirectory(string path) 479 463 { 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. 480 490 mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes 481 491 … … 500 510 bool DataLogger::DoesPathExist(string path) 501 511 { 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; 507 531 } 508 532 // -------------------------------------------------------------------------- … … 603 627 return false; 604 628 } 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 633 630 // -------------------------------------------------------------------------- 634 631 // … … 687 684 // -------------------------------------------------------------------------- 688 685 // 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 // 692 string 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 // 727 string 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 945 750 // -------------------------------------------------------------------------- 946 751 // … … 1382 1187 return -1; 1383 1188 } 1384 FormRunTextFileName(run.logName, false, run.runNumber);1189 run.logName = CompileFileName(fRunFilePath, Time(), run.runNumber, "", "log"); 1385 1190 errno = 0; 1386 1191 run.logFile->open(run.logName.c_str(), ios_base::out | ios_base::app); … … 1393 1198 #endif 1394 1199 //open report file 1395 FormRunTextFileName(run.reportName, true, run.runNumber);1200 run.reportName = CompileFileName(fRunFilePath, Time(), run.runNumber, "", "rep"); 1396 1201 if (run.reportFile->is_open()) 1397 1202 { … … 1432 1237 1433 1238 //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, "", ""); 1436 1240 NotifyOpenedFile(baseFileName, 3, fOpenedRunFiles); 1437 1241 run.openedFits.clear(); … … 1658 1462 1659 1463 //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()); 1662 1466 1663 1467 //print active run numbers … … 1863 1667 if (!DoesPathExist(givenPath)) 1864 1668 { 1865 Error(" Given Nightly folder \"" + givenPath + "\" does not exist. Please specify a valid path");1669 Error("Provided path is not a valid folder. Ignored"); 1866 1670 return GetCurrentState(); 1867 1671 } … … 1888 1692 if (!DoesPathExist(givenPath)) 1889 1693 { 1890 Error(" Given Run folder \"" + givenPath + "\" does not exist. Please specify a valid path");1694 Error("Provided path is not a valid folder. Ignored"); 1891 1695 return GetCurrentState(); 1892 1696 } … … 1957 1761 Debug("Starting..."); 1958 1762 } 1959 1960 FormNightlyTextFileName(fFullNightlyLogFileName, false); 1763 fFullNightlyLogFileName = CompileFileName(fNightlyFilePath, Time(), "", "log"); 1961 1764 OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName); 1962 1765 1963 FormNightlyTextFileName(fFullNightlyReportFileName, true);1766 fFullNightlyReportFileName = CompileFileName(fNightlyFilePath, Time(), "", "rep"); 1964 1767 OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName); 1965 1768 … … 1979 1782 1980 1783 //notify that a new file has been opened. 1981 string baseFileName; 1982 FormBaseNightlyTextFileName(baseFileName); 1784 string baseFileName = CompileFileName(fNightlyFilePath, Time(), "", ""); 1983 1785 NotifyOpenedFile(baseFileName, 3, fOpenedNightlyFiles); 1984 1786 … … 2030 1832 { 2031 1833 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()); 2033 1836 AllocateFITSBuffers(sub); 2034 1837 //get the size of the file we're about to open … … 2039 1842 2040 1843 //notify the opening 2041 string baseFileName; 2042 FormBaseNightlyTextFileName(baseFileName); 1844 string baseFileName = CompileFileName(fNightlyFilePath, Time(), "", ""); 2043 1845 NotifyOpenedFile(baseFileName, 7, fOpenedNightlyFiles); 2044 1846 if (fNumSubAndFitsIsOn) … … 2056 1858 string fileNameOnly; 2057 1859 string partialName; 2058 2059 1860 if (hasGrouping) 2060 1861 { 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()); 2062 1864 } 2063 1865 else 2064 1866 { 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()); 2066 1869 } 2067 1870 //get the size of the file we're about to open … … 2088 1891 } 2089 1892 2090 string baseFileName; 2091 FormBaseRunTextFileName(baseFileName, sub.runNumber); 1893 string baseFileName = CompileFileName(fRunFilePath, Time(), sub.runNumber, "", ""); 2092 1894 NotifyOpenedFile(baseFileName, 7, fOpenedRunFiles);// + '_' + serviceName, 4); 2093 1895 if (hasGrouping) … … 2269 2071 return; 2270 2072 } 2271 string groupName; 2272 FormFitsGroupingFileName(groupName, runNumber); 2073 string groupName = CompileFileName(fRunFilePath, Time(), runNumber, "", "fits"); 2273 2074 CCfits::Table* groupTable; 2274 2075 int maxCharLength = 50;//FILENAME_MAX; … … 2532 2333 2533 2334 DataLogger logger(wout); 2335 2534 2336 if (!logger.SetConfiguration(conf)) 2535 2337 return -1;
Note:
See TracChangeset
for help on using the changeset viewer.