00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #define INCLXX_IN_GEANT4_MODE 1
00034
00035 #include "globals.hh"
00036
00037 #ifndef G4INCLLogger_hh
00038 #define G4INCLLogger_hh 1
00039
00040 #include <iostream>
00041 #include <fstream>
00042 #include <sstream>
00043 #include <string>
00044 #include <cstdlib>
00045
00046 namespace G4INCL {
00047
00051 enum MessageType { InfoMsg = 1,
00052 FatalMsg = 2,
00053 ErrorMsg = 3,
00054 WarningMsg = 4,
00055 DebugMsg = 7,
00056 DataBlockMsg = 10,
00057 ZeroMsg = 0 };
00058
00059 #if defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
00060
00061 class LoggerSlave {
00062 public:
00063
00064 LoggerSlave(std::string const &logFileName) : logStream(0), verbosityLevel(4) {
00065 if(logFileName=="-") {
00066 logStream = &(std::cout);
00067 logToStdout = true;
00068 } else {
00069 logToStdout = false;
00070 logStream = new std::ofstream(logFileName.c_str());
00071 if(!logStream)
00072 {
00073 std::cerr << "Fatal error: couldn't open log file " << logFileName << std::endl;
00074 std::exit(EXIT_FAILURE);
00075 }
00076 }
00077
00078
00079 std::boolalpha(*logStream);
00080
00081 logMessage(InfoMsg, __FILE__,__LINE__, "# Logging enabled!\n");
00082 };
00083 ~LoggerSlave() {
00084 if(!logToStdout)
00085 delete logStream;
00086 };
00087
00091 void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
00092
00096 G4int getVerbosityLevel() { return verbosityLevel; }
00097
00099 void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s) const;
00100
00102 void flush() { logStream->flush(); }
00103
00105 void logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const;
00106
00107 typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
00108 typedef CoutType& (*StandardEndLine)(CoutType&);
00110 LoggerSlave const &operator<<(StandardEndLine const &manip) const {
00111 manip(*logStream);
00112 return *this;
00113 }
00114
00116 template<typename T>
00117 LoggerSlave const &operator<<(const T &t) const {
00118 (*logStream) << t;
00119 return *this;
00120 }
00121
00122 private:
00123 std::ostream *logStream;
00124 G4int verbosityLevel;
00125 G4bool logToStdout;
00126 };
00127
00128 class Logger {
00129 public:
00131 static void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s) {
00132 theLoggerSlave->logMessage(type, fileName, lineNumber, s);
00133 }
00134
00136 static void flush() { theLoggerSlave->flush(); }
00137
00139 static void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) {
00140 theLoggerSlave->logDataBlock(block, fileName, lineNumber);
00141 }
00142
00144 static void setLoggerSlave(LoggerSlave * const logger) { theLoggerSlave = logger; }
00145
00147 static void setVerbosityLevel(G4int lvl) { theLoggerSlave->setVerbosityLevel(lvl); }
00148
00150 static G4int getVerbosityLevel() { return theLoggerSlave->getVerbosityLevel(); }
00151
00153 static void deleteLoggerSlave() {
00154 delete theLoggerSlave;
00155 theLoggerSlave=NULL;
00156 }
00157
00158 private:
00159 static LoggerSlave *theLoggerSlave;
00160 };
00161
00162
00163 #define FATAL(x) \
00164 if(true) {\
00165 std::stringstream ss;\
00166 ss << x;\
00167 G4INCL::Logger::logMessage(G4INCL::FatalMsg, __FILE__,__LINE__, ss.str());\
00168 G4INCL::Logger::flush();\
00169 std::exit(EXIT_FAILURE);\
00170 } else (void)0
00171 #define ERROR(x) \
00172 if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
00173 std::stringstream ss;\
00174 ss << x;\
00175 G4INCL::Logger::logMessage(G4INCL::ErrorMsg, __FILE__,__LINE__, ss.str());\
00176 } else (void)0
00177 #define WARN(x) \
00178 if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
00179 std::stringstream ss;\
00180 ss << x;\
00181 G4INCL::Logger::logMessage(G4INCL::WarningMsg, __FILE__,__LINE__, ss.str());\
00182 } else (void)0
00183 #define INFO(x) \
00184 if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
00185 std::stringstream ss;\
00186 ss << x;\
00187 G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss.str());\
00188 } else (void)0
00189 #define DEBUG(x) \
00190 if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
00191 std::stringstream ss;\
00192 ss << x;\
00193 G4INCL::Logger::logMessage(G4INCL::DebugMsg, __FILE__,__LINE__, ss.str());\
00194 } else (void)0
00195 #define DATABLOCK(x) \
00196 if(G4INCL::DataBlockMsg <= G4INCL::Logger::getVerbosityLevel()) {\
00197 G4INCL::Logger::dataBlock(x,__FILE__,__LINE__);\
00198 } else (void)0
00199
00200 #else
00201
00202 class LoggerSlave {
00203 public:
00204 LoggerSlave(std::string const &) {};
00205 LoggerSlave() {};
00206 ~LoggerSlave() {};
00207 void setVerbosityLevel(G4int) {};
00208 };
00209
00210 class Logger {
00211 public:
00212 Logger() {};
00213 ~Logger() {};
00214 static void setVerbosityLevel(G4int) {};
00215 static void setLoggerSlave(LoggerSlave * const slave) { theLoggerSlave = slave; }
00216 static void deleteLoggerSlave() {
00217 delete theLoggerSlave;
00218 theLoggerSlave=NULL;
00219 }
00220 private:
00221 static LoggerSlave *theLoggerSlave;
00222 };
00223
00224 #define FATAL(x) \
00225 if(true) {\
00226 std::stringstream ss;\
00227 ss << x;\
00228 std::stringstream location;\
00229 location << __FILE__ << ":" << __LINE__;\
00230 G4Exception(location.str().c_str(), "INCLXX0000", FatalException, ss.str().c_str());\
00231 } else (void)0
00232 #define ERROR(x);
00233 #define WARN(x);
00234 #define INFO(x);
00235 #define DEBUG(x);
00236 #define DATABLOCK(x);
00237
00238 #endif
00239 }
00240 #endif