LCOV - code coverage report
Current view: top level - imageanalysis/IO - LogFile.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 40 43 93.0 %
Date: 2024-11-06 17:42:47 Functions: 7 7 100.0 %

          Line data    Source code
       1             : //# tSubImage.cc: Test program for class SubImage
       2             : //# Copyright (C) 1998,1999,2000,2001,2003
       3             : //# Associated Universities, Inc. Washington DC, USA.
       4             : //#
       5             : //# This program is free software; you can redistribute it and/or modify it
       6             : //# under the terms of the GNU General Public License as published by the Free
       7             : //# Software Foundation; either version 2 of the License, or (at your option)
       8             : //# any later version.
       9             : //#
      10             : //# This program is distributed in the hope that it will be useful, but WITHOUT
      11             : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12             : //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      13             : //# more details.
      14             : //#
      15             : //# You should have received a copy of the GNU General Public License along
      16             : //# with this program; if not, write to the Free Software Foundation, Inc.,
      17             : //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
      18             : //#
      19             : //# Correspondence concerning AIPS++ should be addressed as follows:
      20             : //#        Internet email: casa-feedback@nrao.edu.
      21             : //#        Postal address: AIPS++ Project Office
      22             : //#                        National Radio Astronomy Observatory
      23             : //#                        520 Edgemont Road
      24             : //#                        Charlottesville, VA 22903-2475 USA
      25             : //#
      26             : //# $Id: $
      27             : 
      28             : #include <imageanalysis/IO/LogFile.h>
      29             : 
      30             : #include <casacore/casa/Exceptions/Error.h>
      31             : #include <casacore/casa/OS/File.h>
      32             : #include <imageanalysis/IO/OutputDestinationChecker.h>
      33             : 
      34             : #include <fcntl.h>
      35             : #include <sys/types.h>
      36             : #include <sys/stat.h>
      37             : 
      38             : using namespace casacore;
      39             : namespace casa {
      40             : 
      41          19 : LogFile::LogFile(const String& filename) :
      42          38 :         _filename(filename), _append(false), _fileHandle(-1),
      43          19 :         _fileIO() {
      44          19 :         ThrowIf(
      45             :                 filename.empty(),
      46             :                 "LogFile::LogFile(): file name cannot be empty"
      47             :         );
      48          19 :         OutputDestinationChecker::OutputStruct logFile;
      49          19 :         logFile.label = "log file";
      50          19 :         logFile.outputFile = &_filename;
      51          19 :         logFile.required = false;
      52          19 :         logFile.replaceable = true;
      53          19 :         LogIO log;
      54          19 :         OutputDestinationChecker::checkOutput(logFile, log);
      55          19 :         ThrowIf(
      56             :                 _filename.empty(),
      57             :                 "Cannot create log file"
      58             :         );
      59          19 : }
      60             : 
      61          34 : LogFile::~LogFile() {}
      62             : 
      63          19 : Bool LogFile::open() {
      64             : 
      65          19 :         File log(_filename);
      66          19 :         switch (File::FileWriteStatus status = log.getWriteStatus()) {
      67           9 :         case File::OVERWRITABLE:
      68           9 :                 if (_append) {
      69           7 :                         _fileHandle = ::open(_filename.c_str(), O_RDWR | O_APPEND);
      70             :                 }
      71             :                 // no break here to fall through to the File::CREATABLE block if logFileAppend is false
      72             :         case File::CREATABLE:
      73          19 :                 if (status == File::CREATABLE || ! _append) {
      74             :                         // can fall throw from previous case block so status can be File::OVERWRITABLE
      75          12 :                         String action = (status == File::OVERWRITABLE) ? "Overwrote" : "Created";
      76          12 :                         _fileHandle = FiledesIO::create(_filename.c_str());
      77          12 :                 }
      78          19 :                 break;
      79           0 :         default:
      80           0 :                 ThrowCc(
      81             :                         "Programming logic error. This block should never be reached"
      82             :                 );
      83             :                 break;
      84             :         }
      85          19 :         _fileIO.reset(new FiledesIO(_fileHandle, _filename.c_str()));
      86          19 :         return true;
      87          19 : }
      88             : 
      89          19 : void LogFile::close() const {
      90          19 :         if (_fileHandle > 0) {
      91          19 :                 FiledesIO::close(_fileHandle);
      92             :         }
      93          19 : }
      94             : 
      95         371 : Bool LogFile::write(
      96             :         const String& output, const Bool openFile, const Bool closeFile
      97             : ) {
      98         371 :         if (openFile) {
      99          17 :                 if (! open()) {
     100           0 :                         return false;
     101             :                 }
     102             :         }
     103         371 :         _fileIO->write(output.length(), output.c_str());
     104         371 :         if (closeFile) {
     105          12 :                 close();
     106             :         }
     107         371 :         return true;
     108             : }
     109             : 
     110          30 : void LogFile::setAppend(Bool a) { _append = a; }
     111             : 
     112             : }
     113             : 

Generated by: LCOV version 1.16