Changeset 11535


Ignore:
Timestamp:
07/22/11 08:53:05 (13 years ago)
Author:
tbretz
Message:
Unified funtions to open an ofstream into a single function; unified some output texts in case of failure; return error from EvalOptions if log file could not be opened; use DimWriteStatistics::GetFileSizeOnDisk to replace GetFileSize code
File:
1 edited

Legend:

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

    r11500 r11535  
    314314    void CreateFitsGrouping(map<string, vector<string> >& filesToGroup, int runNumber);
    315315
     316    bool OpenStreamImp(ofstream &stream, const string &filename, bool mightbeopen);
    316317    bool OpenStream(shared_ptr<ofstream> stream, const string &filename);
    317318    ///Open the relevant text files related to a particular run
     
    437438    {
    438439        ostringstream str;
    439         str << "Could not create directory " << path << " mkdir error code: " << errno;
     440        str << "mkdir() failed for '" << path << "': " << strerror(errno) << " [errno=" << errno << "]";
    440441        Error(str.str());
    441442        return false;
     
    524525            return;
    525526
    526         ostringstream str;
    527         str << "Subscription " << server << "/" << service << " could not be removed as it is not present";
    528         Error(str.str());
     527        Error("Subscription "+server+"/"+service+" could not be removed as it is not present");
    529528        return;
    530529    }
     
    572571}
    573572
     573bool DataLogger::OpenStreamImp(ofstream &stream, const string &filename, bool mightbeopen)
     574{
     575    if (stream.is_open())
     576    {
     577        if (!mightbeopen)
     578            Error(filename+" was already open when trying to open it.");
     579        return mightbeopen;
     580    }
     581
     582    errno = 0;
     583    stream.open(filename.c_str(), ios_base::out | ios_base::app);
     584    if (!stream /*|| errno!=0*/)
     585    {
     586        ostringstream str;
     587        str << "ofstream::open() failed for '" << filename << "': " << strerror(errno) << " [errno=" << errno << "]";
     588        Error(str);
     589        return false;
     590    }
     591
     592    if (!stream.is_open())
     593    {
     594        Error("File "+filename+" not open as it ought to be.");
     595        return false;
     596    }
     597
     598    Info("Opened: "+filename);
     599
     600    return true;
     601}
     602
     603bool DataLogger::OpenStream(shared_ptr<ofstream> stream, const string &filename)
     604{
     605    return OpenStreamImp(*stream, filename, false);
     606}
     607
    574608// --------------------------------------------------------------------------
    575609//
     
    580614bool DataLogger::OpenTextFilePlease(ofstream& stream, const string& name)
    581615{
    582     if (stream.is_open())
    583         return true;
    584 
    585     errno = 0;
    586     stream.open(name.c_str(), ios_base::out | ios_base::app);
    587     if (!stream.is_open())
    588     {
    589         ostringstream str;
    590         str << "Trying to open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
    591         Error(str);
    592         return false;
    593     }
    594 
    595     Info("Opened: "+name);
    596 
    597     return true;
     616    return OpenStreamImp(stream, name, true);
    598617}
    599618
     
    607626{
    608627    if (fDebugIsOn)
    609     {
    610         ostringstream str;
    611         str << "Subscribing to service " << server << "/" << service;
    612         Debug(str);
    613     }
     628        Debug("Subscribing to service "+server+"/"+service);
     629
    614630    return new DimStampedInfo((server + "/" + service).c_str(), (void*)NULL, 0, this);
    615631}
     
    737753off_t DataLogger::GetFileSize(const string& fileName)
    738754{
    739     errno = 0;
    740     struct stat st;
    741     if (!stat(fileName.c_str(), &st))
    742         return st.st_size;
    743 
    744     if (errno != 0 && errno != 2)//ignoring error #2: no such file or directory is not an error for new files
    745     {
    746         ostringstream str;
    747         str << "Unable to stat " << fileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
    748         Error(str);
    749     }
    750 
    751     return 0;
    752 }
     755    return DimWriteStatistics::GetFileSizeOnDisk(fileName, *this);
     756}
     757
    753758// --------------------------------------------------------------------------
    754759//
     
    10341039    {
    10351040        ostringstream str;
    1036         str << "Format of " << I->getName() << " is empty. Ignoring it";
     1041        str << "Format of " << I->getName() << " is empty (ptr=" << I->getData() << ", size=" << I->getSize() << ")... ignoring it.";
    10371042        Error(str);
    10381043        return;
     
    10551060    //remove old run numbers
    10561061    TrimOldRunNumbers();
    1057 }
    1058 
    1059 bool DataLogger::OpenStream(shared_ptr<ofstream> stream, const string &filename)
    1060 {
    1061     if (stream->is_open())
    1062     {
    1063         ostringstream str;
    1064         str << filename << " was already open when trying to open it.";
    1065         Error(str);
    1066         return false;
    1067     }
    1068 
    1069     errno = 0;
    1070     stream->open(filename.c_str(), ios_base::out | ios_base::app);
    1071     if (errno != 0)
    1072     {
    1073         ostringstream str;
    1074         str << "Unable to open " << filename << ": " << strerror(errno) << " (errno=" << errno << ")";
    1075         Error(str);
    1076         return false;
    1077     }
    1078 
    1079     if (!stream->is_open())
    1080     {
    1081         ostringstream str;
    1082         str << "File " << filename << " not open as it ought to be.";
    1083         Error(str);
    1084         return false;
    1085     }
    1086 
    1087     Info("Opened: "+filename);
    1088 
    1089     return true;
    10901062}
    10911063
     
    11461118    {
    11471119        ostringstream str;
    1148         str << "Adding new run number " << newRun << " that was issued on " << time;
     1120        str << "Adding new run number " << newRun << " issued at " << time;
    11491121        Debug(str);
    11501122    }
     
    12181190    {
    12191191        ostringstream str;
    1220         str << "Logging " <<  I->getName() << " [" << I->getFormat() << "] (" << I->getSize() << ")";
     1192        str << "Logging " << I->getName() << " [" << I->getFormat() << "] (" << I->getSize() << ")";
    12211193        Debug(str);
    12221194    }
     
    12641236
    12651237    //create the converter for that service
    1266     if (!sub.fConv.get())
     1238    if (!sub.fConv)
    12671239    {
    12681240        sub.fConv = shared_ptr<Converter>(new Converter(Out(), I->getFormat()));
    1269         if (!sub.fConv)
     1241        if (!sub.fConv->valid())
    12701242        {
    12711243            ostringstream str;
     
    13041276        if (rit == fRunNumber.rend() && fRunNumber.size() != 0)
    13051277        {
    1306             ostringstream str;
    1307             str << "Could not find an appropriate run number for info coming at time: " << cTime;
    1308             Error(str);
     1278            Error("Could not find an appropriate run number for info coming at time "+cTime.GetAsStr());
    13091279            Error("Active run numbers: ");
    13101280            for (rit=fRunNumber.rbegin(); rit != fRunNumber.rend(); rit++)
    13111281            {
    1312                 str.str("");
    1313                 str << rit->runNumber;
     1282                ostringstream str;
     1283                str << " -> " << rit->runNumber;
    13141284                Error(str);
    13151285            }
     
    20061976        if (sub.nightlyFile.IsOpen())
    20071977        {
    2008             if (!sub.nightlyFile.Write(sub.fConv.get()))
     1978            const shared_ptr<Converter> ptr(sub.fConv);
     1979            if (!sub.nightlyFile.Write(*ptr))
    20091980            {
    20101981                sub.nightlyFile.Close();
     
    20171988        if (sub.runFile.IsOpen())
    20181989        {
    2019             if (!sub.runFile.Write(sub.fConv.get()))
     1990            const shared_ptr<Converter> ptr(sub.fConv);
     1991            if (!sub.runFile.Write(*ptr))
    20201992            {
    20211993                sub.runFile.Close();
     
    23912363         fFullNightlyLogFileName = CompileFileNameWithPath(fNightlyFilePath, "", "log");
    23922364         if (!OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName))
    2393              SetCurrentState(kSM_BadNightlyConfig);
    2394          else
    2395          {
    2396              fNightlyLogFile << endl;
    2397              NotifyOpenedFile(fFullNightlyLogFileName, 1, fOpenedNightlyFiles);
    2398              for (vector<string>::iterator it=backLogBuffer.begin();it!=backLogBuffer.end();it++)
    2399              {
    2400                  fNightlyLogFile << *it;
    2401              }
    2402          }
    2403     }
     2365             return 3;
     2366
     2367         fNightlyLogFile << endl;
     2368         NotifyOpenedFile(fFullNightlyLogFileName, 1, fOpenedNightlyFiles);
     2369         for (vector<string>::iterator it=backLogBuffer.begin();it!=backLogBuffer.end();it++)
     2370             fNightlyLogFile << *it;
     2371     }
    24042372
    24052373    shouldBackLog = false;
Note: See TracChangeset for help on using the changeset viewer.