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 0 : LogFile::LogFile(const String& filename) : 42 0 : _filename(filename), _append(false), _fileHandle(-1), 43 0 : _fileIO() { 44 0 : ThrowIf( 45 : filename.empty(), 46 : "LogFile::LogFile(): file name cannot be empty" 47 : ); 48 0 : OutputDestinationChecker::OutputStruct logFile; 49 0 : logFile.label = "log file"; 50 0 : logFile.outputFile = &_filename; 51 0 : logFile.required = false; 52 0 : logFile.replaceable = true; 53 0 : LogIO log; 54 0 : OutputDestinationChecker::checkOutput(logFile, log); 55 0 : ThrowIf( 56 : _filename.empty(), 57 : "Cannot create log file" 58 : ); 59 0 : } 60 : 61 0 : LogFile::~LogFile() {} 62 : 63 0 : Bool LogFile::open() { 64 : 65 0 : File log(_filename); 66 0 : switch (File::FileWriteStatus status = log.getWriteStatus()) { 67 0 : case File::OVERWRITABLE: 68 0 : if (_append) { 69 0 : _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 0 : if (status == File::CREATABLE || ! _append) { 74 : // can fall throw from previous case block so status can be File::OVERWRITABLE 75 0 : String action = (status == File::OVERWRITABLE) ? "Overwrote" : "Created"; 76 0 : _fileHandle = FiledesIO::create(_filename.c_str()); 77 0 : } 78 0 : break; 79 0 : default: 80 0 : ThrowCc( 81 : "Programming logic error. This block should never be reached" 82 : ); 83 : break; 84 : } 85 0 : _fileIO.reset(new FiledesIO(_fileHandle, _filename.c_str())); 86 0 : return true; 87 0 : } 88 : 89 0 : void LogFile::close() const { 90 0 : if (_fileHandle > 0) { 91 0 : FiledesIO::close(_fileHandle); 92 : } 93 0 : } 94 : 95 0 : Bool LogFile::write( 96 : const String& output, const Bool openFile, const Bool closeFile 97 : ) { 98 0 : if (openFile) { 99 0 : if (! open()) { 100 0 : return false; 101 : } 102 : } 103 0 : _fileIO->write(output.length(), output.c_str()); 104 0 : if (closeFile) { 105 0 : close(); 106 : } 107 0 : return true; 108 : } 109 : 110 0 : void LogFile::setAppend(Bool a) { _append = a; } 111 : 112 : } 113 :