Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:27

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // INCL++ intra-nuclear cascade model
0027 // Alain Boudard, CEA-Saclay, France
0028 // Joseph Cugnon, University of Liege, Belgium
0029 // Jean-Christophe David, CEA-Saclay, France
0030 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
0031 // Sylvie Leray, CEA-Saclay, France
0032 // Davide Mancusi, CEA-Saclay, France
0033 //
0034 #define INCLXX_IN_GEANT4_MODE 1
0035 
0036 #include "globals.hh"
0037 
0038 #ifndef G4INCLLogger_hh
0039 #define G4INCLLogger_hh 1
0040 
0041 #include <iostream>
0042 #include <fstream>
0043 #include <sstream>
0044 #include <string>
0045 #include <cstdlib>
0046 
0047 #ifdef INCLXX_IN_GEANT4_MODE
0048 #include "G4ios.hh"
0049 #endif
0050 
0051 #include "G4INCLRandom.hh"
0052 #include "G4INCLConfig.hh"
0053 
0054 namespace G4INCL {
0055 
0056   /**
0057    * Verbosity scale from 0 (fatal errors only) to 10 (print everything)
0058    */
0059   enum MessageType { InfoMsg = 1,
0060     FatalMsg = 2,
0061     ErrorMsg = 3,
0062     WarningMsg = 4,
0063     DebugMsg = 7,
0064     DataBlockMsg = 10,
0065     ZeroMsg = 0 };
0066 
0067 #if defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
0068 
0069   class LoggerSlave {
0070     public:
0071       // By default, log fatal errors, errors and warnings
0072       LoggerSlave(std::string const &logFileName, const G4int verbosity=4) :
0073         logStream(0),
0074         verbosityLevel(verbosity)
0075     {
0076       if(logFileName=="-") {
0077         logStream = &(std::cout);
0078         logToStdout = true;
0079       } else {
0080         logToStdout = false;
0081         logStream = new std::ofstream(logFileName.c_str());
0082         if(!logStream)
0083         {
0084           std::cerr << "Fatal error: couldn't open log file " << logFileName << std::endl;
0085           std::exit(EXIT_FAILURE);
0086         }
0087       }
0088 
0089       // Spell out "true" and "false" when logging G4bool variables
0090       std::boolalpha(*logStream);
0091     };
0092       ~LoggerSlave() {
0093         if(!logToStdout)
0094           delete logStream;
0095       };
0096 
0097       /**
0098        * Set the verbosity level
0099        */
0100       void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
0101 
0102       /**
0103        * Get the verbosity level
0104        */
0105       G4int getVerbosityLevel() { return verbosityLevel; }
0106 
0107       /// \brief Write the log message.
0108       void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true) const;
0109 
0110       /// \brief Flush the log stream
0111       void flush() { logStream->flush(); }
0112 
0113       /// \brief Log a data block.
0114       void logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const;
0115 
0116       typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
0117       typedef CoutType& (*StandardEndLine)(CoutType&);
0118       /// \brief Overload << operator to support std::endl.
0119       LoggerSlave const &operator<<(StandardEndLine const &manip) const {
0120         manip(*logStream);
0121         return *this;
0122       }
0123 
0124       /// \brief Overloaded << operator to provide a stream-like API.
0125       template<typename T>
0126         LoggerSlave const &operator<<(const T &t) const {
0127           (*logStream) << t;
0128           return *this;
0129         }
0130 
0131     private:
0132       std::ostream *logStream;
0133       G4int verbosityLevel;
0134       G4bool logToStdout;
0135   };
0136 
0137   namespace Logger {
0138       /// \brief Log a message.
0139       void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true);
0140 
0141       /// \brief Flush the log stream
0142       void flush();
0143 
0144       /// \brief Log a data block.
0145       void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber);
0146 
0147       /// \brief Set the slave Logger.
0148       void setLoggerSlave(LoggerSlave * const logger);
0149 
0150       /// \brief Set the verbosity of the slave Logger.
0151       void setVerbosityLevel(G4int lvl);
0152 
0153       /// \brief Get the verbosity of the slave Logger.
0154       G4int getVerbosityLevel();
0155 
0156       /// \brief Delete the slave Logger.
0157       void deleteLoggerSlave();
0158 
0159       /// \brief Initialize the Logger.
0160       void initialize(Config const * const theConfig);
0161 
0162   }
0163 
0164   // Macro definitions for line numbering in log files!
0165 #define INCL_FATAL(x) \
0166   if(true) {\
0167     std::stringstream ss_;\
0168     ss_ << x;\
0169     ss_ << "Random seeds at the beginning of this event: " << G4INCL::Random::getSavedSeeds() << std::endl;\
0170     G4INCL::Logger::logMessage(G4INCL::FatalMsg, __FILE__,__LINE__, ss_.str());\
0171     G4INCL::Logger::flush();\
0172     std::exit(EXIT_FAILURE);\
0173   } else (void)0
0174 #define INCL_ERROR(x) \
0175   if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0176     std::stringstream ss_;\
0177     ss_ << x;\
0178     ss_ << "Random seeds at the beginning of this event: " << G4INCL::Random::getSavedSeeds() << std::endl;\
0179     G4INCL::Logger::logMessage(G4INCL::ErrorMsg, __FILE__,__LINE__, ss_.str());\
0180   } else (void)0
0181 #define INCL_WARN(x) \
0182   if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0183     std::stringstream ss_;\
0184     ss_ << x;\
0185     G4INCL::Logger::logMessage(G4INCL::WarningMsg, __FILE__,__LINE__, ss_.str());\
0186   } else (void)0
0187 #define INCL_INFO(x) \
0188   if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0189     std::stringstream ss_;\
0190     ss_ << x;\
0191     G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss_.str());\
0192   } else (void)0
0193 #define INCL_INFO_NOCOMMENT(x) \
0194   if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0195     std::stringstream ss_;\
0196     ss_ << x;\
0197     G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss_.str(), false);\
0198   } else (void)0
0199 #define INCL_DEBUG(x) \
0200   if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0201     std::stringstream ss_;\
0202     ss_ << x;\
0203     G4INCL::Logger::logMessage(G4INCL::DebugMsg, __FILE__,__LINE__, ss_.str());\
0204   } else (void)0
0205 #define INCL_DATABLOCK(x) \
0206   if(G4INCL::DataBlockMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0207     G4INCL::Logger::dataBlock(x,__FILE__,__LINE__);\
0208   } else (void)0
0209 
0210 #else // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
0211   namespace Logger {
0212     void initVerbosityLevelFromEnvvar();
0213     G4int getVerbosityLevel();
0214   }
0215 
0216 #define INCL_FATAL(x) \
0217   if(true) {\
0218     std::stringstream ss_;\
0219     ss_ << x;\
0220     std::stringstream location_;\
0221     std::string fileName_(__FILE__);\
0222     location_ << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__;\
0223     G4Exception(location_.str().c_str(), "INCLXX0000", EventMustBeAborted, ss_.str().c_str());\
0224   } else (void)0
0225 #define INCL_ERROR(x) \
0226   if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0227     std::string fileName_(__FILE__);\
0228     std::stringstream ss_;\
0229     ss_ << "INCL++ error [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
0230     G4cout << ss_.str() << '\n';\
0231   } else (void)0
0232 #define INCL_WARN(x) \
0233   if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0234     std::string fileName_(__FILE__);\
0235     std::stringstream ss_;\
0236     ss_ << "INCL++ warning [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
0237     G4cout << ss_.str() << '\n';\
0238   } else (void)0
0239 #define INCL_INFO(x);
0240 #define INCL_DEBUG(x) \
0241   if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
0242     std::string fileName_(__FILE__);\
0243     std::stringstream ss_;\
0244     ss_ << "INCL++ debug [" << fileName_.substr(fileName_.find_last_of("/")+1) << ":" << __LINE__ << "] " << x;\
0245     G4cout << ss_.str() << '\n';\
0246   } else (void)0
0247 #define INCL_DATABLOCK(x);
0248 
0249 #endif // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
0250 }
0251 #endif