Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4BuffercoutDestination.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// G4BuffercoutDestination class implementation
27//
28// Author: A.Dotti (SLAC), 14 April 2017
29// --------------------------------------------------------------------
30
32
33#include "G4AutoLock.hh"
34
35#include <iostream>
36
37// Private class to implement buffering of logging via an ostringstream
39{
40 public:
41 using FlushFn_t = std::function<void(const std::string&)>;
42
43 public:
44 explicit BufferImpl(std::size_t maxSize) : m_maxSize(maxSize) {}
45 explicit BufferImpl(std::size_t maxSize, FlushFn_t&& f) : m_maxSize(maxSize), m_flushFn(f) {}
46
47 ~BufferImpl() = default;
48
49 // Set number of characters to hold before Flush() will be called
50 // If buffer exceeds new maximum, Flush() will not be called until next call to Receive()
51 void SetMaxSize(std::size_t n) { m_maxSize = n; }
52
53 // Reset buffer without flushing
54 void Reset()
55 {
56 m_buffer.str("");
57 m_buffer.clear();
58 m_currentSize = 0;
59 }
60
62 {
63 m_currentSize += msg.size();
64 m_buffer << msg;
65
66 if (m_maxSize > 0 && m_currentSize > m_maxSize) {
67 return Flush();
68 }
69 return 0;
70 }
71
72 // Flush buffer to destination and reset it
74 {
75 m_flushFn(m_buffer.str());
76 Reset();
77 return 0;
78 }
79
80 private:
81 std::size_t m_maxSize = 0;
82 std::ostringstream m_buffer;
83 std::size_t m_currentSize = 0;
84 FlushFn_t m_flushFn = [](auto& s) { std::cout << s << std::flush; };
85};
86
87// --------------------------------------------------------------------
89 : m_maxSize(max),
90 m_buffer_dbg(std::make_unique<BufferImpl>(max)),
91 m_buffer_out(std::make_unique<BufferImpl>(max)),
92 m_buffer_err(std::make_unique<BufferImpl>(max, [](auto& s) { std::cerr << s << std::flush; }))
93{}
94
95// --------------------------------------------------------------------
97
98// --------------------------------------------------------------------
105
106// --------------------------------------------------------------------
108{
109 return m_buffer_dbg->Receive(msg);
110}
111
112// --------------------------------------------------------------------
114{
115 return m_buffer_out->Receive(msg);
116}
117
118// --------------------------------------------------------------------
120{
121 return m_buffer_err->Receive(msg);
122}
123
124// --------------------------------------------------------------------
129
130// --------------------------------------------------------------------
135
136// --------------------------------------------------------------------
141
142// --------------------------------------------------------------------
144{
145 m_maxSize = max;
146 m_buffer_dbg->SetMaxSize(m_maxSize);
147 m_buffer_out->SetMaxSize(m_maxSize);
148 m_buffer_err->SetMaxSize(m_maxSize);
149}
int G4int
Definition G4Types.hh:85
std::function< void(const std::string &)> FlushFn_t
BufferImpl(std::size_t maxSize, FlushFn_t &&f)
std::unique_ptr< BufferImpl > m_buffer_err
std::unique_ptr< BufferImpl > m_buffer_out
std::unique_ptr< BufferImpl > m_buffer_dbg
G4int ReceiveG4debug(const G4String &msg) override
G4BuffercoutDestination(std::size_t maxSize=0)
G4int ReceiveG4cerr(const G4String &msg) override
G4int ReceiveG4cout(const G4String &msg) override