Geant4_10
G4MTcoutDestination.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 //
27 // $Id: G4MTcoutDestination.cc 66241 2012-12-13 18:34:42Z gunter $
28 //
29 //
30 // ----------------------------------------------------------------------
31 // G4MTcoutDestination
32 //
33 
34 #include "G4MTcoutDestination.hh"
35 #include "G4strstreambuf.hh"
36 #include "G4AutoLock.hh"
37 
39  std::ostream& co, std::ostream& ce)
40 : finalcout(co), finalcerr(ce), id(threadId), useBuffer(false),
41  threadCoutToFile(false), threadCerrToFile(false), ignoreCout(false)
42 {
45  prefix = "G4WT";
46 }
47 
49 {
50  if( useBuffer ) DumpBuffer();
51  if( threadCoutToFile ) CloseCoutFile();
52  if( threadCerrToFile ) CloseCerrFile();
53 }
54 
55 namespace { G4Mutex coutm = G4MUTEX_INITIALIZER; }
56 
58 {
59  if( threadCoutToFile )
60  { coutFile<<msg<<std::flush; }
61  else if( useBuffer )
62  { cout_buffer<<msg; }
63  else if( !ignoreCout )
64  { G4AutoLock l(&coutm); finalcout<<prefix<<id<<" > "<<msg; }
65  return 0;
66 }
67 
69 {
70  if( threadCerrToFile )
71  { cerrFile<<msg<<std::flush; }
72  if( useBuffer )
73  { cerr_buffer<<msg; }
74  else
75  { G4AutoLock l(&coutm); finalcerr<<prefix<<id<<" > "<<msg; }
76  return 0;
77 }
78 
80 {
81  if( threadCoutToFile ) CloseCoutFile();
82  if( fileN == "**Screen**" ) return;
83  if( ! coutFile.is_open() )
84  {
85  std::ios::openmode mode = std::ios::out;
86  if ( ifAppend ) mode |= std::ios::app;
87  coutFile.open(fileN,mode);
88  }
89  threadCoutToFile = true;
90 }
91 
93 {
94  if( threadCerrToFile ) CloseCerrFile();
95  if( fileN == "**Screen**" ) return;
96  if( ! cerrFile.is_open() )
97  {
98  std::ios::openmode mode = std::ios::out;
99  if ( ifAppend ) mode |= std::ios::app;
100  cerrFile.open(fileN,mode);
101  }
102  threadCerrToFile = true;
103 }
104 
106 {
107  if(useBuffer && !flag) DumpBuffer();
108  useBuffer = flag;
109 }
110 
112 { prefix = wd; }
113 
115 {
116  if(tid<0)
117  { ignoreCout = false; }
118  else
119  { ignoreCout = (tid!=id); }
120 }
121 
122 void G4MTcoutDestination::CloseCoutFile()
123 {
124  if( coutFile.is_open() ) coutFile.close();
125  threadCoutToFile = false;
126 }
127 
128 void G4MTcoutDestination::CloseCerrFile()
129 {
130  if( cerrFile.is_open() ) cerrFile.close();
131  threadCerrToFile = false;
132 }
133 
134 void G4MTcoutDestination::DumpBuffer()
135 {
136  G4AutoLock l(&coutm);
137  finalcout<<"====================="<<std::endl;
138  finalcout<<"cout buffer for worker with ID:"<<id<<std::endl;
139  finalcout<<cout_buffer.str()<<std::endl;
140  finalcerr<<"====================="<<std::endl;
141  finalcerr<<"cerr buffer for worker with ID:"<<id<<std::endl;
142  finalcerr<<cerr_buffer.str()<<std::endl;
143  finalcerr<<"====================="<<std::endl;
144 }
145 
tuple app
Definition: testem0.py:189
void SetCerrFileName(const G4String &fileN="G4cerr.txt", G4bool ifAppend=true)
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
void SetPrefixString(const G4String &wd="G4WT")
void SetCoutFileName(const G4String &fileN="G4cout.txt", G4bool ifAppend=true)
G4strstreambuf G4coutbuf
Definition: G4ios.cc:70
bool G4bool
Definition: G4Types.hh:79
G4MTcoutDestination(const G4int &threadId, std::ostream &co=std::cout, std::ostream &ce=std::cerr)
void EnableBuffering(G4bool flag=true)
void SetIgnoreCout(G4int tid=0)
G4int G4Mutex
Definition: G4Threading.hh:156
void SetDestination(G4coutDestination *dest)
virtual G4int ReceiveG4cerr(const G4String &)
virtual G4int ReceiveG4cout(const G4String &)
G4strstreambuf G4cerrbuf
Definition: G4ios.cc:71