Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4INCLLogger.hh
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 // INCL++ intra-nuclear cascade model
27 // Alain Boudard, CEA-Saclay, France
28 // Joseph Cugnon, University of Liege, Belgium
29 // Jean-Christophe David, CEA-Saclay, France
30 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
31 // Sylvie Leray, CEA-Saclay, France
32 // Davide Mancusi, CEA-Saclay, France
33 //
34 #define INCLXX_IN_GEANT4_MODE 1
35 
36 #include "globals.hh"
37 
38 #ifndef G4INCLLogger_hh
39 #define G4INCLLogger_hh 1
40 
41 #include <iostream>
42 #include <fstream>
43 #include <sstream>
44 #include <string>
45 #include <cstdlib>
46 
47 #ifdef INCLXX_IN_GEANT4_MODE
48 #include "G4ios.hh"
49 #endif
50 
51 #include "G4INCLRandom.hh"
52 #include "G4INCLConfig.hh"
53 
54 namespace G4INCL {
55 
59  enum MessageType { InfoMsg = 1,
60  FatalMsg = 2,
61  ErrorMsg = 3,
63  DebugMsg = 7,
65  ZeroMsg = 0 };
66 
67 #if defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
68 
69  class LoggerSlave {
70  public:
71  // By default, log fatal errors, errors and warnings
72  LoggerSlave(std::string const &logFileName, const G4int verbosity=4) :
73  logStream(0),
74  verbosityLevel(verbosity)
75  {
76  if(logFileName=="-") {
77  logStream = &(std::cout);
78  logToStdout = true;
79  } else {
80  logToStdout = false;
81  logStream = new std::ofstream(logFileName.c_str());
82  if(!logStream)
83  {
84  std::cerr << "Fatal error: couldn't open log file " << logFileName << std::endl;
85  std::exit(EXIT_FAILURE);
86  }
87  }
88 
89  // Spell out "true" and "false" when logging G4bool variables
90  std::boolalpha(*logStream);
91  };
92  ~LoggerSlave() {
93  if(!logToStdout)
94  delete logStream;
95  };
96 
100  void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
101 
105  G4int getVerbosityLevel() { return verbosityLevel; }
106 
108  void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true) const;
109 
111  void flush() { logStream->flush(); }
112 
114  void logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const;
115 
116  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
117  typedef CoutType& (*StandardEndLine)(CoutType&);
119  LoggerSlave const &operator<<(StandardEndLine const &manip) const {
120  manip(*logStream);
121  return *this;
122  }
123 
125  template<typename T>
126  LoggerSlave const &operator<<(const T &t) const {
127  (*logStream) << t;
128  return *this;
129  }
130 
131  private:
132  std::ostream *logStream;
133  G4int verbosityLevel;
134  G4bool logToStdout;
135  };
136 
137  namespace Logger {
139  void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true);
140 
142  void flush();
143 
145  void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber);
146 
148  void setLoggerSlave(LoggerSlave * const logger);
149 
151  void setVerbosityLevel(G4int lvl);
152 
155 
157  void deleteLoggerSlave();
158 
160  void initialize(Config const * const theConfig);
161 
162  }
163 
164  // Macro definitions for line numbering in log files!
165 #define INCL_FATAL(x) \
166  if(true) {\
167  std::stringstream ss_;\
168  ss_ << x;\
169  ss_ << "Random seeds at the beginning of this event: " << G4INCL::Random::getSavedSeeds() << std::endl;\
170  G4INCL::Logger::logMessage(G4INCL::FatalMsg, __FILE__,__LINE__, ss_.str());\
171  G4INCL::Logger::flush();\
172  std::exit(EXIT_FAILURE);\
173  } else (void)0
174 #define INCL_ERROR(x) \
175  if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
176  std::stringstream ss_;\
177  ss_ << x;\
178  ss_ << "Random seeds at the beginning of this event: " << G4INCL::Random::getSavedSeeds() << std::endl;\
179  G4INCL::Logger::logMessage(G4INCL::ErrorMsg, __FILE__,__LINE__, ss_.str());\
180  } else (void)0
181 #define INCL_WARN(x) \
182  if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
183  std::stringstream ss_;\
184  ss_ << x;\
185  G4INCL::Logger::logMessage(G4INCL::WarningMsg, __FILE__,__LINE__, ss_.str());\
186  } else (void)0
187 #define INCL_INFO(x) \
188  if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
189  std::stringstream ss_;\
190  ss_ << x;\
191  G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss_.str());\
192  } else (void)0
193 #define INCL_INFO_NOCOMMENT(x) \
194  if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
195  std::stringstream ss_;\
196  ss_ << x;\
197  G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss_.str(), false);\
198  } else (void)0
199 #define INCL_DEBUG(x) \
200  if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
201  std::stringstream ss_;\
202  ss_ << x;\
203  G4INCL::Logger::logMessage(G4INCL::DebugMsg, __FILE__,__LINE__, ss_.str());\
204  } else (void)0
205 #define INCL_DATABLOCK(x) \
206  if(G4INCL::DataBlockMsg <= G4INCL::Logger::getVerbosityLevel()) {\
207  G4INCL::Logger::dataBlock(x,__FILE__,__LINE__);\
208  } else (void)0
209 
210 #else // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
211  namespace Logger {
214  }
215 
216 #define INCL_FATAL(x) \
217  if(true) {\
218  std::stringstream ss_;\
219  ss_ << x;\
220  std::stringstream location_;\
221  std::string fileName_(__FILE__);\
222  location_ << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__;\
223  G4Exception(location_.str().c_str(), "INCLXX0000", EventMustBeAborted, ss_.str().c_str());\
224  } else (void)0
225 #define INCL_ERROR(x) \
226  if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
227  std::string fileName_(__FILE__);\
228  std::stringstream ss_;\
229  ss_ << "INCL++ error [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
230  G4cout << ss_.str() << '\n';\
231  } else (void)0
232 #define INCL_WARN(x) \
233  if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
234  std::string fileName_(__FILE__);\
235  std::stringstream ss_;\
236  ss_ << "INCL++ warning [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
237  G4cout << ss_.str() << '\n';\
238  } else (void)0
239 #define INCL_INFO(x);
240 #define INCL_DEBUG(x) \
241  if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
242  std::string fileName_(__FILE__);\
243  std::stringstream ss_;\
244  ss_ << "INCL++ debug [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
245  G4cout << ss_.str() << '\n';\
246  } else (void)0
247 #define INCL_DATABLOCK(x);
248 
249 #endif // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
250 }
251 #endif
int G4int
Definition: G4Types.hh:78
const XML_Char * s
Definition: expat.h:262
bool G4bool
Definition: G4Types.hh:79
std::ostream & operator<<(std::ostream &ostr, const G4String &astr)
Definition: pyG4String.cc:40
void initialize(Config const *const theConfig)
Initialize the clustering model based on the Config object.
void initVerbosityLevelFromEnvvar()
G4int getVerbosityLevel()