FastJet  3.4.0-beta.1
LimitedWarning.cc
1 //FJSTARTHEADER
2 // $Id$
3 //
4 // Copyright (c) 2005-2021, 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 
35 using namespace std;
36 
37 FASTJET_BEGIN_NAMESPACE
38 
39 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
40 atomic<ostream *> LimitedWarning::_default_ostr{&cerr};
41 atomic<int> LimitedWarning::_max_warn_default{5};
42 std::mutex LimitedWarning::_global_warnings_summary_mutex;
43 #else
44 ostream * LimitedWarning::_default_ostr = &cerr;
45 int LimitedWarning::_max_warn_default = 5;
46 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
47 
48 std::list< LimitedWarning::Summary > LimitedWarning::_global_warnings_summary;
49 
50 // /// output a warning to ostr
51 // void LimitedWarning::warn(const std::string & warning) {
52 // warn(warning, _default_ostr);
53 // }
54 
55 /// the number of times so far that a warning has been registered
56 /// with this instance of the class.
57 int LimitedWarning::n_warn_so_far() const{
58  // explicitly cast to the pointer type (useless wo thread-safety
59  // features but works around an issue with the intel compiler
60  // (v13.1.3) with thread-safety features
61  if (((LimitedWarning::Summary *)_this_warning_summary) == 0) return 0;
62  return (*_this_warning_summary).second;
63 }
64 
65 
66 void LimitedWarning::warn(const char * warning, std::ostream * ostr) {
67  // update the summary
68  if (((LimitedWarning::Summary *)_this_warning_summary) == 0){
69 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
70  // Threadsafety note:
71  // here we need to lock _this_warning_summary to be sure that we
72  // can quietly initialising things without taking the risk that
73  // another thread "regenerates" the Summary entry at the same time
74  //
75  // first acquire the mutex!
76  // See e.g. http://baptiste-wicht.com/posts/2012/03/cp11-concurrency-tutorial-part-2-protect-shared-data.html
77  // for a quick intro.
78  std::lock_guard<std::mutex> guard(_global_warnings_summary_mutex);
79  // then make sure we still need to create things (in case another
80  // thread got us beaten (it's better to use the mutex as little as
81  // possible, hence the repetition of the text which might
82  // otherwise look stupid)
83  if (((LimitedWarning::Summary *)_this_warning_summary) == 0){
84  // prepare the information for the summary
85  _global_warnings_summary.push_back(Summary(warning, 0));
86  _this_warning_summary = & (_global_warnings_summary.back());
87  }
88  // the lock will automatically be released here
89 #else
90  // prepare the information for the summary
91  _global_warnings_summary.push_back(Summary(warning, 0));
92  _this_warning_summary = & (_global_warnings_summary.back());
93 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
94  }
95 
96 
97  // maintain the count, but do not allow overflow
98  unsigned int count = (*_this_warning_summary).second.step();
99 
100  // print the warning if we have not done it enough already
101  if ((_max_warn<0) || (count < (unsigned int)_max_warn)) {
102  // prepare the warning within a string stream
103  ostringstream warnstr;
104  warnstr << "WARNING from FastJet: ";
105  warnstr << warning;
106  if ((_max_warn>0) && (count+1 == (unsigned int)_max_warn))
107  warnstr << " (LAST SUCH WARNING)";
108  warnstr << std::endl;
109  // arrange for the whole warning to be output in one go (that way
110  // user can easily insert their own printout, e.g. event number
111  // before the warning string).
112  if (ostr) {
113  (*ostr) << warnstr.str();
114  ostr->flush(); // get something written to file even if the program aborts
115  }
116  }
117 
118 }
119 
120 //----------------------------------------------------------------------
121 string LimitedWarning::summary() {
122  ostringstream str;
123 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
124  {
125  // is the lock here really necessary. The only potential issue I
126  // see is if another thread adds a warning when the loop below
127  // calls it++ on the previous last element. Is there a simpler way
128  // to handle this?
129  std::lock_guard<std::mutex> guard(_global_warnings_summary_mutex);
130 #endif
131  for (list<Summary>::const_iterator it = _global_warnings_summary.begin();
132  it != _global_warnings_summary.end(); it++) {
133  str << it->second << " times: " << it->first << endl;
134  }
135 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
136  }
137 #endif
138  return str.str();
139 }
140 
141 FASTJET_END_NAMESPACE