Mir
fatal.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 or 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * ---
17  * Fatal error handling - Fatal errors are situations we don't expect to ever
18  * happen and don't have logic to gracefully recover from. The most useful
19  * thing you can do in that situation is abort to get a clean core file and
20  * stack trace to maximize the chances of it being readable.
21  */
22 
23 #ifndef MIR_FATAL_H_
24 #define MIR_FATAL_H_
25 
26 namespace mir
27 {
28 /**
29  * fatal_error() is strictly for "this should never happen" situations that
30  * you cannot recover from. By default it points at fatal_error_abort().
31  * Note the reason parameter is a simple char* so its value is clearly visible
32  * in stack trace output.
33  * \remark There is no attempt to make this thread-safe, if it needs to be changed
34  * that should be done before spinning up the Mir server.
35  * \param [in] reason A printf-style format string.
36  */
37 extern void (*fatal_error)(char const* reason, ...);
38 
39 /**
40  * Throws an exception that will typically kill the Mir server and propagate from
41  * mir::run_mir.
42  * \param [in] reason A printf-style format string.
43  */
44 void fatal_error_except(char const* reason, ...);
45 
46 /**
47  * An alternative to fatal_error_except() that kills the program and dump core
48  * as cleanly as possible.
49  * \param [in] reason A printf-style format string.
50  */
51 [[noreturn]]
52 void fatal_error_abort(char const* reason, ...);
53 
54 // Utility class to override & restore existing error handler
56 {
57 public:
58  explicit FatalErrorStrategy(void (*fatal_error_handler)(char const* reason, ...)) :
59  old_fatal_error_handler(fatal_error)
60  {
61  fatal_error = fatal_error_handler;
62  }
63 
65  {
66  fatal_error = old_fatal_error_handler;
67  }
68 
69 private:
70  void (*old_fatal_error_handler)(char const* reason, ...);
71  FatalErrorStrategy(FatalErrorStrategy const&) = delete;
72  FatalErrorStrategy& operator=(FatalErrorStrategy const&) = delete;
73 };
74 } // namespace mir
75 
76 #endif // MIR_FATAL_H_

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Sep 8 12:37:23 UTC 2022
This documentation is licensed under the GPL version 2 or 3.