Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <exception>
8#include <cassert>
9#include "log.h" // unused. for convenience.
10
11#ifdef _MSC_VER
12 #define FUNCTION_NAME __FUNCTION__
13#else
14 #define FUNCTION_NAME __PRETTY_FUNCTION__
15#endif
16
17// Whether to catch all std::exceptions.
18// When "catching", the program will abort with an error message.
19// In debug mode, leave these for the debugger and don't catch them.
20#ifdef NDEBUG
21 #define CATCH_UNHANDLED_EXCEPTIONS 1
22#else
23 #define CATCH_UNHANDLED_EXCEPTIONS 0
24#endif
25
26/* Abort program execution immediately
27 */
28[[noreturn]] extern void fatal_error_fn(
29 const char *msg, const char *file,
30 unsigned int line, const char *function);
31
32#define FATAL_ERROR(msg) \
33 fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
34
35#define FATAL_ERROR_IF(expr, msg) \
36 ((expr) \
37 ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
38 : (void)(0))
39
40/*
41 sanity_check()
42 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
43 defined)
44*/
45
46[[noreturn]] extern void sanity_check_fn(
47 const char *assertion, const char *file,
48 unsigned int line, const char *function);
49
50#define SANITY_CHECK(expr) \
51 ((expr) \
52 ? (void)(0) \
53 : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
54
55#define sanity_check(expr) SANITY_CHECK(expr)
56
57std::string debug_describe_exc(const std::exception &e);
58
60
61/*
62 These should be put into every thread
63*/
64
65#if CATCH_UNHANDLED_EXCEPTIONS == 1
66 #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
67 #define END_DEBUG_EXCEPTION_HANDLER \
68 } catch (std::exception &e) { \
69 std::string e_descr = debug_describe_exc(e); \
70 errorstream << "An unhandled exception occurred: " \
71 << e_descr << std::endl; \
72 FATAL_ERROR(e_descr.c_str()); \
73 }
74#else
75 // Dummy ones
76 #define BEGIN_DEBUG_EXCEPTION_HANDLER
77 #define END_DEBUG_EXCEPTION_HANDLER
78#endif
std::string debug_describe_exc(const std::exception &e)
Definition debug.cpp:64
void debug_set_exception_handler()
Definition debug.cpp:185
void sanity_check_fn(const char *assertion, const char *file, unsigned int line, const char *function)
Definition debug.cpp:34
void fatal_error_fn(const char *msg, const char *file, unsigned int line, const char *function)
Definition debug.cpp:49