Geant4-11
G4FilecoutDestination.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4FilecoutDestination class implementation
27//
28// Author: A.Dotti (SLAC), April 2017
29// --------------------------------------------------------------------
30
32
33#include <ios>
34
35// --------------------------------------------------------------------
37{
38 Close();
39 if(m_output)
40 m_output.reset();
41}
42
43// --------------------------------------------------------------------
44void G4FilecoutDestination::Open(std::ios_base::openmode mode)
45{
46 if(m_name.empty())
47 {
48#ifndef __MIC
49 // Cannot use G4Exception, because G4cout/G4cerr is not setup
50 throw std::ios_base::failure("No output file name specified");
51#endif
52 }
53 if(m_output != nullptr && m_output->is_open())
54 Close();
55 m_output.reset(new std::ofstream(m_name, std::ios_base::out | mode));
56}
57
58// --------------------------------------------------------------------
60{
61 if(m_output && m_output->is_open())
62 {
63 m_output->close();
64 }
65}
66
67// --------------------------------------------------------------------
69{
70 if(m_output == nullptr || !m_output->is_open())
71 Open(m_mode);
72 *m_output << msg;
73 return 0;
74}
75
76// --------------------------------------------------------------------
78{
79 if(m_output == nullptr || !m_output->is_open())
80 Open(m_mode);
81 *m_output << msg;
82 return 0;
83}
int G4int
Definition: G4Types.hh:85
std::ios_base::openmode m_mode
void Open(std::ios_base::openmode mode=std::ios_base::app)
virtual G4int ReceiveG4cout(const G4String &msg) override
virtual G4int ReceiveG4cerr(const G4String &msg) override
std::unique_ptr< std::ofstream > m_output