Geant4-11
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes
G4MTcoutDestination Class Reference

#include <G4MTcoutDestination.hh>

Inheritance diagram for G4MTcoutDestination:
G4MulticoutDestination G4coutDestination

Public Types

using Transformer = std::function< G4bool(G4String &)>
 

Public Member Functions

void AddCerrFileName (const G4String &fileN="G4cerr.txt", G4bool ifAppend=true)
 
void AddCerrTransformer (const Transformer &t)
 
void AddCerrTransformer (Transformer &&t)
 
void AddCoutFileName (const G4String &fileN="G4cout.txt", G4bool ifAppend=true)
 
void AddCoutTransformer (const Transformer &t)
 
void AddCoutTransformer (Transformer &&t)
 
void EnableBuffering (G4bool flag=true)
 
 G4MTcoutDestination (const G4int &threadId)
 
G4String GetFullPrefixString () const
 
G4String GetPrefixString () const
 
virtual G4int ReceiveG4cerr (const G4String &msg) override
 
G4int ReceiveG4cerr_ (const G4String &msg)
 
virtual G4int ReceiveG4cout (const G4String &msg) override
 
G4int ReceiveG4cout_ (const G4String &msg)
 
virtual void Reset ()
 
virtual void ResetTransformers ()
 
void SetCerrFileName (const G4String &fileN="G4cerr.txt", G4bool ifAppend=true)
 
void SetCoutFileName (const G4String &fileN="G4cout.txt", G4bool ifAppend=true)
 
void SetDefaultOutput (G4bool addMasterDestination=true, G4bool formatAlsoMaster=true)
 
void SetIgnoreCout (G4int tid=0)
 
void SetIgnoreInit (G4bool val=true)
 
void SetPrefixString (const G4String &wd="G4WT")
 
virtual ~G4MTcoutDestination ()
 

Protected Member Functions

void AddMasterOutput (G4bool formatAlsoMaster)
 
void HandleFileCerr (G4String fileN, G4bool appendFlag, G4bool suppressDefault)
 
void HandleFileCout (G4String fileN, G4bool appendFlag, G4bool suppressDefault)
 

Protected Attributes

std::vector< TransformertransformersCerr
 
std::vector< TransformertransformersCout
 

Static Protected Attributes

static G4MTGLOB_DLL G4coutDestinationmasterG4coutDestination = nullptr
 

Private Member Functions

void DumpBuffer ()
 

Private Attributes

const G4int id
 
G4bool ignoreCout = false
 
G4bool ignoreInit = true
 
G4bool masterDestinationFlag = true
 
G4bool masterDestinationFmtFlag = true
 
G4String prefix = "G4WT"
 
G4coutDestinationref_defaultOut = nullptr
 
G4coutDestinationref_masterOut = nullptr
 
G4StateManagerstateMgr = nullptr
 
G4bool useBuffer = false
 

Detailed Description

Definition at line 45 of file G4MTcoutDestination.hh.

Member Typedef Documentation

◆ Transformer

Definition at line 61 of file G4coutDestination.hh.

Constructor & Destructor Documentation

◆ G4MTcoutDestination()

G4MTcoutDestination::G4MTcoutDestination ( const G4int threadId)
explicit

Definition at line 48 of file G4MTcoutDestination.cc.

49 : id(threadId)
50{
51 // TODO: Move these two out of here and in the caller
54
57}
G4GLOB_DLL G4strstreambuf G4coutbuf
Definition: G4ios.cc:108
G4GLOB_DLL G4strstreambuf G4cerrbuf
Definition: G4ios.cc:109
void SetDefaultOutput(G4bool addMasterDestination=true, G4bool formatAlsoMaster=true)
G4StateManager * stateMgr
static G4StateManager * GetStateManager()
void SetDestination(G4coutDestination *dest)

References G4cerrbuf, G4coutbuf, G4StateManager::GetStateManager(), masterDestinationFlag, masterDestinationFmtFlag, SetDefaultOutput(), G4strstreambuf::SetDestination(), and stateMgr.

