cleanup
[waspsaliva.git] / src / debug.h
blob1faeece8db56a06eee10b9f8d3547bfc25a6a1c4
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #pragma once
22 #include <iostream>
23 #include <exception>
24 #include <cassert>
25 #include "gettime.h"
26 #include "log.h"
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
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
52 /* Abort program execution immediately
54 NORETURN extern void fatal_error_fn(
55 const char *msg, const char *file,
56 unsigned int line, const char *function);
58 #define FATAL_ERROR(msg) \
59 fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
61 #define FATAL_ERROR_IF(expr, msg) \
62 ((expr) \
63 ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
64 : (void)(0))
67 sanity_check()
68 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
69 defined)
72 NORETURN extern void sanity_check_fn(
73 const char *assertion, const char *file,
74 unsigned int line, const char *function);
76 #define SANITY_CHECK(expr) \
77 ((expr) \
78 ? (void)(0) \
79 : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
81 #define sanity_check(expr) SANITY_CHECK(expr)
84 void debug_set_exception_handler();
87 These should be put into every thread
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()); \
98 #else
99 // Dummy ones
100 #define BEGIN_DEBUG_EXCEPTION_HANDLER
101 #define END_DEBUG_EXCEPTION_HANDLER
102 #endif