File indexing completed on 2025-01-18 09:58:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
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
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
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
0090 std::boolalpha(*logStream);
0091 };
0092 ~LoggerSlave() {
0093 if(!logToStdout)
0094 delete logStream;
0095 };
0096
0097
0098
0099
0100 void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
0101
0102
0103
0104
0105 G4int getVerbosityLevel() { return verbosityLevel; }
0106
0107
0108 void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true) const;
0109
0110
0111 void flush() { logStream->flush(); }
0112
0113
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
0119 LoggerSlave const &operator<<(StandardEndLine const &manip) const {
0120 manip(*logStream);
0121 return *this;
0122 }
0123
0124
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
0139 void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s, const G4bool prefixHash=true);
0140
0141
0142 void flush();
0143
0144
0145 void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber);
0146
0147
0148 void setLoggerSlave(LoggerSlave * const logger);
0149
0150
0151 void setVerbosityLevel(G4int lvl);
0152
0153
0154 G4int getVerbosityLevel();
0155
0156
0157 void deleteLoggerSlave();
0158
0159
0160 void initialize(Config const * const theConfig);
0161
0162 }
0163
0164
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
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
0250 }
0251 #endif