◆ ~G4MTcoutDestination()

G4MTcoutDestination::~G4MTcoutDestination ( )
virtual

Definition at line 132 of file G4MTcoutDestination.cc.

References DumpBuffer(), and useBuffer.

Member Function Documentation

◆ AddCerrFileName()

void G4MTcoutDestination::AddCerrFileName ( const G4String fileN = "G4cerr.txt",
G4bool  ifAppend = true 
)

Definition at line 254 of file G4MTcoutDestination.cc.

256{
257 HandleFileCerr(fileN, ifAppend, false);
258}
void HandleFileCerr(G4String fileN, G4bool appendFlag, G4bool suppressDefault)

References HandleFileCerr().

◆ AddCerrTransformer() [1/2]

void G4coutDestination::AddCerrTransformer ( const Transformer t)
inlineinherited

Definition at line 67 of file G4coutDestination.hh.

68 {
69 transformersCerr.push_back(t);
70 }
std::vector< Transformer > transformersCerr

References G4coutDestination::transformersCerr.

Referenced by HandleFileCerr().

◆ AddCerrTransformer() [2/2]

void G4coutDestination::AddCerrTransformer ( Transformer &&  t)
inlineinherited

Definition at line 71 of file G4coutDestination.hh.

71{ transformersCerr.push_back(t); }

References G4coutDestination::transformersCerr.

◆ AddCoutFileName()

void G4MTcoutDestination::AddCoutFileName ( const G4String fileN = "G4cout.txt",
G4bool  ifAppend = true 
)

Definition at line 233 of file G4MTcoutDestination.cc.

235{
236 // This is like the equivalent SetCoutFileName, but in this case we do not
237 // remove or silence what is already exisiting
238 HandleFileCout(fileN, ifAppend, false);
239}
void HandleFileCout(G4String fileN, G4bool appendFlag, G4bool suppressDefault)

References HandleFileCout().

◆ AddCoutTransformer() [1/2]

void G4coutDestination::AddCoutTransformer ( const Transformer t)
inlineinherited

Definition at line 62 of file G4coutDestination.hh.

63 {
64 transformersCout.push_back(t);
65 }
std::vector< Transformer > transformersCout

References G4coutDestination::transformersCout.

Referenced by AddMasterOutput(), HandleFileCout(), and SetDefaultOutput().

◆ AddCoutTransformer() [2/2]

void G4coutDestination::AddCoutTransformer ( Transformer &&  t)
inlineinherited

Definition at line 66 of file G4coutDestination.hh.

66{ transformersCout.push_back(t); }

References G4coutDestination::transformersCout.

◆ AddMasterOutput()

void G4MTcoutDestination::AddMasterOutput ( G4bool  formatAlsoMaster)
protected

Definition at line 99 of file G4MTcoutDestination.cc.

100{
101 // Add a destination, that forwards the message to the master thread
103 ref_masterOut = forwarder.get();
104 const auto filter_out = [this](G4String&) -> G4bool {
105 if(this->ignoreCout ||
106 (this->ignoreInit && this->stateMgr->GetCurrentState() == G4State_Idle))
107 {
108 return false;
109 }
110 return true;
111 };
112 forwarder->AddCoutTransformer(filter_out);
113 if(formatAlsoMaster)
114 {
115 // Formatter: add prefix to each thread
116 const auto f = [this](G4String& msg) -> G4bool {
117 std::ostringstream str;
118 str << prefix;
120 str << id;
121 str << " > " << msg;
122 msg = str.str();
123 return true;
124 };
125 forwarder->AddCoutTransformer(f);
126 forwarder->AddCerrTransformer(f);
127 }
128 push_back(std::move(forwarder));
129}
@ G4State_Idle
std::unique_ptr< G4coutDestination > G4coutDestinationUPtr
bool G4bool
Definition: G4Types.hh:86
G4coutDestination * ref_masterOut
const G4ApplicationState & GetCurrentState() const
void AddCoutTransformer(const Transformer &t)

