FastJet  3.3.3
LimitedWarning.cc
1 //FJSTARTHEADER
2 // $Id: LimitedWarning.cc 4420 2019-11-29 09:28:20Z soyez $
3 //
4 // Copyright (c) 2005-2019, 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 ostream * LimitedWarning::_default_ostr = &cerr;
40 std::list< LimitedWarning::Summary > LimitedWarning::_global_warnings_summary;
41 int LimitedWarning::_max_warn_default = 5;
42 
43 
44 // /// output a warning to ostr
45 // void LimitedWarning::warn(const std::string & warning) {
46 // warn(warning, _default_ostr);
47 // }
48 
49 void LimitedWarning::warn(const char * warning, std::ostream * ostr) {
50  if (_this_warning_summary == 0) {
51  // prepare the information for the summary
52  _global_warnings_summary.push_back(Summary(warning, 0));
53  _this_warning_summary = & (_global_warnings_summary.back());
54  }
55  if (_n_warn_so_far < _max_warn) {
56  // prepare the warning within a string stream
57  ostringstream warnstr;
58  warnstr << "WARNING from FastJet: ";
59  warnstr << warning;
60  _n_warn_so_far++;
61  if (_n_warn_so_far == _max_warn) warnstr << " (LAST SUCH WARNING)";
62  warnstr << std::endl;
63  // arrange for the whole warning to be output in one go (that way
64  // user can easily insert their own printout, e.g. event number
65  // before the warning string).
66  if (ostr) {
67  (*ostr) << warnstr.str();
68  ostr->flush(); // get something written to file even if the program aborts
69  }
70  }
71 
72  // maintain the count, but do not allow overflow
73  if (_this_warning_summary->second < numeric_limits<unsigned>::max()) {
74  _this_warning_summary->second++;
75  }
76 }
77 
78 //----------------------------------------------------------------------
79 string LimitedWarning::summary() {
80  ostringstream str;
81  for (list<Summary>::const_iterator it = _global_warnings_summary.begin();
82  it != _global_warnings_summary.end(); it++) {
83  str << it->second << " times: " << it->first << endl;
84  }
85  return str.str();
86 }
87 
88 FASTJET_END_NAMESPACE