Geant4_10
G4INCLLogger.cc
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 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 #include "G4INCLLogger.hh"
38 #include "G4INCLGlobals.hh"
39 
40 #ifdef INCLXX_IN_GEANT4_MODE
41 #include <cstdlib>
42 #endif
43 
44 namespace G4INCL {
45 #if defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
46  std::string typeToString(const MessageType t) {
47  if(t == ErrorMsg)
48  return std::string("Error");
49  else if(t == FatalMsg)
50  return std::string ("Fatal");
51  else if(t == WarningMsg)
52  return std::string("Warning");
53  else if(t == DebugMsg)
54  return std::string("Debug");
55  else if(t == InfoMsg)
56  return std::string("");
57  else if(t == DataBlockMsg)
58  return std::string("DataBlock");
59  else
60  return std::string("Unknown");
61  }
62 
63  void LoggerSlave::logMessage(const MessageType type, const std::string &fileName, const G4int lineNumber, std::string const &s) const {
64  if(type==InfoMsg) {
65  (*logStream) << s;
66  return;
67  }
68 
69  std::stringstream headerss;
70  headerss << typeToString(type) << " [";
71  std::string cont("\n");
72  cont += headerss.str();
73  headerss <<
74  fileName.substr(fileName.find_last_of("/")+1) <<
75  ":" << lineNumber << "] ";
76  std::string header = headerss.str();
77  cont.append(header.size() - cont.size() - 1, '.');
78  cont += "] ";
79 
80  std::string message(s);
81  String::replaceAll(message, "\n", cont, s.size()-2);
82  (*logStream) << header << message;
83 
84  return;
85  }
86 
87  void LoggerSlave::logDataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) const {
88  (*logStream) << typeToString(DataBlockMsg) << " [" <<
89  fileName.substr(fileName.find_last_of("/")+1) <<
90  ":" << lineNumber << "] " << std::endl
91  << "BEGINDATA"
92  << block
93  << "ENDDATA" << std::endl;
94  }
95 
96  namespace Logger {
97 
98  namespace {
99  G4ThreadLocal LoggerSlave *theLoggerSlave = NULL;
100  }
101 
102  void logMessage(const MessageType type, std::string const &fileName, const G4int lineNumber, std::string const &s) {
103  theLoggerSlave->logMessage(type, fileName, lineNumber, s);
104  }
105 
106  void flush() { theLoggerSlave->flush(); }
107 
108  void dataBlock(const std::string &block, const std::string &fileName, const G4int lineNumber) {
109  theLoggerSlave->logDataBlock(block, fileName, lineNumber);
110  }
111 
112  void setLoggerSlave(LoggerSlave * const logger) { theLoggerSlave = logger; }
113 
114  void setVerbosityLevel(G4int lvl) { theLoggerSlave->setVerbosityLevel(lvl); }
115 
116  G4int getVerbosityLevel() { return theLoggerSlave->getVerbosityLevel(); }
117 
118  void deleteLoggerSlave() {
119  delete theLoggerSlave;
120  theLoggerSlave=NULL;
121  }
122 
123  }
124 
125 #else // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
126 
127  namespace Logger {
128 
129  namespace {
130  G4ThreadLocal G4int verbosityLevel = 0;
131  }
132 
134  const char * const envVar = getenv("G4INCL_DEBUG_VERBOSITY");
135  if(envVar) {
136  std::stringstream verbss(envVar);
137  verbss >> verbosityLevel;
138  } else {
139  verbosityLevel = 0;
140  }
141  }
142 
144  return verbosityLevel;
145  }
146 
147  }
148 
149 #endif // defined(INCL_DEBUG_LOG) && !defined(INCLXX_IN_GEANT4_MODE)
150 }
const XML_Char * s
Definition: expat.h:262
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
void initVerbosityLevelFromEnvvar()
void replaceAll(std::string &str, const std::string &from, const std::string &to, const size_t maxPosition=std::string::npos)
G4int getVerbosityLevel()