References G4coutDestination::AddCoutTransformer(), G4State_Idle, G4Threading::GENERICTHREAD_ID, G4StateManager::GetCurrentState(), id, ignoreCout, ignoreInit, prefix, ref_masterOut, and stateMgr.

Referenced by SetDefaultOutput().

◆ DumpBuffer()

void G4MTcoutDestination::DumpBuffer ( )
private

Definition at line 279 of file G4MTcoutDestination.cc.

280{
281 G4AutoLock l(&coutm);
282 std::ostringstream msg;
283 msg << "=======================\n";
284 msg << "cout buffer(s) for worker with ID:" << id << std::endl;
286 G4bool sep = false;
287 std::for_each(begin(), end(), [this, &sep](G4coutDestinationUPtr& el) {
288 auto cout = dynamic_cast<G4BuffercoutDestination*>(el.get());
289 if(cout != nullptr)
290 {
291 cout->FlushG4cout();
292 if(sep)
293 {
294 G4coutDestination::ReceiveG4cout("==========\n");
295 }
296 else
297 {
298 sep = true;
299 }
300 }
301 });
302 sep = false;
303 msg.str("");
304 msg.clear();
305 msg << "=======================\n";
306 msg << "cerr buffer(s) for worker with ID:" << id << " (goes to std error)"
307 << std::endl;
309 std::for_each(begin(), end(), [this, &sep](G4coutDestinationUPtr& el) {
310 auto cout = dynamic_cast<G4BuffercoutDestination*>(el.get());
311 if(cout != nullptr)
312 {
313 cout->FlushG4cerr();
314 if(sep)
315 {
316 G4coutDestination::ReceiveG4cout("==========\n");
317 }
318 else
319 {
320 sep = true;
321 }
322 }
323 });
324 G4coutDestination::ReceiveG4cout("=======================\n");
325}
virtual G4int ReceiveG4cout(const G4String &msg)

References anonymous_namespace{G4MTcoutDestination.cc}::coutm, G4BuffercoutDestination::FlushG4cerr(), G4BuffercoutDestination::FlushG4cout(), and G4coutDestination::ReceiveG4cout().

Referenced by EnableBuffering(), and ~G4MTcoutDestination().

◆ EnableBuffering()

void G4MTcoutDestination::EnableBuffering ( G4bool  flag = true)

Definition at line 202 of file G4MTcoutDestination.cc.

203{
204 // I was using buffered output and now I want to turn it off, dump current
205 // buffer content and reset output
206 if(useBuffer && !flag)
207 {
208 DumpBuffer();
209 Reset();
210 }
211 else if(useBuffer && flag)
212 { /* do nothing: already using */
213 }
214 else if(!useBuffer && !flag)
215 { /* do nothing: not using */
216 }
217 else if(!useBuffer && flag)
218 {
219 // Remove everything, in this case also removing the forward to the master
220 // thread, we want everything to be dumpled to a file
221 clear();
222 const size_t infiniteSize = 0;
223 push_back(G4coutDestinationUPtr(new G4BuffercoutDestination(infiniteSize)));
224 }
225 else // Should never happen
226 {
227 assert(false);
228 }
229 useBuffer = flag;
230}

References DumpBuffer(), Reset(), and useBuffer.

Referenced by G4UImanager::SetThreadUseBuffer().

◆ GetFullPrefixString()

G4String G4MTcoutDestination::GetFullPrefixString ( ) const
inline

Definition at line 73 of file G4MTcoutDestination.hh.

74 {
75 std::stringstream os;
76 os << prefix << id;
77 return os.str();
78 }

References id.

Referenced by G4UIQt::GetThreadPrefix().

◆ GetPrefixString()

G4String G4MTcoutDestination::GetPrefixString ( ) const
inline

Definition at line 72 of file G4MTcoutDestination.hh.

72{ return prefix; }

References prefix.

Referenced by G4UIQt::GetThreadPrefix().

◆ HandleFileCerr()

void G4MTcoutDestination::HandleFileCerr ( G4String  fileN,
G4bool  appendFlag,
G4bool  suppressDefault 
)
protected

