FastJet 3.4.3
Loading...
Searching...
No Matches
LimitedWarning.cc
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2024, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
5//
6//----------------------------------------------------------------------
7// This file is part of FastJet.
8//
9// FastJet is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// The algorithms that underlie FastJet have required considerable
15// development. They are described in the original FastJet paper,
16// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
17// FastJet as part of work towards a scientific publication, please
18// quote the version you use and include a citation to the manual and
19// optionally also to hep-ph/0512210.
20//
21// FastJet is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
28//----------------------------------------------------------------------
29//FJENDHEADER
30
31#include "fastjet/LimitedWarning.hh"
32#include <sstream>
33#include <limits>
34
35using namespace std;
36
37FASTJET_BEGIN_NAMESPACE
38
39#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
40atomic<ostream *> LimitedWarning::_default_ostr{&cerr};
41atomic<mutex *> LimitedWarning::_stream_mutex{nullptr};
42atomic<int> LimitedWarning::_max_warn_default{5};
43std::mutex LimitedWarning::_global_warnings_summary_mutex;
44#else
45ostream * LimitedWarning::_default_ostr = &cerr;
46int LimitedWarning::_max_warn_default = 5;
47#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
48
49std::list< LimitedWarning::Summary > LimitedWarning::_global_warnings_summary;
50
51// /// output a warning to ostr
52// void LimitedWarning::warn(const std::string & warning) {
53// warn(warning, _default_ostr);
54// }
55
56/// the number of times so far that a warning has been registered
57/// with this instance of the class.
58int LimitedWarning::n_warn_so_far() const{
59 // explicitly cast to the pointer type (useless wo thread-safety
60 // features but works around an issue with the intel compiler
61 // (v13.1.3) with thread-safety features
62 if (((LimitedWarning::Summary *)_this_warning_summary) == 0) return 0;
63 return (*_this_warning_summary).second;
64}
65
66
67void LimitedWarning::warn(const char * warning, std::ostream * ostr) {
68 // update the summary
69 if (((LimitedWarning::Summary *)_this_warning_summary) == 0){
70#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
71 // Threadsafety note:
72 // here we need to lock _this_warning_summary to be sure that we
73 // can quietly initialising things without taking the risk that
74 // another thread "regenerates" the Summary entry at the same time
75 //
76 // first acquire the mutex!
77 // See e.g. http://baptiste-wicht.com/posts/2012/03/cp11-concurrency-tutorial-part-2-protect-shared-data.html
78 // for a quick intro.
79 std::lock_guard<std::mutex> guard(_global_warnings_summary_mutex);
80 // then make sure we still need to create things (in case another
81 // thread got us beaten (it's better to use the mutex as little as
82 // possible, hence the repetition of the text which might
83 // otherwise look stupid)
84 if (((LimitedWarning::Summary *)_this_warning_summary) == 0){
85 // prepare the information for the summary
86 _global_warnings_summary.push_back(Summary(warning, 0));
87 _this_warning_summary = & (_global_warnings_summary.back());
88 }
89 // the lock will automatically be released here
90#else
91 // prepare the information for the summary
92 _global_warnings_summary.push_back(Summary(warning, 0));
93 _this_warning_summary = & (_global_warnings_summary.back());
94#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
95 }
96
97
98 // maintain the count, but do not allow overflow
99 unsigned int count = (*_this_warning_summary).second.step();
100
101 // print the warning if we have not done it enough already
102 if ((_max_warn<0) || (count < (unsigned int)_max_warn)) {
103 // prepare the warning within a string stream
104 ostringstream warnstr;
105 warnstr << "WARNING from FastJet: ";
106 warnstr << warning;
107 if ((_max_warn>0) && (count+1 == (unsigned int)_max_warn))
108 warnstr << " (LAST SUCH WARNING)";
109 warnstr << std::endl;
110 // arrange for the whole warning to be output in one go (that way
111 // user can easily insert their own printout, e.g. event number
112 // before the warning string).
113 if (ostr) {
114 // if there is a mutex, use it to lock
115#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
116 if (_stream_mutex){
117 std::lock_guard<std::mutex> guard(*_stream_mutex);
118 (*ostr) << warnstr.str();
119 ostr->flush(); // get something written to file even if the program aborts
120 } else
121#endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
122 {
123 (*ostr) << warnstr.str();
124 ostr->flush(); // get something written to file even if the program aborts
125 }
126 }
127 }
128
129}
130
131//----------------------------------------------------------------------
132string LimitedWarning::summary() {
133 ostringstream str;
134#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
135 {
136 // is the lock here really necessary. The only potential issue I
137 // see is if another thread adds a warning when the loop below
138 // calls it++ on the previous last element. Is there a simpler way
139 // to handle this?
140 std::lock_guard<std::mutex> guard(_global_warnings_summary_mutex);
141#endif
142 for (list<Summary>::const_iterator it = _global_warnings_summary.begin();
143 it != _global_warnings_summary.end(); it++) {
144 str << it->second << " times: " << it->first << endl;
145 }
146#ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
147 }
148#endif
149 return str.str();
150}
151
152FASTJET_END_NAMESPACE