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] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 
 56   /**
 57    * Verbosity scale from 0 (fatal errors only) to 10 (print everything)
 58    */
 59   enum MessageType { InfoMsg = 1,
 60     FatalMsg = 2,
 61     ErrorMsg = 3,
 62     WarningMsg = 4,
 63     DebugMsg = 7,
 64     DataBlockMsg = 10,
 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 
 97       /**
 98        * Set the verbosity level
 99        */
100       void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
101 
102       /**
103        * Get the verbosity level
104        */
105       G4int getVerbosityLevel() { return verbosityLevel; }
106 
107       /// \brief Write the log message.
108       void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true) const;
109 
110       /// \brief Flush the log stream
111       void flush() { logStream->flush(); }
112 
113       /// \brief Log a data block.
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&);
118       /// \brief Overload << operator to support std::endl.
119       LoggerSlave const &operator<<(StandardEndLine const &manip) const {
120         manip(*logStream);
121         return *this;
122       }
123 
124       /// \brief Overloaded << operator to provide a stream-like API.
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 {
138       /// \brief Log a message.
139       void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true);
140 
141       /// \brief Flush the log stream
142       void flush();
143 
144       /// \brief Log a data block.
145       void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber);
146 
147       /// \brief Set the slave Logger.
148       void setLoggerSlave(LoggerSlave * const logger);
149 
150       /// \brief Set the verbosity of the slave Logger.
151       void setVerbosityLevel(G4int lvl);
152 
153       /// \brief Get the verbosity of the slave Logger.
154       G4int getVerbosityLevel();
155 
156       /// \brief Delete the slave Logger.
157       void deleteLoggerSlave();
158 
159       /// \brief Initialize the Logger.
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 {
212     void initVerbosityLevelFromEnvvar();
213     G4int getVerbosityLevel();
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
252