Definition at line 171 of file G4MTcoutDestination.cc.

173{
174 // See HandleFileCout for explanation, switching cout with cerr
175
176 std::ios_base::openmode mode =
177 (ifAppend ? std::ios_base::app : std::ios_base::trunc);
178 auto output = G4coutDestinationUPtr(new G4FilecoutDestination(fileN, mode));
179 output->AddCoutTransformer([](G4String&) { return false; });
180 push_back(std::move(output));
181 if(suppressDefault)
182 {
183 ref_defaultOut->AddCerrTransformer([](G4String&) { return false; });
184 if(ref_masterOut)
185 ref_masterOut->AddCerrTransformer([](G4String&) { return false; });
186 }
187}
G4coutDestination * ref_defaultOut
void AddCerrTransformer(const Transformer &t)
app
Definition: demo.py:189

References G4coutDestination::AddCerrTransformer(), demo::app, ref_defaultOut, and ref_masterOut.

Referenced by AddCerrFileName(), and SetCerrFileName().

◆ HandleFileCout()

void G4MTcoutDestination::HandleFileCout ( G4String  fileN,
G4bool  appendFlag,
G4bool  suppressDefault 
)
protected

Definition at line 146 of file G4MTcoutDestination.cc.

148{
149 // Logic: we create a file destination. We want this to get only the G4cout
150 // stream and should discard everything in G4cerr.
151 // First we create the destination with the appropriate open mode
152
153 std::ios_base::openmode mode =
154 (ifAppend ? std::ios_base::app : std::ios_base::trunc);
155 auto output = G4coutDestinationUPtr(new G4FilecoutDestination(fileN, mode));
156
157 // This reacts only to G4cout, so let's make a filter that removes everything
158 // from G4cerr
159 output->AddCerrTransformer([](G4String&) { return false; });
160 push_back(std::move(output));
161 // Silence G4cout from default formatter
162 if(suppressDefault)
163 {
164 ref_defaultOut->AddCoutTransformer([](G4String&) { return false; });
165 if(ref_masterOut)
166 ref_masterOut->AddCoutTransformer([](G4String&) { return false; });
167 }
168}

References G4coutDestination::AddCoutTransformer(), demo::app, ref_defaultOut, and ref_masterOut.

Referenced by AddCoutFileName(), and SetCoutFileName().

◆ ReceiveG4cerr()

virtual G4int G4MulticoutDestination::ReceiveG4cerr ( const G4String msg)
inlineoverridevirtualinherited

Reimplemented from G4coutDestination.

Definition at line 79 of file G4MulticoutDestination.hh.

80 {
81 G4bool result = true;
82 std::for_each(begin(), end(), [&](G4coutDestinationUPtr& e) {
83 result &= (e->ReceiveG4cerr_(msg) == 0);
84 });
85 return (result ? 0 : -1);
86 }

◆ ReceiveG4cerr_()

G4int G4coutDestination::ReceiveG4cerr_ ( const G4String msg)
inherited

Definition at line 84 of file G4coutDestination.cc.

85{
86 if(transformersCout.size() > 0)
87 {
88 G4String m = msg;
89 std::for_each(transformersCerr.begin(), transformersCerr.end(),
90 [&m](const Transformer& t) { t(m); }
91 // Call transforming function on message
92 );
93 return ReceiveG4cerr(m);
94 }
95 else
96 {
97 return ReceiveG4cerr(msg);
98 }
99}
static constexpr double m
Definition: G4SIunits.hh:109
virtual G4int ReceiveG4cerr(const G4String &msg)
std::function< G4bool(G4String &)> Transformer

References m, G4coutDestination::ReceiveG4cerr(), G4coutDestination::transformersCerr, and G4coutDestination::transformersCout.

Referenced by G4MasterForwardcoutDestination::ReceiveG4cerr().

◆ ReceiveG4cout()

virtual G4int G4MulticoutDestination::ReceiveG4cout ( const G4String msg)
inlineoverridevirtualinherited

Reimplemented from G4coutDestination.

