FastJet 3.0.2
LimitedWarning.cc
00001 //STARTHEADER
00002 // $Id: LimitedWarning.cc 2577 2011-09-13 15:11:38Z salam $
00003 //
00004 // Copyright (c) 2005-2011, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
00026 //----------------------------------------------------------------------
00027 //ENDHEADER
00028 
00029 #include "fastjet/LimitedWarning.hh"
00030 #include <sstream>
00031 #include <limits>
00032 
00033 using namespace std;
00034 
00035 FASTJET_BEGIN_NAMESPACE
00036 
00037 ostream * LimitedWarning::_default_ostr = &cerr;
00038 std::list< LimitedWarning::Summary > LimitedWarning::_global_warnings_summary;
00039 int LimitedWarning::_max_warn_default = 5;
00040 
00041 
00042 /// output a warning to ostr
00043 void LimitedWarning::warn(const std::string & warning) {
00044   warn(warning, _default_ostr);
00045 }
00046 
00047 void LimitedWarning::warn(const std::string & warning, std::ostream * ostr) {
00048   if (_this_warning_summary == 0) {
00049     // prepare the information for the summary
00050     _global_warnings_summary.push_back(Summary(warning, 0));
00051     _this_warning_summary = & (_global_warnings_summary.back());
00052   }
00053   if (_n_warn_so_far < _max_warn) {
00054     // prepare the warning within a string stream
00055     ostringstream warnstr;
00056     warnstr << "WARNING: ";
00057     warnstr << warning;
00058     _n_warn_so_far++;
00059     if (_n_warn_so_far == _max_warn) warnstr << " (LAST SUCH WARNING)";
00060     warnstr << std::endl;
00061     // arrange for the whole warning to be output in one go (that way
00062     // user can easily insert their own printout, e.g. event number
00063     // before the warning string).
00064     if (ostr) {
00065       (*ostr) << warnstr.str();
00066       ostr->flush(); // get something written to file even if the program aborts
00067     }
00068   }
00069 
00070   // maintain the count, but do not allow overflow
00071   if (_this_warning_summary->second < numeric_limits<unsigned>::max()) {
00072     _this_warning_summary->second++;
00073   }
00074 }
00075 
00076 //----------------------------------------------------------------------
00077 string LimitedWarning::summary() {
00078   ostringstream str;
00079   for (list<Summary>::const_iterator it = _global_warnings_summary.begin();
00080        it != _global_warnings_summary.end(); it++) {
00081     str << it->second << " times: " << it->first << endl;
00082   }
00083   return str.str();
00084 }
00085 
00086 FASTJET_END_NAMESPACE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends