G4INCLLogger.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // INCL++ intra-nuclear cascade model
00027 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
00028 // Davide Mancusi, CEA
00029 // Alain Boudard, CEA
00030 // Sylvie Leray, CEA
00031 // Joseph Cugnon, University of Liege
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       // By default, log fatal errors, errors and warnings
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         // Spell out "true" and "false" when logging G4bool variables
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   // Macro definitions for line numbering in log files!
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   // Empty logger for normal (production) use:
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

Generated on Mon May 27 17:48:35 2013 for Geant4 by  doxygen 1.4.7