Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:18

0001 #ifndef __FASTJET_LIMITEDWARNING_HH__
0002 #define __FASTJET_LIMITEDWARNING_HH__
0003 
0004 //FJSTARTHEADER
0005 // $Id$
0006 //
0007 // Copyright (c) 2005-2021, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
0008 //
0009 //----------------------------------------------------------------------
0010 // This file is part of FastJet.
0011 //
0012 //  FastJet is free software; you can redistribute it and/or modify
0013 //  it under the terms of the GNU General Public License as published by
0014 //  the Free Software Foundation; either version 2 of the License, or
0015 //  (at your option) any later version.
0016 //
0017 //  The algorithms that underlie FastJet have required considerable
0018 //  development. They are described in the original FastJet paper,
0019 //  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
0020 //  FastJet as part of work towards a scientific publication, please
0021 //  quote the version you use and include a citation to the manual and
0022 //  optionally also to hep-ph/0512210.
0023 //
0024 //  FastJet is distributed in the hope that it will be useful,
0025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
0026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0027 //  GNU General Public License for more details.
0028 //
0029 //  You should have received a copy of the GNU General Public License
0030 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
0031 //----------------------------------------------------------------------
0032 //FJENDHEADER
0033 
0034 
0035 #include "fastjet/internal/base.hh"
0036 #include <iostream>
0037 #include <string>
0038 #include <list>
0039 
0040 #include "fastjet/config.h"
0041 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
0042 #include <atomic>
0043 #include <mutex>
0044 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
0045 #include "fastjet/internal/thread_safety_helpers.hh" // provides a counter, thread-safe if needed
0046 
0047 FASTJET_BEGIN_NAMESPACE
0048 
0049 /// @ingroup error_handling
0050 /// \class LimitedWarning
0051 /// class to provide facilities for giving warnings up to some maximum
0052 /// number of times and to provide global summaries of warnings that have
0053 /// been issued.
0054 class LimitedWarning {
0055 public:
0056   
0057   /// constructor that provides a default maximum number of warnings
0058   LimitedWarning() : _max_warn(_max_warn_default),_this_warning_summary(0) {}
0059 
0060   /// constructor that provides a user-set max number of warnings
0061   LimitedWarning(int max_warn_in) : _max_warn(max_warn_in), _this_warning_summary(0) {}  
0062 
0063 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
0064   /// copy ctor (have to be specified explicitly because of the atomic variable)
0065   LimitedWarning(const LimitedWarning &other)
0066     : _max_warn(other._max_warn), _this_warning_summary{other._this_warning_summary.load()} {}  
0067 #endif
0068   
0069   /// outputs a warning to standard error (or the user's default
0070   /// warning stream if set)
0071   void warn(const char * warning) {warn(warning, _default_ostr);}
0072 
0073   /// outputs a warning to standard error (or the user's default
0074   /// warning stream if set)
0075   void warn(const std::string & warning) {warn(warning.c_str(), _default_ostr);}
0076 
0077   /// outputs a warning to the specified stream
0078   void warn(const char * warning, std::ostream * ostr);
0079 
0080   /// outputs a warning to the specified stream
0081   void warn(const std::string & warning, std::ostream * ostr) {warn(warning.c_str(), ostr);}
0082 
0083   /// sets the default output stream for all warnings (by default
0084   /// cerr; passing a null pointer prevents warnings from being output)
0085   static void set_default_stream(std::ostream * ostr) {
0086     _default_ostr = ostr;
0087   }
0088 
0089 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
0090   /// sets the default output stream for all warnings (by default
0091   /// cerr; passing a null pointer prevents warnings from being output)
0092   /// The second argument is a mutex that would be used to guarantee 
0093   /// that only a single thread writes to the stream at a time
0094   static void set_default_stream_and_mutex(std::ostream * ostr,
0095                                            std::mutex * warnings_mutex) {
0096     _default_ostr  = ostr;
0097     _stream_mutex = warnings_mutex;
0098   }
0099 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
0100 
0101   /// sets the default maximum number of warnings of a given kind
0102   /// before warning messages are silenced.
0103   static void set_default_max_warn(int max_warn) {
0104     _max_warn_default = max_warn;
0105   }
0106 
0107   /// the maximum number of warning messages that will be printed
0108   /// by this instance of the class
0109   int max_warn() const {return _max_warn;}
0110 
0111   /// the number of times so far that a warning has been registered
0112   /// with this instance of the class.
0113   int n_warn_so_far() const;
0114 
0115   /// returns a summary of all the warnings that came through the
0116   /// LimiteWarning class
0117   static std::string summary();
0118 
0119 private:
0120   const int _max_warn;
0121 
0122   typedef std::pair<std::string, thread_safety_helpers::AtomicCounter<unsigned int> > Summary;
0123 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
0124   static std::atomic<int> _max_warn_default;
0125   static std::atomic<std::ostream *> _default_ostr;
0126   static std::atomic<std::mutex *> _stream_mutex;
0127   static std::mutex _global_warnings_summary_mutex;
0128   std::atomic<Summary*> _this_warning_summary;
0129 #else
0130   static int _max_warn_default;
0131   static std::ostream * _default_ostr;
0132   Summary* _this_warning_summary;
0133 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
0134 
0135   // Note that this is updated internally and we use a mutex for the
0136   // thread-safe version. So no other specific treatment is needed at
0137   // this level.
0138   static std::list< Summary > _global_warnings_summary;
0139  
0140 };
0141 
0142 FASTJET_END_NAMESPACE
0143 
0144 #endif // __FASTJET_LIMITEDWARNING_HH__