PYTHIA  8.312
Logger.h
1 // Logger.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2024 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // This file contains the Logger class for diagnostic messages.
7 
8 #ifndef Pythia8_Logger_H
9 #define Pythia8_Logger_H
10 
11 #include "Pythia8/PythiaStdlib.h"
12 
13 namespace Pythia8 {
14 
15 class Settings;
16 
17 //==========================================================================
18 
19 // The Logger class prints diagnostic messages and counts error messages.
20 // Objects of the Logger class can be used as ostream object,
21 // supporting the << operator.
22 
23 class Logger : private streambuf, public ostream {
24 
25  // This struct defines the ordering of log messages.
26  // If the two messages have different severity (abort, error, warning,
27  // info, debug), order by most severe first. Otherwise, order alphabetically.
28  struct LogComparer {
29  bool operator() (const string& a, const string& b) const {
30  char a1 = a[0], b1 = b[0];
31  int severityA, severityB;
32  if (a1 == 'A') severityA = 0;
33  else if (a1 == 'E') severityA = 1;
34  else if (a1 == 'W') severityA = 2;
35  else if (a1 == 'I') severityA = 3;
36  else if (a1 == 'R') severityA = 4;
37  else severityA = 5;
38  if (b1 == 'A') severityB = 0;
39  else if (b1 == 'E') severityB = 1;
40  else if (b1 == 'W') severityB = 2;
41  else if (b1 == 'I') severityB = 3;
42  else if (b1 == 'R') severityB = 4;
43  else severityB = 5;
44  if (severityA != severityB) return severityA < severityB;
45  return a < b;
46  }
47  };
48 
49 public:
50 
51  // Construct with default values.
52  Logger() : ostream(this), infoStreamSave(cout), errStreamSave(cerr),
53  verbosity(2), printInitSave(true), printNextSave(true), printErrors(true),
54  isQuietSave(false), useErrorStream(false) { }
55  virtual ~Logger() {}
56 
57  void init(Settings& settings);
58 
59  // Methods for providing different levels of error messaging.
60  // These methods are thread safe.
61 
62  // Report messages contain information not relevant in normal runs.
63  void reportMsg(string loc, string message, string extraInfo = "",
64  bool showAlways = false);
65 
66  // Info messages are diagnostic messages that don't indicate an issue.
67  void infoMsg(string loc, string message, string extraInfo = "",
68  bool showAlways = false);
69 
70  // Warnings indicate that there might be an issue, but the run will continue.
71  void warningMsg(string loc, string message, string extraInfo = "",
72  bool showAlways = false);
73 
74  // Errors indicate an issue that might cause the current event to fail.
75  void errorMsg(string loc, string message, string extraInfo = "",
76  bool showAlways = false);
77 
78  // Aborts indicate critical issues that prevent further event generation.
79  void abortMsg(string loc, string message, string extraInfo = "",
80  bool showAlways = false);
81 
82  // If quiet, the logger will not print any messages.
83  bool isQuiet() const { return isQuietSave; }
84 
85  // Indicates whether information will be printed during initialization.
86  bool mayPrintInit() const { return printInitSave && !isQuietSave; }
87 
88  // Indicates whether information will be printed during event generation.
89  bool mayPrintNext() const { return printNextSave && !isQuietSave; }
90 
91  // Indicates whether error messages will be printed.
92  bool mayPrintErrors() const { return printErrors && !isQuietSave; }
93 
94  // 0: no messages | 1: aborts only | 2: default | 3: debug
95  void setVerbosity(int verbosityIn) { verbosity = verbosityIn; }
96  int getVerbosity() const { return verbosity; }
97 
98  // Add all errors from the other Logger object to the counts of this object.
99  void errorCombine(const Logger& other, string prefix = "");
100 
101  // Reset to empty map of error messages.
102  void errorReset() {messages.clear();}
103 
104  // Total number of errors/aborts/warnings logged.
105  int errorTotalNumber() const {
106  int nTot = 0;
107  for (pair<string, int> messageEntry : messages)
108  nTot += messageEntry.second;
109  return nTot;
110  }
111 
112  // Print error statistics.
113  void errorStatistics() const { errorStatistics(infoStreamSave); }
114  void errorStatistics(ostream& stream) const;
115 
116  ostream& infoStream() { return infoStreamSave; }
117  ostream& errorStream() {
118  return useErrorStream ? errStreamSave : infoStreamSave; }
119 
120  // Iterators over error messages.
121  map<string, int, LogComparer>::iterator begin() {
122  return messages.begin(); }
123  map<string, int, LogComparer>::iterator end() {
124  return messages.end(); }
125  map<string, int, LogComparer>::const_iterator begin() const {
126  return messages.begin(); }
127  map<string, int, LogComparer>::const_iterator end() const {
128  return messages.end(); }
129 
130  // Abort verbosity level: print only abort messages.
131  static constexpr int ABORT = 1;
132  // Normal verbosity level: print errors, warnings and info messages.
133  static constexpr int NORMAL = 2;
134  // Report verbosity level: print everything, including report messages.
135  static constexpr int REPORT = 3;
136 
137 private:
138 
139  // Map for all error messages.
140  map<string, int, LogComparer> messages;
141 
142  // Override method from std::streambuf.
143  // This makes the operator<< write to infoStreamSave.
144  int overflow(int c) override {
145  infoStreamSave.put(c);
146  return 0;
147  }
148 
149  // Streams where messages are written.
150  ostream& infoStreamSave;
151  // Optional separate error stream.
152  ostream& errStreamSave;
153 
154  // Configuration.
155  int verbosity;
156  bool printInitSave, printNextSave, printErrors, isQuietSave, useErrorStream;
157 
158  // Mutual exclusive access to writing messages.
159  mutex writeMutex;
160 
161  // Method to print the diagnostic message. This method is thread safe.
162  void msg(int verbosityLevel, string message, string extraInfo = "",
163  bool showAlways = false);
164 
165 };
166 
167 //--------------------------------------------------------------------------
168 
169 // Shorthand macros for logger messages.
170 
171 #define INFO_MSG(...) infoMsg(__METHOD_NAME__, __VA_ARGS__)
172 #define WARNING_MSG(...) warningMsg(__METHOD_NAME__, __VA_ARGS__)
173 #define ERROR_MSG(...) errorMsg(__METHOD_NAME__, __VA_ARGS__)
174 #define ABORT_MSG(...) abortMsg(__METHOD_NAME__, __VA_ARGS__)
175 #define REPORT_MSG(...) reportMsg(__METHOD_NAME__, __VA_ARGS__)
176 
177 //==========================================================================
178 
179 } // end namespace Pythia8
180 
181 #endif // Pythia8_Logger_H
bool mayPrintErrors() const
Indicates whether error messages will be printed.
Definition: Logger.h:92
int errorTotalNumber() const
Total number of errors/aborts/warnings logged.
Definition: Logger.h:105
void infoMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Info messages are diagnostic messages that don&#39;t indicate an issue.
Definition: Logger.cc:29
bool mayPrintNext() const
Indicates whether information will be printed during event generation.
Definition: Logger.h:89
void reportMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Report messages contain information not relevant in normal runs.
Definition: Logger.cc:26
Definition: Logger.h:23
void warningMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Warnings indicate that there might be an issue, but the run will continue.
Definition: Logger.cc:32
static constexpr int ABORT
Abort verbosity level: print only abort messages.
Definition: Logger.h:131
void errorReset()
Reset to empty map of error messages.
Definition: Logger.h:102
void errorMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Errors indicate an issue that might cause the current event to fail.
Definition: Logger.cc:35
map< string, int, LogComparer >::iterator begin()
Iterators over error messages.
Definition: Logger.h:121
bool mayPrintInit() const
Indicates whether information will be printed during initialization.
Definition: Logger.h:86
void errorCombine(const Logger &other, string prefix="")
Add all errors from the other Logger object to the counts of this object.
Definition: Logger.cc:68
static constexpr int REPORT
Report verbosity level: print everything, including report messages.
Definition: Logger.h:135
Logger()
Construct with default values.
Definition: Logger.h:52
static constexpr int NORMAL
Normal verbosity level: print errors, warnings and info messages.
Definition: Logger.h:133
void abortMsg(string loc, string message, string extraInfo="", bool showAlways=false)
Aborts indicate critical issues that prevent further event generation.
Definition: Logger.cc:38
void setVerbosity(int verbosityIn)
0: no messages | 1: aborts only | 2: default | 3: debug
Definition: Logger.h:95
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
bool isQuiet() const
If quiet, the logger will not print any messages.
Definition: Logger.h:83
void errorStatistics() const
Print error statistics.
Definition: Logger.h:113
Definition: Settings.h:195