Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4coutFormatters.cc
이 파일의 문서화 페이지로 가기
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 // $Id: G4coutFormatters.cc 103582 2017-04-18 17:24:45Z adotti $
27 //
28 // --------------------------------------------------------------------
29 //
30 // G4coutFormatters.cc
31 //
32 // Author: A.Dotti (SLAC), April 2017
33 // --------------------------------------------------------------------
34 
35 #include "G4coutFormatters.hh"
36 
37 namespace G4coutFormatters
38 {
39  // Internal functions and utilites used to setup default formatters
40  namespace
41  {
42  // Split a single string in an array of strings
43  String_V split(const G4String& input, char separator='\n')
44  {
45  String_V output;
46  G4String::size_type prev_pos=0, pos=0;
47  while( (pos=input.find(separator,pos))!=G4String::npos)
48  {
49  G4String substr( input.substr(prev_pos,pos-prev_pos)) ;
50  output.push_back(substr);
51  prev_pos = ++pos;
52  }
53  // output.push_back( input.substr(prev_pos,pos-prev_pos));
54  return output;
55  }
56 
57  // Return a syslog style message with input message, type identifies
58  // the type of the message
59  G4bool transform( G4String& input , const G4String& type)
60  {
61  std::time_t result = std::time(nullptr);
62  std::ostringstream newm;
63 #if __GNUC__ >= 5
64  newm << std::put_time(std::localtime(&result),"%d/%b/%Y:%H:%M:%S %z");
65 #else
66  std::tm* time_ = std::localtime(&result);
67  newm << time_->tm_mday << "/" << time_->tm_mon << "/" << time_->tm_year;
68  newm << ":" << time_->tm_hour << ":"<<time_->tm_min<<":"<<time_->tm_sec;
69 #endif
70  newm<<" "<<type<<" [";
71  G4String delimiter = "";
72  for (const auto& el : split(input) )
73  {
74  if ( !el.empty() )
75  {
76  newm << delimiter << el ;
77  delimiter = "\\n";
78  }
79  }
80  newm<<" ]"<<G4endl;
81  input = newm.str();
82  return true;
83  }
84 
85  // Style used in master thread
86  G4String masterStyle = "";
87 
88  // Modify output to look like syslog messages:
89  // DATE TIME **LOG|ERROR** [ "multi","line","message"]
90  SetupStyle_f SysLogStyle = [](G4coutDestination* dest)->G4int
91  {
92  if ( dest != nullptr )
93  {
94  dest->AddCoutTransformer(std::bind(&transform,std::placeholders::_1,
95  "INFO"));
96  dest->AddCerrTransformer(std::bind(&transform,std::placeholders::_1,
97  "ERROR"));
98  }
99  return 0;
100  };
101 
102  // Bring back destination to original state
103  SetupStyle_f DefaultStyle = [](G4coutDestination* dest)->G4int
104  {
105  if ( dest != nullptr )
106  {
107  dest->ResetTransformers();
108  }
109  return 0;
110  };
111 
112  std::unordered_map<std::string,SetupStyle_f> transformers =
113  {
114  {ID::SYSLOG,SysLogStyle},
115  {ID::DEFAULT,DefaultStyle}
116  };
117  }
118 
119  void SetMasterStyle(const G4String& news )
120  {
121  masterStyle = news;
122  }
123 
125  {
126  return masterStyle;
127  }
128 
129  void SetupStyleGlobally(const G4String& news)
130  {
131  static G4coutDestination ss;
136  }
137 
139  {
141  for ( const auto& el : transformers )
142  {
143  result.push_back(el.first);
144  }
145  return result;
146  }
147 
149  {
150  const auto& handler = transformers.find(style);
151  return ( handler != transformers.end() ) ? (handler->second)(dest) : -1;
152  }
153 
155  {
156  if ( transformers.find(name) != transformers.end() )
157  {
159  msg << "Format Style with name " << name
160  << " already exists. Replacing existing.";
161  G4Exception("G4coutFormatters::RegisterNewStyle()",
162  "FORMATTER001", JustWarning, msg);
163  }
164  // transformers.insert(std::make_pair(name,fmt));
165  transformers[name]=fmt;
166  }
167 }
std::vector< G4String > String_V
const XML_Char * name
Definition: expat.h:151
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
static const G4double pos
void RegisterNewStyle(const G4String &name, SetupStyle_f &formatter)
#define G4endl
Definition: G4ios.hh:61
void SetupStyleGlobally(const G4String &news)
bool G4bool
Definition: G4Types.hh:79
std::function< G4int(G4coutDestination *)> SetupStyle_f
G4strstreambuf G4coutbuf
Definition: G4ios.cc:104
static const G4String DEFAULT
void SetDestination(G4coutDestination *dest)
static const G4String SYSLOG
G4double G4ParticleHPJENDLHEData::G4double result
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4String GetMasterStyle()
int G4int
Definition: G4Types.hh:78
G4int HandleStyle(G4coutDestination *dest, const G4String &style)
void SetMasterStyle(const G4String &)
G4strstreambuf G4cerrbuf
Definition: G4ios.cc:105
int time_()