Branch data Line data Source code
1 : : // ***************************************************************************** 2 : : /*! 3 : : \file src/Base/Writer.cpp 4 : : \copyright 2012-2015 J. Bakosi, 5 : : 2016-2018 Los Alamos National Security, LLC., 6 : : 2019-2021 Triad National Security, LLC. 7 : : All rights reserved. See the LICENSE file for details. 8 : : \brief Writer base class definition 9 : : \details Writer base class definition. Writer base serves as a base class 10 : : for various file writers. It does generic low-level I/O, e.g., opening and 11 : : closing a file, and associated error handling. 12 : : */ 13 : : // ***************************************************************************** 14 : : 15 : : #include <string> 16 : : #include <exception> 17 : : #include <cstdio> 18 : : 19 : : #include "Writer.hpp" 20 : : #include "Exception.hpp" 21 : : 22 : : using tk::Writer; 23 : : 24 : 398790 : Writer::Writer( const std::string& filename, std::ios_base::openmode mode ) : 25 [ + - ]: 398790 : m_filename( filename ), m_outFile() 26 : : // ***************************************************************************** 27 : : // Constructor: Acquire file handle 28 : : //! \param[in] filename Name of file to open for writing 29 : : //! \param[in] mode Open mode, see 30 : : //! http://en.cppreference.com/w/cpp/io/ios_base/openmode 31 : : // ***************************************************************************** 32 : : { 33 : : // Doing this if-test gives the derived class an option to pass an empty 34 : : // string in case the file does not need to be opened, because e.g., there is 35 : : // no data that needs to be written, without contaminating client-code with 36 : : // this if-test. 37 [ + + ]: 398790 : if (!m_filename.empty()) { 38 [ + - ]: 368783 : m_outFile.open( m_filename, mode ); 39 [ + - ][ - + ]: 368783 : ErrChk( m_outFile.good(), "Failed to open file: " + m_filename ); [ - - ][ - - ] [ - - ] 40 : : } 41 : 398790 : } 42 : : 43 : 398790 : Writer::~Writer() noexcept 44 : : // ***************************************************************************** 45 : : // Destructor: Release file handle 46 : : //! \details Exception safety: no-throw guarantee: never throws exceptions. 47 : : //! Error handling, while done by throwing and catching exceptions, results in 48 : : //! warnings to terminal. We use C-style printf, since that will not throw 49 : : //! exceptions. 50 : : // ***************************************************************************** 51 : : { 52 [ + + ]: 398790 : if (!m_filename.empty()) { 53 : : try { 54 : : 55 [ + - ]: 368783 : m_outFile.close(); 56 : : 57 [ + - ][ - + ]: 368783 : if ( m_outFile.fail() ) 58 [ - - ]: 0 : printf( ">>> WARNING: Failed to close file: %s\n", m_filename.c_str() ); 59 : : 60 : : } // emit only a warning on error 61 [ - - ][ - ]: 0 : catch ( Exception& e ) { 62 : 0 : e.handleException(); 63 : : } 64 : 0 : catch ( std::exception& e ) { 65 : 0 : printf( ">>> WARNING: std::exception in Writer destructor: %s\n", 66 : 0 : e.what() ); 67 : : } 68 : 0 : catch (...) { 69 : 0 : printf( ">>> WARNING: UNKNOWN EXCEPTION in Writer destructor\n" ); 70 : : } 71 : : } 72 : 398790 : }