FastJet  3.4.0-beta.1
Error.hh
1  #ifndef __FASTJET_ERROR_HH__
2 #define __FASTJET_ERROR_HH__
3 
4 //FJSTARTHEADER
5 // $Id$
6 //
7 // Copyright (c) 2005-2021, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
8 //
9 //----------------------------------------------------------------------
10 // This file is part of FastJet.
11 //
12 // FastJet is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
16 //
17 // The algorithms that underlie FastJet have required considerable
18 // development. They are described in the original FastJet paper,
19 // hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
20 // FastJet as part of work towards a scientific publication, please
21 // quote the version you use and include a citation to the manual and
22 // optionally also to hep-ph/0512210.
23 //
24 // FastJet is distributed in the hope that it will be useful,
25 // but WITHOUT ANY WARRANTY; without even the implied warranty of
26 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 // GNU General Public License for more details.
28 //
29 // You should have received a copy of the GNU General Public License
30 // along with FastJet. If not, see <http://www.gnu.org/licenses/>.
31 //----------------------------------------------------------------------
32 //FJENDHEADER
33 
34 #include<iostream>
35 #include<string>
36 #include "fastjet/internal/base.hh"
37 #include "fastjet/config.h"
38 //#include <exception>
39 #if (!defined(FASTJET_HAVE_EXECINFO_H)) || defined(__FJCORE__)
40 #include "fastjet/LimitedWarning.hh"
41 #endif
42 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
43 #include <atomic>
44 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
45 
46 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
47 
48 /// @ingroup error_handling
49 /// \class Error
50 /// base class corresponding to errors that can be thrown by FastJet
51 class Error{
52 public:
53  /// default constructors
54  Error() {}
55 
56  /// ctor from an error message
57  /// \param message to be printed
58  /// Note: in addition to the error message, one can choose to print the
59  /// backtrace (showing the last few calls before the error) by
60  /// using set_print_backtrace(true). The default is "false".
61  Error(const std::string & message);
62 
63  /// virtual dummy dtor
64  virtual ~Error() {}
65 
66  /// the error message
67  std::string message() const {return _message;}
68 
69  /// an alternative access to the error message (more standard)
70  std::string description() const {return message();}
71 
72  /// controls whether the error message (and the backtrace, if its printing is enabled)
73  /// is printed out or not
74  static void set_print_errors(bool print_errors) {_print_errors = print_errors;}
75 
76  /// controls whether the backtrace is printed out with the error message or not.
77  /// The default is "false".
78  static void set_print_backtrace(bool enabled);
79 
80  /// sets the default output stream for all errors; by default
81  /// cerr; if it's null then error output is suppressed.
82  static void set_default_stream(std::ostream * ostr) {
83  _default_ostr = ostr;
84  }
85 
86 private:
87 
88 #ifndef __FJCORE__
89 #if defined(FASTJET_HAVE_EXECINFO_H) && defined(FASTJET_HAVE_DEMANGLING_SUPPORT)
90  /// demangle a given backtrace symbol
91  std::string _demangle(const char* symbol);
92 #endif
93 #endif
94 
95  std::string _message; ///< error message
96 
97 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
98  static std::atomic<bool> _print_errors; ///< do we print anything?
99  static std::atomic<bool> _print_backtrace; ///< do we print the backtrace?
100  static std::atomic<std::ostream *> _default_ostr; ///< the output stream (cerr if not set)
101 #else
102  static bool _print_errors; ///< do we print anything?
103  static bool _print_backtrace; ///< do we print the backtrace?
104  static std::ostream * _default_ostr; ///< the output stream (cerr if not set)
105 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
106 
107 
108 #if (!defined(FASTJET_HAVE_EXECINFO_H)) || defined(__FJCORE__)
109  static LimitedWarning _execinfo_undefined;
110 #endif
111 };
112 
113 
114 /// @ingroup error_handling
115 /// \class InternalError
116 /// class corresponding to critical internal errors
117 ///
118 /// This is an error class (derived from Error) meant for serious,
119 /// critical, internal errors that we still want to be catchable by an
120 /// end-user [e.g. a serious issue in clustering where the end-user
121 /// can catch it and retry with a different strategy]
122 ///
123 /// Please directly contact the FastJet authors if you see such an
124 /// error.
125 class InternalError : public Error{
126 public:
127  /// ctor with error message:
128  /// just add a bit of info to the message and pass it to the base class
129  InternalError(const std::string & message_in) : Error(std::string("*** CRITICAL INTERNAL FASTJET ERROR *** CONTACT THE AUTHORS *** ") + message_in){ }
130 };
131 
132 FASTJET_END_NAMESPACE
133 
134 #endif // __FASTJET_ERROR_HH__
fastjet::InternalError::InternalError
InternalError(const std::string &message_in)
ctor with error message: just add a bit of info to the message and pass it to the base class
Definition: Error.hh:129
fastjet::Error::message
std::string message() const
the error message
Definition: Error.hh:67
fastjet::Error::description
std::string description() const
an alternative access to the error message (more standard)
Definition: Error.hh:70
fastjet::Error::Error
Error()
default constructors
Definition: Error.hh:54
fastjet::Error::~Error
virtual ~Error()
virtual dummy dtor
Definition: Error.hh:64
fastjet::Error::set_print_errors
static void set_print_errors(bool print_errors)
controls whether the error message (and the backtrace, if its printing is enabled) is printed out or ...
Definition: Error.hh:74
fastjet::Error::set_default_stream
static void set_default_stream(std::ostream *ostr)
sets the default output stream for all errors; by default cerr; if it's null then error output is sup...
Definition: Error.hh:82
fastjet::InternalError
class corresponding to critical internal errors
Definition: Error.hh:125
fastjet::LimitedWarning
class to provide facilities for giving warnings up to some maximum number of times and to provide glo...
Definition: LimitedWarning.hh:54
fastjet::Error
base class corresponding to errors that can be thrown by FastJet
Definition: Error.hh:51