Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4INCLLogger.hh
Go to the documentation of this file.
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// Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28// Davide Mancusi, CEA
29// Alain Boudard, CEA
30// Sylvie Leray, CEA
31// Joseph Cugnon, University of Liege
32//
33// INCL++ revision: v5.1.8
34//
35#define INCLXX_IN_GEANT4_MODE 1
36
37#include "globals.hh"
38
39#ifndef G4INCLLogger_hh
40#define G4INCLLogger_hh 1
41
42#include <iostream>
43#include <fstream>
44#include <sstream>
45#include <string>
46#include <cstdlib>
47
48namespace G4INCL {
49
50 /**
51 * Verbosity scale from 0 (fatal errors only) to 10 (print everything)
52 */
53 enum MessageType { InfoMsg = 1,
59 ZeroMsg = 0 };
60
61#ifdef INCL_DEBUG_LOG
62
63 class LoggerSlave {
64 public:
65 // By default, log fatal errors, errors and warnings
66 LoggerSlave(std::string const &logFileName) : logStream(0), verbosityLevel(4) {
67 if(logFileName=="-") {
68#ifdef INCLXX_IN_GEANT4_MODE
69 logStream = &(G4cout);
70#else
71 logStream = &(std::cout);
72#endif
73 logToStdout = true;
74 } else {
75 logToStdout = false;
76 logStream = new std::ofstream(logFileName.c_str());
77 if(!logStream)
78 {
79 std::cerr << "Fatal error: couldn't open log file " << logFileName << std::endl;
80 std::exit(EXIT_FAILURE);
81 }
82 }
83
84 // Spell out "true" and "false" when logging G4bool variables
85 std::boolalpha(*logStream);
86
87#ifndef INCLXX_IN_GEANT4_MODE
88 logMessage(InfoMsg, __FILE__,__LINE__, "# Logging enabled!\n");
89#endif
90 };
91 ~LoggerSlave() {
92 if(!logToStdout)
93 delete logStream;
94 };
95
96 /**
97 * Set the verbosity level
98 */
99 void setVerbosityLevel(G4int lvl) { verbosityLevel = lvl; }
100
101 /**
102 * Get the verbosity level
103 */
104 G4int getVerbosityLevel() { return verbosityLevel; }
105
106 /// \brief Write the log message.
107 void logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s) const;
108
109 /// \brief Flush the log stream
110 void flush() { logStream->flush(); }
111
112 /// \brief Log a data block.
113 void logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const;
114
115 typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
116 typedef CoutType& (*StandardEndLine)(CoutType&);
117 /// \brief Overload << operator to support std::endl.
118 LoggerSlave const &operator<<(StandardEndLine const &manip) const {
119 manip(*logStream);
120 return *this;
121 }
122
123 /// \brief Overloaded << operator to provide a stream-like API.
124 template<typename T>
125 LoggerSlave const &operator<<(const T &t) const {
126 (*logStream) << t;
127 return *this;
128 }
129
130 private:
131 std::ostream *logStream;
132 G4int verbosityLevel;
133 G4bool logToStdout;
134 };
135
136 class Logger {
137 public:
138 /// \brief Log a message.
139 static void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s) {
140 theLoggerSlave->logMessage(type, fileName, lineNumber, s);
141 }
142
143 /// \brief Flush the log stream
144 static void flush() { theLoggerSlave->flush(); }
145
146 /// \brief Log a data block.
147 static void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) {
148 theLoggerSlave->logDataBlock(block, fileName, lineNumber);
149 }
150
151 /// \brief Set the slave Logger.
152 static void setLoggerSlave(LoggerSlave * const logger) { theLoggerSlave = logger; }
153
154 /// \brief Set the verbosity of the slave Logger.
155 static void setVerbosityLevel(G4int lvl) { theLoggerSlave->setVerbosityLevel(lvl); }
156
157 /// \brief Get the verbosity of the slave Logger.
158 static G4int getVerbosityLevel() { return theLoggerSlave->getVerbosityLevel(); }
159
160 /// \brief Delete the slave Logger.
161 static void deleteLoggerSlave() {
162 delete theLoggerSlave;
163 theLoggerSlave=NULL;
164 }
165
166 private:
167 static LoggerSlave *theLoggerSlave;
168 };
169
170 // Macro definitions for line numbering in log files!
171#define FATAL(x) \
172 if(G4INCL::FatalMsg <= G4INCL::Logger::getVerbosityLevel()) {\
173 std::stringstream ss;\
174 ss << x;\
175 G4INCL::Logger::logMessage(G4INCL::FatalMsg, __FILE__,__LINE__, ss.str());\
176 G4INCL::Logger::flush();\
177 } else (void)0
178#define ERROR(x) \
179 if(G4INCL::ErrorMsg <= G4INCL::Logger::getVerbosityLevel()) {\
180 std::stringstream ss;\
181 ss << x;\
182 G4INCL::Logger::logMessage(G4INCL::ErrorMsg, __FILE__,__LINE__, ss.str());\
183 } else (void)0
184#define WARN(x) \
185 if(G4INCL::WarningMsg <= G4INCL::Logger::getVerbosityLevel()) {\
186 std::stringstream ss;\
187 ss << x;\
188 G4INCL::Logger::logMessage(G4INCL::WarningMsg, __FILE__,__LINE__, ss.str());\
189 } else (void)0
190#define INFO(x) \
191 if(G4INCL::InfoMsg <= G4INCL::Logger::getVerbosityLevel()) {\
192 std::stringstream ss;\
193 ss << x;\
194 G4INCL::Logger::logMessage(G4INCL::InfoMsg, __FILE__,__LINE__, ss.str());\
195 } else (void)0
196#define DEBUG(x) \
197 if(G4INCL::DebugMsg <= G4INCL::Logger::getVerbosityLevel()) {\
198 std::stringstream ss;\
199 ss << x;\
200 G4INCL::Logger::logMessage(G4INCL::DebugMsg, __FILE__,__LINE__, ss.str());\
201 } else (void)0
202#define DATABLOCK(x) \
203 if(G4INCL::DataBlockMsg <= G4INCL::Logger::getVerbosityLevel()) {\
204 G4INCL::Logger::dataBlock(x,__FILE__,__LINE__);\
205 } else (void)0
206
207#else
208 // Empty logger for normal (production) use:
210 public:
211 LoggerSlave(std::string const &) {};
215 };
216
217 class Logger {
218 public:
219 Logger() {};
221 static void setVerbosityLevel(G4int) {};
222 static void setLoggerSlave(LoggerSlave * const slave) { theLoggerSlave = slave; }
223 static void deleteLoggerSlave() {
224 delete theLoggerSlave;
225 theLoggerSlave=NULL;
226 }
227 private:
228 static LoggerSlave *theLoggerSlave;
229 };
230
231#define FATAL(x);
232#define ERROR(x);
233#define WARN(x);
234#define INFO(x);
235#define DEBUG(x);
236#define DATABLOCK(x);
237
238#endif
239}
240#endif
std::ostream & operator<<(std::ostream &out, const G4CellScoreComposer &ps)
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
G4DLLIMPORT std::ostream G4cout
LoggerSlave(std::string const &)
void setVerbosityLevel(G4int)
static void setLoggerSlave(LoggerSlave *const slave)
static void deleteLoggerSlave()
static void setVerbosityLevel(G4int)
@ WarningMsg
Definition: G4INCLLogger.hh:56
@ DataBlockMsg
Definition: G4INCLLogger.hh:58