Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4MTcoutDestination Class Reference

#include <G4MTcoutDestination.hh>

+ Inheritance diagram for G4MTcoutDestination:

Public Member Functions

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

Protected Member Functions

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

Additional Inherited Members

- Public Types inherited from G4coutDestination
using Transformer = std::function<G4bool(G4String&)>
 
- Protected Attributes inherited from G4coutDestination
std::vector< TransformertransformersDebug
 
std::vector< TransformertransformersCout
 
std::vector< TransformertransformersCerr
 
- Static Protected Attributes inherited from G4coutDestination
static G4MTGLOB_DLL G4coutDestinationmasterG4coutDestination = nullptr
 

Detailed Description

Definition at line 45 of file G4MTcoutDestination.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 this out of here and in the caller
53
55 SetDefaultOutput(masterDestinationFlag, masterDestinationFmtFlag);
56}
void G4iosSetDestination(G4coutDestination *sink)
Definition G4ios.cc:270
void SetDefaultOutput(G4bool addMasterDestination=true, G4bool formatAlsoMaster=true)
static G4StateManager * GetStateManager()

◆ ~G4MTcoutDestination()

G4MTcoutDestination::~G4MTcoutDestination ( )
override

Definition at line 133 of file G4MTcoutDestination.cc.

134{
135 if(useBuffer)
136 {
137 DumpBuffer();
138 }
139}

Member Function Documentation

◆ AddCerrFileName()

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

Definition at line 262 of file G4MTcoutDestination.cc.

264{
265 HandleFileCerr(fileN, ifAppend, false);
266}
void HandleFileCerr(const G4String &fileN, G4bool appendFlag, G4bool suppressDefault)

◆ AddCoutFileName()

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

Definition at line 241 of file G4MTcoutDestination.cc.

243{
244 // This is like the equivalent SetCoutFileName, but in this case we do not
245 // remove or silence what is already exisiting
246 HandleFileCout(fileN, ifAppend, false);
247}
void HandleFileCout(const G4String &fileN, G4bool appendFlag, G4bool suppressDefault)

◆ 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 return !(
106 this->ignoreCout ||
107 (this->ignoreInit && this->stateMgr->GetCurrentState() == G4State_Idle));
108 };
109 forwarder->AddDebugTransformer(filter_out);
110 forwarder->AddCoutTransformer(filter_out);
111 if(formatAlsoMaster)
112 {
113 // Formatter: add prefix to each thread
114 const auto f = [this](G4String& msg) -> G4bool {
115 std::ostringstream str;
116 str << prefix;
118 {
119 str << id;
120 }
121 str << " > " << msg;
122 msg = str.str();
123 return true;
124 };
125 forwarder->AddDebugTransformer(f);
126 forwarder->AddCoutTransformer(f);
127 forwarder->AddCerrTransformer(f);
128 }
129 push_back(std::move(forwarder));
130}
@ G4State_Idle
std::unique_ptr< G4coutDestination > G4coutDestinationUPtr
bool G4bool
Definition G4Types.hh:86
const G4ApplicationState & GetCurrentState() const
void AddDebugTransformer(const Transformer &t)

Referenced by SetDefaultOutput().

◆ EnableBuffering()

void G4MTcoutDestination::EnableBuffering ( G4bool flag = true)

Definition at line 210 of file G4MTcoutDestination.cc.

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

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 }

◆ GetPrefixString()

G4String G4MTcoutDestination::GetPrefixString ( ) const
inline

Definition at line 72 of file G4MTcoutDestination.hh.

72{ return prefix; }

◆ HandleFileCerr()

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

Definition at line 176 of file G4MTcoutDestination.cc.

178{
179 // See HandleFileCout for explanation, switching cout with cerr
180
181 std::ios_base::openmode mode =
182 (ifAppend ? std::ios_base::app : std::ios_base::trunc);
183 auto output = G4coutDestinationUPtr(new G4FilecoutDestination(fileN, mode));
184 output->AddDebugTransformer([](G4String&) { return false; });
185 output->AddCoutTransformer([](G4String&) { return false; });
186 push_back(std::move(output));
187 if(suppressDefault)
188 {
189 ref_defaultOut->AddCerrTransformer([](G4String&) { return false; });
190 if(ref_masterOut != nullptr)
191 {
192 ref_masterOut->AddCerrTransformer([](G4String&) { return false; });
193 }
194 }
195}
void AddCerrTransformer(const Transformer &t)

Referenced by AddCerrFileName(), and SetCerrFileName().

◆ HandleFileCout()

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

Definition at line 149 of file G4MTcoutDestination.cc.

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

Referenced by AddCoutFileName(), and SetCoutFileName().

◆ Reset()

void G4MTcoutDestination::Reset ( )
virtual

Definition at line 142 of file G4MTcoutDestination.cc.

143{
144 clear();
145 SetDefaultOutput(masterDestinationFlag, masterDestinationFmtFlag);
146}

Referenced by EnableBuffering(), SetCerrFileName(), and SetCoutFileName().

◆ SetCerrFileName()

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

Definition at line 250 of file G4MTcoutDestination.cc.

252{
253 // See SetCoutFileName for explanation
254 Reset();
255 if(fileN != "**Screen**")
256 {
257 HandleFileCerr(fileN, ifAppend, true);
258 }
259}

Referenced by G4UImanager::SetCerrFileName().

◆ SetCoutFileName()

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

Definition at line 198 of file G4MTcoutDestination.cc.

200{
201 // First let's go back to the default
202 Reset();
203 if(fileN != "**Screen**")
204 {
205 HandleFileCout(fileN, ifAppend, true);
206 }
207}

Referenced by G4UImanager::SetCoutFileName().

◆ SetDefaultOutput()

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

Definition at line 59 of file G4MTcoutDestination.cc.

61{
62 masterDestinationFlag = addmasterDestination;
63 masterDestinationFmtFlag = formatAlsoMaster;
64 // Formatter: add prefix to each thread
65 const auto f = [this](G4String& msg) -> G4bool {
66 std::ostringstream str;
67 str << prefix;
69 {
70 str << id;
71 }
72 str << " > " << msg;
73 msg = str.str();
74 return true;
75 };
76 // Block cout if not in correct state
77 const auto filter_out = [this](G4String&) -> G4bool {
78 return !(
79 this->ignoreCout ||
80 (this->ignoreInit && this->stateMgr->GetCurrentState() == G4State_Init));
81 };
82
83 // Default behavior, add a destination that uses cout and uses a mutex
85 ref_defaultOut = output.get();
86 output->AddDebugTransformer(filter_out);
87 output->AddDebugTransformer(f);
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)

Referenced by G4MTcoutDestination(), and Reset().

◆ SetIgnoreCout()

void G4MTcoutDestination::SetIgnoreCout ( G4int tid = 0)

Definition at line 269 of file G4MTcoutDestination.cc.

270{
271 if(tid < 0)
272 {
273 ignoreCout = false;
274 }
275 else
276 {
277 ignoreCout = (tid != id);
278 }
279}

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; }

Referenced by G4UImanager::SetThreadIgnoreInit().

◆ SetPrefixString()

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

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