Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:26

0001 // Logger.h is a part of the PYTHIA event generator.
0002 // Copyright (C) 2024 Torbjorn Sjostrand.
0003 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
0004 // Please respect the MCnet Guidelines, see GUIDELINES for details.
0005 
0006 // This file contains the Logger class for diagnostic messages.
0007 
0008 #ifndef Pythia8_Logger_H
0009 #define Pythia8_Logger_H
0010 
0011 #include "Pythia8/PythiaStdlib.h"
0012 
0013 namespace Pythia8 {
0014 
0015 class Settings;
0016 
0017 //==========================================================================
0018 
0019 // The Logger class prints diagnostic messages and counts error messages.
0020 // Objects of the Logger class can be used as ostream object,
0021 // supporting the << operator.
0022 
0023 class Logger : private streambuf, public ostream {
0024 
0025   // This struct defines the ordering of log messages.
0026   // If the two messages have different severity (abort, error, warning,
0027   // info, debug), order by most severe first. Otherwise, order alphabetically.
0028   struct LogComparer {
0029     bool operator() (const string& a, const string& b) const {
0030       char a1 = a[0], b1 = b[0];
0031       int severityA, severityB;
0032       if      (a1 == 'A') severityA = 0;
0033       else if (a1 == 'E') severityA = 1;
0034       else if (a1 == 'W') severityA = 2;
0035       else if (a1 == 'I') severityA = 3;
0036       else if (a1 == 'R') severityA = 4;
0037       else severityA = 5;
0038       if      (b1 == 'A') severityB = 0;
0039       else if (b1 == 'E') severityB = 1;
0040       else if (b1 == 'W') severityB = 2;
0041       else if (b1 == 'I') severityB = 3;
0042       else if (b1 == 'R') severityB = 4;
0043       else severityB = 5;
0044       if (severityA != severityB) return severityA < severityB;
0045       return a < b;
0046     }
0047   };
0048 
0049 public:
0050 
0051   // Construct with default values.
0052   Logger() : ostream(this), infoStreamSave(cout), errStreamSave(cerr),
0053     verbosity(2), printInitSave(true), printNextSave(true), printErrors(true),
0054     isQuietSave(false), useErrorStream(false) { }
0055   virtual ~Logger() {}
0056 
0057   void init(Settings& settings);
0058 
0059   // Methods for providing different levels of error messaging.
0060   // These methods are thread safe.
0061 
0062   // Report messages contain information not relevant in normal runs.
0063   void reportMsg(string loc, string message, string extraInfo = "",
0064     bool showAlways = false);
0065 
0066   // Info messages are diagnostic messages that don't indicate an issue.
0067   void infoMsg(string loc, string message, string extraInfo = "",
0068     bool showAlways = false);
0069 
0070   // Warnings indicate that there might be an issue, but the run will continue.
0071   void warningMsg(string loc, string message, string extraInfo = "",
0072     bool showAlways = false);
0073 
0074   // Errors indicate an issue that might cause the current event to fail.
0075   void errorMsg(string loc, string message, string extraInfo = "",
0076     bool showAlways = false);
0077 
0078   // Aborts indicate critical issues that prevent further event generation.
0079   void abortMsg(string loc, string message, string extraInfo = "",
0080     bool showAlways = false);
0081 
0082   // If quiet, the logger will not print any messages.
0083   bool isQuiet()      const { return isQuietSave; }
0084 
0085   // Indicates whether information will be printed during initialization.
0086   bool mayPrintInit() const { return printInitSave && !isQuietSave; }
0087 
0088   // Indicates whether information will be printed during event generation.
0089   bool mayPrintNext() const { return printNextSave && !isQuietSave; }
0090 
0091   // Indicates whether error messages will be printed.
0092   bool mayPrintErrors() const { return printErrors && !isQuietSave; }
0093 
0094   // 0: no messages | 1: aborts only | 2: default | 3: debug
0095   void setVerbosity(int verbosityIn) { verbosity = verbosityIn; }
0096   int getVerbosity() const { return verbosity; }
0097 
0098   // Add all errors from the other Logger object to the counts of this object.
0099   void errorCombine(const Logger& other, string prefix = "");
0100 
0101   // Reset to empty map of error messages.
0102   void errorReset() {messages.clear();}
0103 
0104   // Total number of errors/aborts/warnings logged.
0105   int errorTotalNumber() const {
0106     int nTot = 0;
0107     for (pair<string, int> messageEntry : messages)
0108       nTot += messageEntry.second;
0109     return nTot;
0110   }
0111 
0112   // Print error statistics.
0113   void errorStatistics() const { errorStatistics(infoStreamSave); }
0114   void errorStatistics(ostream& stream) const;
0115 
0116   ostream& infoStream() { return infoStreamSave; }
0117   ostream& errorStream() {
0118     return useErrorStream ? errStreamSave : infoStreamSave; }
0119 
0120   // Iterators over error messages.
0121   map<string, int, LogComparer>::iterator begin() {
0122     return messages.begin(); }
0123   map<string, int, LogComparer>::iterator end() {
0124     return messages.end(); }
0125   map<string, int, LogComparer>::const_iterator begin() const {
0126     return messages.begin(); }
0127   map<string, int, LogComparer>::const_iterator end() const {
0128     return messages.end(); }
0129 
0130   // Abort verbosity level: print only abort messages.
0131   static constexpr int ABORT  = 1;
0132   // Normal verbosity level: print errors, warnings and info messages.
0133   static constexpr int NORMAL = 2;
0134   // Report verbosity level: print everything, including report messages.
0135   static constexpr int REPORT = 3;
0136 
0137 private:
0138 
0139   // Map for all error messages.
0140   map<string, int, LogComparer> messages;
0141 
0142   // Override method from std::streambuf.
0143   // This makes the operator<< write to infoStreamSave.
0144   int overflow(int c) override {
0145     infoStreamSave.put(c);
0146     return 0;
0147   }
0148 
0149   // Streams where messages are written.
0150   ostream& infoStreamSave;
0151   // Optional separate error stream.
0152   ostream& errStreamSave;
0153 
0154   // Configuration.
0155   int verbosity;
0156   bool printInitSave, printNextSave, printErrors, isQuietSave, useErrorStream;
0157 
0158   // Mutual exclusive access to writing messages.
0159   mutex writeMutex;
0160 
0161   // Method to print the diagnostic message. This method is thread safe.
0162   void msg(int verbosityLevel, string message, string extraInfo = "",
0163     bool showAlways = false);
0164 
0165 };
0166 
0167 //--------------------------------------------------------------------------
0168 
0169 // Shorthand macros for logger messages.
0170 
0171 #define INFO_MSG(...) infoMsg(__METHOD_NAME__, __VA_ARGS__)
0172 #define WARNING_MSG(...) warningMsg(__METHOD_NAME__, __VA_ARGS__)
0173 #define ERROR_MSG(...) errorMsg(__METHOD_NAME__, __VA_ARGS__)
0174 #define ABORT_MSG(...) abortMsg(__METHOD_NAME__, __VA_ARGS__)
0175 #define REPORT_MSG(...) reportMsg(__METHOD_NAME__, __VA_ARGS__)
0176 
0177 //==========================================================================
0178 
0179 } // end namespace Pythia8
0180 
0181 #endif // Pythia8_Logger_H