]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.git] / src / debug.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <iostream>
23 #include <exception>
24 #include <cassert>
25 #include "gettime.h"
26 #include "log.h"
27
28 #ifdef _WIN32
29         #ifndef _WIN32_WINNT
30                 #define _WIN32_WINNT 0x0501
31         #endif
32         #include <windows.h>
33         #ifdef _MSC_VER
34                 #include <eh.h>
35         #endif
36         #define NORETURN __declspec(noreturn)
37         #define FUNCTION_NAME __FUNCTION__
38 #else
39         #define NORETURN __attribute__ ((__noreturn__))
40         #define FUNCTION_NAME __PRETTY_FUNCTION__
41 #endif
42
43 // Whether to catch all std::exceptions.
44 // When "catching", the program will abort with an error message.
45 // In debug mode, leave these for the debugger and don't catch them.
46 #ifdef NDEBUG
47         #define CATCH_UNHANDLED_EXCEPTIONS 1
48 #else
49         #define CATCH_UNHANDLED_EXCEPTIONS 0
50 #endif
51
52 /* Abort program execution immediately
53  */
54 NORETURN extern void fatal_error_fn(
55                 const char *msg, const char *file,
56                 unsigned int line, const char *function);
57
58 #define FATAL_ERROR(msg) \
59         fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
60
61 #define FATAL_ERROR_IF(expr, msg) \
62         ((expr) \
63         ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
64         : (void)(0))
65
66 /*
67         sanity_check()
68         Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
69         defined)
70 */
71
72 NORETURN extern void sanity_check_fn(
73                 const char *assertion, const char *file,
74                 unsigned int line, const char *function);
75
76 #define SANITY_CHECK(expr) \
77         ((expr) \
78         ? (void)(0) \
79         : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
80
81 #define sanity_check(expr) SANITY_CHECK(expr)
82
83
84 void debug_set_exception_handler();
85
86 /*
87         These should be put into every thread
88 */
89
90 #if CATCH_UNHANDLED_EXCEPTIONS == 1
91         #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
92         #define END_DEBUG_EXCEPTION_HANDLER                        \
93                 } catch (std::exception &e) {                          \
94                         errorstream << "An unhandled exception occurred: " \
95                                 << e.what() << std::endl;                      \
96                         FATAL_ERROR(e.what());                             \
97                 }
98 #else
99         // Dummy ones
100         #define BEGIN_DEBUG_EXCEPTION_HANDLER
101         #define END_DEBUG_EXCEPTION_HANDLER
102 #endif