Geant4  10.01.p03
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),
42  ignoreCout(false), ignoreInit(true)
43 {
46  prefix = "G4WT";
47 }
48 
50 {
51  if( useBuffer ) DumpBuffer();
54 }
55 
56 namespace { G4Mutex coutm = G4MUTEX_INITIALIZER; }
57 #include "G4StateManager.hh"
58 
60 {
61  if( threadCoutToFile )
62  { coutFile<<msg<<std::flush; }
63  else if( useBuffer )
64  { cout_buffer<<msg; }
65  else if( !ignoreCout )
66  {
67  if(!ignoreInit ||
68  G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
69  {
70  G4AutoLock l(&coutm);
73  finalcout<<" > "<<msg<<std::flush;
74  }
75  }
76  //forward message to master G4coutDestination if set
78  ( !ignoreInit || G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
79  ){
80  G4AutoLock l(&coutm);
81  std::stringstream ss;
82  ss<<prefix;
83  if ( id!=G4Threading::GENERICTHREAD_ID ) ss<<id;
84  ss<<" > "<<msg;
86  }
87  return 0;
88 }
89 
91 {
92  if( threadCerrToFile )
93  { cerrFile<<msg<<std::flush; }
94  if( useBuffer )
95  { cerr_buffer<<msg; }
96  else
97  { G4AutoLock l(&coutm);
100  finalcerr<<" > "<<msg<<std::flush;
101  }
102  //forward message to master G4coutDestination if set
104  ( !ignoreInit || G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
105  ){
106  G4AutoLock l(&coutm);
107  std::stringstream ss;
108  ss<<prefix;
109  if ( id!=G4Threading::GENERICTHREAD_ID ) ss<<id;
110  ss<<" > "<<msg;
112  }
113  return 0;
114 }
115 
117 {
119  if( fileN == "**Screen**" ) return;
120  if( ! coutFile.is_open() )
121  {
122  std::ios::openmode mode = std::ios::out;
123  if ( ifAppend ) mode |= std::ios::app;
124  coutFile.open(fileN,mode);
125  }
126  threadCoutToFile = true;
127 }
128 
130 {
132  if( fileN == "**Screen**" ) return;
133  if( ! cerrFile.is_open() )
134  {
135  std::ios::openmode mode = std::ios::out;
136  if ( ifAppend ) mode |= std::ios::app;
137  cerrFile.open(fileN,mode);
138  }
139  threadCerrToFile = true;
140 }
141 
143 {
144  if(useBuffer && !flag) DumpBuffer();
145  useBuffer = flag;
146 }
147 
149 { prefix = wd; }
150 
152 {
153  if(tid<0)
154  { ignoreCout = false; }
155  else
156  { ignoreCout = (tid!=id); }
157 }
158 
160 {
161  if( coutFile.is_open() ) coutFile.close();
162  threadCoutToFile = false;
163 }
164 
166 {
167  if( cerrFile.is_open() ) cerrFile.close();
168  threadCerrToFile = false;
169 }
170 
172 {
173  G4AutoLock l(&coutm);
174  finalcout<<"====================="<<std::endl;
175  finalcout<<"cout buffer for worker with ID:"<<id<<std::endl;
176  finalcout<<cout_buffer.str()<<std::endl;
177  finalcerr<<"====================="<<std::endl;
178  finalcerr<<"cerr buffer for worker with ID:"<<id<<std::endl;
179  finalcerr<<cerr_buffer.str()<<std::endl;
180  finalcerr<<"====================="<<std::endl;
181 }
182 
void SetCerrFileName(const G4String &fileN="G4cerr.txt", G4bool ifAppend=true)
virtual G4int ReceiveG4cout(const G4String &)
static G4coutDestination * masterG4coutDestination
int G4int
Definition: G4Types.hh:78
std::ostringstream cout_buffer
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
void SetPrefixString(const G4String &wd="G4WT")
void SetCoutFileName(const G4String &fileN="G4cout.txt", G4bool ifAppend=true)
static G4StateManager * GetStateManager()
G4strstreambuf G4coutbuf
Definition: G4ios.cc:82
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)
virtual G4int ReceiveG4cerr(const G4String &)
void SetIgnoreCout(G4int tid=0)
G4int G4Mutex
Definition: G4Threading.hh:173
void SetDestination(G4coutDestination *dest)
std::ostringstream cerr_buffer
virtual G4int ReceiveG4cerr(const G4String &)
virtual G4int ReceiveG4cout(const G4String &)
G4strstreambuf G4cerrbuf
Definition: G4ios.cc:83