Luanti 5.10.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 "gettime.h"
10#include "log.h"
11
12#ifdef _MSC_VER
13 #define FUNCTION_NAME __FUNCTION__
14#else
15 #define FUNCTION_NAME __PRETTY_FUNCTION__
16#endif
17
18// Whether to catch all std::exceptions.
19// When "catching", the program will abort with an error message.
20// In debug mode, leave these for the debugger and don't catch them.
21#ifdef NDEBUG
22 #define CATCH_UNHANDLED_EXCEPTIONS 1
23#else
24 #define CATCH_UNHANDLED_EXCEPTIONS 0
25#endif
26
27/* Abort program execution immediately
28 */
29[[noreturn]] extern void fatal_error_fn(
30 const char *msg, const char *file,
31 unsigned int line, const char *function);
32
33#define FATAL_ERROR(msg) \
34 fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
35
36#define FATAL_ERROR_IF(expr, msg) \
37 ((expr) \
38 ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
39 : (void)(0))
40
41/*
42 sanity_check()
43 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
44 defined)
45*/
46
47[[noreturn]] extern void sanity_check_fn(
48 const char *assertion, const char *file,
49 unsigned int line, const char *function);
50
51#define SANITY_CHECK(expr) \
52 ((expr) \
53 ? (void)(0) \
54 : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
55
56#define sanity_check(expr) SANITY_CHECK(expr)
57
58std::string debug_describe_exc(const std::exception &e);
59
61
62/*
63 These should be put into every thread
64*/
65
66#if CATCH_UNHANDLED_EXCEPTIONS == 1
67 #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
68 #define END_DEBUG_EXCEPTION_HANDLER \
69 } catch (std::exception &e) { \
70 std::string e_descr = debug_describe_exc(e); \
71 errorstream << "An unhandled exception occurred: " \
72 << e_descr << std::endl; \
73 FATAL_ERROR(e_descr.c_str()); \
74 }
75#else
76 // Dummy ones
77 #define BEGIN_DEBUG_EXCEPTION_HANDLER
78 #define END_DEBUG_EXCEPTION_HANDLER
79#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