Definition at line 70 of file G4MulticoutDestination.hh.

71 {
72 G4bool result = true;
73 std::for_each(begin(), end(), [&](G4coutDestinationUPtr& e) {
74 result &= (e->ReceiveG4cout_(msg) == 0);
75 });
76 return (result ? 0 : -1);
77 }

◆ ReceiveG4cout_()

G4int G4coutDestination::ReceiveG4cout_ ( const G4String msg)
inherited

Definition at line 62 of file G4coutDestination.cc.

63{
64 // Avoid copy of string if not necessary
65 if(transformersCout.size() > 0)
66 {
67 G4String m = msg;
68 G4bool result = true;
69 for(const auto& el : transformersCout)
70 {
71 result &= el(m);
72 if(!result)
73 break;
74 }
75 return (result ? ReceiveG4cout(m) : 0);
76 }
77 else
78 {
79 return ReceiveG4cout(msg);
80 }
81}

References m, G4coutDestination::ReceiveG4cout(), and G4coutDestination::transformersCout.

Referenced by G4MasterForwardcoutDestination::ReceiveG4cout().

◆ Reset()

void G4MTcoutDestination::Reset ( )
virtual

◆ ResetTransformers()

void G4coutDestination::ResetTransformers ( )
virtualinherited

Definition at line 41 of file G4coutDestination.cc.

42{
43 transformersCout.clear();
44 transformersCerr.clear();
45}

References G4coutDestination::transformersCerr, and G4coutDestination::transformersCout.

◆ SetCerrFileName()

void G4MTcoutDestination::SetCerrFileName ( const G4String fileN = "G4cerr.txt",
G4bool  ifAppend = true 
)

Definition at line 242 of file G4MTcoutDestination.cc.

244{
245 // See SetCoutFileName for explanation
246 Reset();
247 if(fileN != "**Screen**")
248 {
249 HandleFileCerr(fileN, ifAppend, true);
250 }
251}

References HandleFileCerr(), and Reset().

Referenced by G4UImanager::SetCerrFileName().

◆ SetCoutFileName()

void G4MTcoutDestination::SetCoutFileName ( const G4String fileN = "G4cout.txt",
G4bool  ifAppend = true 
)

Definition at line 190 of file G4MTcoutDestination.cc.

192{
193 // First let's go back to the default
194 Reset();
195 if(fileN != "**Screen**")
196 {
197 HandleFileCout(fileN, ifAppend, true);
198 }
199}

References HandleFileCout(), and Reset().

Referenced by G4UImanager::SetCoutFileName().

◆ SetDefaultOutput()

void G4MTcoutDestination::SetDefaultOutput ( G4bool  addMasterDestination = true,
G4bool  formatAlsoMaster = true 
)

Definition at line 60 of file G4MTcoutDestination.cc.

62{
63 masterDestinationFlag = addmasterDestination;
64 masterDestinationFmtFlag = formatAlsoMaster;
65 // Formatter: add prefix to each thread
66 const auto f = [this](G4String& msg) -> G4bool {
67 std::ostringstream str;
68 str << prefix;
70 str << id;
71 str << " > " << msg;
72 msg = str.str();
73 return true;
74 };
75 // Block cout if not in correct state
76 const auto filter_out = [this](G4String&) -> G4bool {
77 if(this->ignoreCout ||
78 (this->ignoreInit && this->stateMgr->GetCurrentState() == G4State_Init))
79 {
80 return false;
81 }
82 return true;
83 };
84
85 // Default behavior, add a destination that uses cout and uses a mutex
87 ref_defaultOut = output.get();
88 output->AddCoutTransformer(filter_out);
89 output->AddCoutTransformer(f);
90 output->AddCerrTransformer(f);
91 push_back(std::move(output));
92 if(addmasterDestination)
93 {
94 AddMasterOutput(formatAlsoMaster);
95 }
96}
@ G4State_Init
void AddMasterOutput(G4bool formatAlsoMaster)

