Geant4  10.00.p02
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 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28 // Davide Mancusi, CEA
29 // Alain Boudard, CEA
30 // Sylvie Leray, CEA
31 // Joseph Cugnon, University of Liege
32 //
33 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 #ifndef G4INCLLogger_hh
38 #define G4INCLLogger_hh 1
39 
40 #include <iostream>
41 #include <fstream>
42 #include <sstream>
43 #include <string>
44 #include <cstdlib>
45 
46 #ifdef INCLXX_IN_GEANT4_MODE
47 #include "G4ios.hh"
48 #endif
49 
50 namespace G4INCL {
51 
55  enum MessageType { InfoMsg = 1,
56  FatalMsg = 2,
57  ErrorMsg = 3,
59  DebugMsg = 7,
61  ZeroMsg = 0 };
62 
63 #if defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
64 
65  class LoggerSlave {
66  public:
67  // By default, log fatal errors, errors and warnings
68  LoggerSlave(std::string const &logFileName) : logStream(0), verbosityLevel(4) {
69  if(logFileName=="-") {
70  logStream = &(std::cout);
71  logToStdout = true;
72  } else {
73  logToStdout = false;
74  logStream = new std::ofstream(logFileName.c_str());
75  if(!logStream)
76  {
77  std::cerr << "Fatal error: couldn't open log file " << logFileName << std::endl;
78  std::exit(EXIT_FAILURE);
79  }
80  }
81 
82  // Spell out "true" and "false" when logging G4bool variables
83  std::boolalpha(*logStream);
84 
85  logMessage(InfoMsg, __FILE__,__LINE__, "# Logging enabled!\n");
86  };
87  ~LoggerSlave() {
88  if(!logToStdout)
89  delete logStream;
90  };
91 
95  void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
96 
100  G4int getVerbosityLevel() { return verbosityLevel; }
101 
103  void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s) const;
104 
106  void flush() { logStream->flush(); }
107 
109  void logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const;
110 
111  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
112  typedef CoutType& (*StandardEndLine)(CoutType&);
114  LoggerSlave const &operator<<(StandardEndLine const &manip) const {
115  manip(*logStream);
116  return *this;
117  }
118 
120  template<typename T>
121  LoggerSlave const &operator<<(const T &t) const {
122  (*logStream) << t;
123  return *this;
124  }
125 
126  private:
127  std::ostream *logStream;
128  G4int verbosityLevel;
129  G4bool logToStdout;
130  };
131 
132  namespace Logger {
134  void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s);
135 
137  void flush();
138 
140  void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber);
141 
143  void setLoggerSlave(LoggerSlave * const logger);
144 
146  void setVerbosityLevel(G4int lvl);
147 
150 
152  void deleteLoggerSlave();
153 
154  }
155 
156  // Macro definitions for line numbering in log files!
157 #define INCL_FATAL(x) \
158  if(true) {\
159  std::stringstream ss_;\
160  ss_ << x;\
161  G4INCL::Logger::logMessage(G4INCL::FatalMsg, __FILE__,__LINE__, ss_.str());\
162  G4INCL::Logger::flush();\
163  std::exit(EXIT_FAILURE);\
164  } else (void)0
165 #define INCL_ERROR(x) \
166  if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
167  std::stringstream ss_;\
168  ss_ << x;\
169  G4INCL::Logger::logMessage(G4INCL::ErrorMsg, __FILE__,__LINE__, ss_.str());\
170  } else (void)0
171 #define INCL_WARN(x) \
172  if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
173  std::stringstream ss_;\
174  ss_ << x;\
175  G4INCL::Logger::logMessage(G4INCL::WarningMsg, __FILE__,__LINE__, ss_.str());\
176  } else (void)0
177 #define INCL_INFO(x) \
178  if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
179  std::stringstream ss_;\
180  ss_ << x;\
181  G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss_.str());\
182  } else (void)0
183 #define INCL_DEBUG(x) \
184  if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
185  std::stringstream ss_;\
186  ss_ << x;\
187  G4INCL::Logger::logMessage(G4INCL::DebugMsg, __FILE__,__LINE__, ss_.str());\
188  } else (void)0
189 #define INCL_DATABLOCK(x) \
190  if(G4INCL::DataBlockMsg <= G4INCL::Logger::getVerbosityLevel()) {\
191  G4INCL::Logger::dataBlock(x,__FILE__,__LINE__);\
192  } else (void)0
193 
194 #else // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
195  namespace Logger {
198  }
199 
200 #define INCL_FATAL(x) \
201  if(true) {\
202  std::stringstream ss_;\
203  ss_ << x;\
204  std::stringstream location_;\
205  std::string fileName_(__FILE__);\
206  location_ << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__;\
207  G4Exception(location_.str().c_str(), "INCLXX0000", FatalException, ss_.str().c_str());\
208  } else (void)0
209 #define INCL_ERROR(x) \
210  if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
211  std::string fileName_(__FILE__);\
212  std::stringstream ss_;\
213  ss_ << "INCL++ error [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
214  G4cout << ss_.str() << std::endl;\
215  } else (void)0
216 #define INCL_WARN(x) \
217  if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
218  std::string fileName_(__FILE__);\
219  std::stringstream ss_;\
220  ss_ << "INCL++ warning [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
221  G4cout << ss_.str() << std::endl;\
222  } else (void)0
223 #define INCL_INFO(x);
224 #define INCL_DEBUG(x) \
225  if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
226  std::string fileName_(__FILE__);\
227  std::stringstream ss_;\
228  ss_ << "INCL++ debug [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
229  G4cout << ss_.str() << std::endl;\
230  } else (void)0
231 #define INCL_DATABLOCK(x);
232 
233 #endif // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
234 }
235 #endif
int G4int
Definition: G4Types.hh:78
static const double s
Definition: G4SIunits.hh:150
bool G4bool
Definition: G4Types.hh:79
std::ostream & operator<<(std::ostream &ostr, const G4String &astr)
Definition: pyG4String.cc:40
void initVerbosityLevelFromEnvvar()
G4int getVerbosityLevel()
MessageType
Verbosity scale from 0 (fatal errors only) to 10 (print everything)
Definition: G4INCLLogger.hh:55