FastJet  3.4.0
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 #include <mutex>
45 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
46 
47 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
48 
49 /// @ingroup error_handling
50 /// \class Error
51 /// base class corresponding to errors that can be thrown by FastJet
52 class Error{
53 public:
54  /// default constructors
55  Error() {}
56 
57  /// ctor from an error message
58  /// \param message to be printed
59  /// Note: in addition to the error message, one can choose to print the
60  /// backtrace (showing the last few calls before the error) by
61  /// using set_print_backtrace(true). The default is "false".
62  Error(const std::string & message);
63 
64  /// virtual dummy dtor
65  virtual ~Error() {}
66 
67  /// the error message
68  std::string message() const {return _message;}
69 
70  /// an alternative access to the error message (more standard)
71  std::string description() const {return message();}
72 
73  /// controls whether the error message (and the backtrace, if its printing is enabled)
74  /// is printed out or not
75  static void set_print_errors(bool print_errors) {_print_errors = print_errors;}
76 
77  /// controls whether the backtrace is printed out with the error message or not.
78  /// The default is "false".
79  static void set_print_backtrace(bool enabled);
80 
81  /// sets the default output stream for all errors; by default
82  /// cerr; if it's null then error output is suppressed.
83  static void set_default_stream(std::ostream * ostr) {
84  _default_ostr = ostr;
85  }
86 
87 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
88  /// sets the default output stream for all errors (by default
89  /// cerr; passing a null pointer prevents errors from being output)
90  /// The second argument is a mutex that would be used to guarantee
91  /// that only a single thread writes to the stream at a time
92  static void set_default_stream_and_mutex(std::ostream * ostr, std::mutex * stream_mutex) {
93  _default_ostr = ostr;
94  _stream_mutex = stream_mutex;
95  }
96 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
97 
98 private:
99 
100 #ifndef __FJCORE__
101 #if defined(FASTJET_HAVE_EXECINFO_H) && defined(FASTJET_HAVE_DEMANGLING_SUPPORT)
102  /// demangle a given backtrace symbol
103  std::string _demangle(const char* symbol);
104 #endif
105 #endif
106 
107  std::string _message; ///< error message
108 
109 #ifdef FASTJET_HAVE_LIMITED_THREAD_SAFETY
110  static std::atomic<bool> _print_errors; ///< do we print anything?
111  static std::atomic<bool> _print_backtrace; ///< do we print the backtrace?
112  static std::atomic<std::ostream *> _default_ostr; ///< the output stream (cerr if not set)
113  static std::atomic<std::mutex *> _stream_mutex; ///< the mutex for the output stream (nullptr if not set)
114 #else
115  static bool _print_errors; ///< do we print anything?
116  static bool _print_backtrace; ///< do we print the backtrace?
117  static std::ostream * _default_ostr; ///< the output stream (cerr if not set)
118 #endif // FASTJET_HAVE_LIMITED_THREAD_SAFETY
119 
120 
121 #if (!defined(FASTJET_HAVE_EXECINFO_H)) || defined(__FJCORE__)
122  static LimitedWarning _execinfo_undefined;
123 #endif
124 };
125 
126 
127 /// @ingroup error_handling
128 /// \class InternalError
129 /// class corresponding to critical internal errors
130 ///
131 /// This is an error class (derived from Error) meant for serious,
132 /// critical, internal errors that we still want to be catchable by an
133 /// end-user [e.g. a serious issue in clustering where the end-user
134 /// can catch it and retry with a different strategy]
135 ///
136 /// Please directly contact the FastJet authors if you see such an
137 /// error.
138 class InternalError : public Error{
139 public:
140  /// ctor with error message:
141  /// just add a bit of info to the message and pass it to the base class
142  InternalError(const std::string & message_in) : Error(std::string("*** CRITICAL INTERNAL FASTJET ERROR *** CONTACT THE AUTHORS *** ") + message_in){ }
143 };
144 
145 FASTJET_END_NAMESPACE
146 
147 #endif // __FASTJET_ERROR_HH__
base class corresponding to errors that can be thrown by FastJet
Definition: Error.hh:52
virtual ~Error()
virtual dummy dtor
Definition: Error.hh:65
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:75
Error()
default constructors
Definition: Error.hh:55
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:83
std::string message() const
the error message
Definition: Error.hh:68
std::string description() const
an alternative access to the error message (more standard)
Definition: Error.hh:71
class corresponding to critical internal errors
Definition: Error.hh:138
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:142