References G4coutDestination::AddCoutTransformer(), AddMasterOutput(), G4State_Init, G4Threading::GENERICTHREAD_ID, G4StateManager::GetCurrentState(), id, ignoreCout, ignoreInit, masterDestinationFlag, masterDestinationFmtFlag, prefix, ref_defaultOut, and stateMgr.

Referenced by G4MTcoutDestination(), and Reset().

◆ SetIgnoreCout()

void G4MTcoutDestination::SetIgnoreCout ( G4int  tid = 0)

Definition at line 261 of file G4MTcoutDestination.cc.

262{
263 if(tid < 0)
264 {
265 ignoreCout = false;
266 }
267 else
268 {
269 ignoreCout = (tid != id);
270 }
271}

References id, and ignoreCout.

Referenced by G4UImanager::SetThreadIgnore(), G4UImanager::SetUpForAThread(), and G4UImanager::SetUpForSpecialThread().

◆ SetIgnoreInit()

void G4MTcoutDestination::SetIgnoreInit ( G4bool  val = true)
inline

Definition at line 70 of file G4MTcoutDestination.hh.

70{ ignoreInit = val; }

References ignoreInit.

Referenced by G4UImanager::SetThreadIgnoreInit().

◆ SetPrefixString()

void G4MTcoutDestination::SetPrefixString ( const G4String wd = "G4WT")
inline

Field Documentation

◆ id

const G4int G4MTcoutDestination::id
private

◆ ignoreCout

G4bool G4MTcoutDestination::ignoreCout = false
private

Definition at line 101 of file G4MTcoutDestination.hh.

Referenced by AddMasterOutput(), SetDefaultOutput(), and SetIgnoreCout().

◆ ignoreInit

G4bool G4MTcoutDestination::ignoreInit = true
private

Definition at line 102 of file G4MTcoutDestination.hh.

Referenced by AddMasterOutput(), SetDefaultOutput(), and SetIgnoreInit().

◆ masterDestinationFlag

G4bool G4MTcoutDestination::masterDestinationFlag = true
private

Definition at line 96 of file G4MTcoutDestination.hh.

Referenced by G4MTcoutDestination(), Reset(), and SetDefaultOutput().

◆ masterDestinationFmtFlag

G4bool G4MTcoutDestination::masterDestinationFmtFlag = true
private

Definition at line 97 of file G4MTcoutDestination.hh.

Referenced by G4MTcoutDestination(), Reset(), and SetDefaultOutput().

◆ masterG4coutDestination

G4coutDestination * G4coutDestination::masterG4coutDestination = nullptr
staticprotectedinherited

◆ prefix

G4String G4MTcoutDestination::prefix = "G4WT"
private

Definition at line 104 of file G4MTcoutDestination.hh.

Referenced by AddMasterOutput(), GetPrefixString(), and SetDefaultOutput().

◆ ref_defaultOut

G4coutDestination* G4MTcoutDestination::ref_defaultOut = nullptr
private

Definition at line 92 of file G4MTcoutDestination.hh.

Referenced by HandleFileCerr(), HandleFileCout(), and SetDefaultOutput().

◆ ref_masterOut

G4coutDestination* G4MTcoutDestination::ref_masterOut = nullptr
private

Definition at line 95 of file G4MTcoutDestination.hh.

Referenced by AddMasterOutput(), HandleFileCerr(), and HandleFileCout().

◆ stateMgr

G4StateManager* G4MTcoutDestination::stateMgr = nullptr
private

Definition at line 105 of file G4MTcoutDestination.hh.

Referenced by AddMasterOutput(), G4MTcoutDestination(), and SetDefaultOutput().

◆ transformersCerr

std::vector<Transformer> G4coutDestination::transformersCerr
protectedinherited

◆ transformersCout

std::vector<Transformer> G4coutDestination::transformersCout
protectedinherited

◆ useBuffer

G4bool G4MTcoutDestination::useBuffer = false
private

Definition at line 100 of file G4MTcoutDestination.hh.

Referenced by EnableBuffering(), and ~G4MTcoutDestination().


The documentation for this class was generated from the following files: