FastJet  3.0.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LimitedWarning.cc
1 //STARTHEADER
2 // $Id: LimitedWarning.cc 2577 2011-09-13 15:11:38Z salam $
3 //
4 // Copyright (c) 2005-2011, 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 and are described in hep-ph/0512210. If you use
16 // FastJet as part of work towards a scientific publication, please
17 // include a citation to the FastJet paper.
18 //
19 // FastJet is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with FastJet. If not, see <http://www.gnu.org/licenses/>.
26 //----------------------------------------------------------------------
27 //ENDHEADER
28 
29 #include "fastjet/LimitedWarning.hh"
30 #include <sstream>
31 #include <limits>
32 
33 using namespace std;
34 
35 FASTJET_BEGIN_NAMESPACE
36 
37 ostream * LimitedWarning::_default_ostr = &cerr;
38 std::list< LimitedWarning::Summary > LimitedWarning::_global_warnings_summary;
39 int LimitedWarning::_max_warn_default = 5;
40 
41 
42 /// output a warning to ostr
43 void LimitedWarning::warn(const std::string & warning) {
44  warn(warning, _default_ostr);
45 }
46 
47 void LimitedWarning::warn(const std::string & warning, std::ostream * ostr) {
48  if (_this_warning_summary == 0) {
49  // prepare the information for the summary
50  _global_warnings_summary.push_back(Summary(warning, 0));
51  _this_warning_summary = & (_global_warnings_summary.back());
52  }
53  if (_n_warn_so_far < _max_warn) {
54  // prepare the warning within a string stream
55  ostringstream warnstr;
56  warnstr << "WARNING: ";
57  warnstr << warning;
58  _n_warn_so_far++;
59  if (_n_warn_so_far == _max_warn) warnstr << " (LAST SUCH WARNING)";
60  warnstr << std::endl;
61  // arrange for the whole warning to be output in one go (that way
62  // user can easily insert their own printout, e.g. event number
63  // before the warning string).
64  if (ostr) {
65  (*ostr) << warnstr.str();
66  ostr->flush(); // get something written to file even if the program aborts
67  }
68  }
69 
70  // maintain the count, but do not allow overflow
71  if (_this_warning_summary->second < numeric_limits<unsigned>::max()) {
72  _this_warning_summary->second++;
73  }
74 }
75 
76 //----------------------------------------------------------------------
77 string LimitedWarning::summary() {
78  ostringstream str;
79  for (list<Summary>::const_iterator it = _global_warnings_summary.begin();
80  it != _global_warnings_summary.end(); it++) {
81  str << it->second << " times: " << it->first << endl;
82  }
83  return str.str();
84 }
85 
86 FASTJET_END_NAMESPACE