FastJet 3.4.1
LimitedWarning.hh
1#ifndef __FASTJET_LIMITEDWARNING_HH__
2#define __FASTJET_LIMITEDWARNING_HH__
3
4//FJSTARTHEADER
5// $Id$
6//
7// Copyright (c) 2005-2023, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
8//
9//----------------------------------------------------------------------
10// This file is part of FastJet.
11//
12// FastJet is free software; you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation; either version 2 of the License, or
15// (at your option) any later version.
16//
17// The algorithms that underlie FastJet have required considerable
18// development. They are described in the original FastJet paper,
19// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
20// FastJet as part of work towards a scientific publication, please
21// quote the version you use and include a citation to the manual and
22// optionally also to hep-ph/0512210.
23//
24// FastJet is distributed in the hope that it will be useful,
25// but WITHOUT ANY WARRANTY; without even the implied warranty of
26// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27// GNU General Public License for more details.
28//
29// You should have received a copy of the GNU General Public License
30// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
31//----------------------------------------------------------------------
32//FJENDHEADER
33
34
35#include "fastjet/internal/base.hh"
36#include <iostream>
37#include <string>
38#include <list>
39
40#include "fastjet/config.h"
41#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
42#include <atomic>
43#include <mutex>
44#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
45#include "fastjet/internal/thread_safety_helpers.hh" // provides a counter, thread-safe if needed
46
47FASTJET_BEGIN_NAMESPACE
48
49/// @ingroup error_handling
50/// \class LimitedWarning
51/// class to provide facilities for giving warnings up to some maximum
52/// number of times and to provide global summaries of warnings that have
53/// been issued.
55public:
56
57 /// constructor that provides a default maximum number of warnings
58 LimitedWarning() : _max_warn(_max_warn_default),_this_warning_summary(0) {}
59
60 /// constructor that provides a user-set max number of warnings
61 LimitedWarning(int max_warn_in) : _max_warn(max_warn_in), _this_warning_summary(0) {}
62
63#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
64 /// copy ctor (have to be specified explicitly because of the atomic variable)
65 LimitedWarning(const LimitedWarning &other)
66 : _max_warn(other._max_warn), _this_warning_summary{other._this_warning_summary.load()} {}
67#endif
68
69 /// outputs a warning to standard error (or the user's default
70 /// warning stream if set)
71 void warn(const char * warning) {warn(warning, _default_ostr);}
72
73 /// outputs a warning to standard error (or the user's default
74 /// warning stream if set)
75 void warn(const std::string & warning) {warn(warning.c_str(), _default_ostr);}
76
77 /// outputs a warning to the specified stream
78 void warn(const char * warning, std::ostream * ostr);
79
80 /// outputs a warning to the specified stream
81 void warn(const std::string & warning, std::ostream * ostr) {warn(warning.c_str(), ostr);}
82
83 /// sets the default output stream for all warnings (by default
84 /// cerr; passing a null pointer prevents warnings from being output)
85 static void set_default_stream(std::ostream * ostr) {
86 _default_ostr = ostr;
87 }
88
89#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
90 /// sets the default output stream for all warnings (by default
91 /// cerr; passing a null pointer prevents warnings from being output)
92 /// The second argument is a mutex that would be used to guarantee
93 /// that only a single thread writes to the stream at a time
94 static void set_default_stream_and_mutex(std::ostream * ostr,
95 std::mutex * warnings_mutex) {
96 _default_ostr = ostr;
97 _stream_mutex = warnings_mutex;
98 }
99#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
100
101 /// sets the default maximum number of warnings of a given kind
102 /// before warning messages are silenced.
103 static void set_default_max_warn(int max_warn) {
104 _max_warn_default = max_warn;
105 }
106
107 /// the maximum number of warning messages that will be printed
108 /// by this instance of the class
109 int max_warn() const {return _max_warn;}
110
111 /// the number of times so far that a warning has been registered
112 /// with this instance of the class.
113 int n_warn_so_far() const;
114
115 /// returns a summary of all the warnings that came through the
116 /// LimiteWarning class
117 static std::string summary();
118
119private:
120 const int _max_warn;
121
122 typedef std::pair<std::string, thread_safety_helpers::AtomicCounter<unsigned int> > Summary;
123#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
124 static std::atomic<int> _max_warn_default;
125 static std::atomic<std::ostream *> _default_ostr;
126 static std::atomic<std::mutex *> _stream_mutex;
127 static std::mutex _global_warnings_summary_mutex;
128 std::atomic<Summary*> _this_warning_summary;
129#else
130 static int _max_warn_default;
131 static std::ostream * _default_ostr;
132 Summary* _this_warning_summary;
133#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
134
135 // Note that this is updated internally and we use a mutex for the
136 // thread-safe version. So no other specific treatment is needed at
137 // this level.
138 static std::list< Summary > _global_warnings_summary;
139
140};
141
142FASTJET_END_NAMESPACE
143
144#endif // __FASTJET_LIMITEDWARNING_HH__
class to provide facilities for giving warnings up to some maximum number of times and to provide glo...
void warn(const std::string &warning)
outputs a warning to standard error (or the user's default warning stream if set)
static void set_default_stream(std::ostream *ostr)
sets the default output stream for all warnings (by default cerr; passing a null pointer prevents war...
static void set_default_max_warn(int max_warn)
sets the default maximum number of warnings of a given kind before warning messages are silenced.
void warn(const char *warning)
outputs a warning to standard error (or the user's default warning stream if set)
LimitedWarning()
constructor that provides a default maximum number of warnings
void warn(const std::string &warning, std::ostream *ostr)
outputs a warning to the specified stream
int max_warn() const
the maximum number of warning messages that will be printed by this instance of the class
LimitedWarning(int max_warn_in)
constructor that provides a user-set max number of warnings