aboutsummaryrefslogtreecommitdiff
path: root/src/debug.h
blob: ba2e8704ebd1cc7c892dcec67618c6009f75035a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef DEBUG_HEADER
#define DEBUG_HEADER

#include <iostream>
#include <exception>
#include "gettime.h"

#if (defined(WIN32) || defined(_WIN32_WCE))
	#define WIN32_LEAN_AND_MEAN
	#ifndef _WIN32_WINNT
		#define _WIN32_WINNT 0x0501
	#endif
	#include <windows.h>
	#ifdef _MSC_VER
		#include <eh.h>
	#endif
	#define __NORETURN __declspec(noreturn)
	#define __FUNCTION_NAME __FUNCTION__
#else
	#define __NORETURN __attribute__ ((__noreturn__))
	#define __FUNCTION_NAME __PRETTY_FUNCTION__
#endif

// Whether to catch all std::exceptions.
// Assert will be called on such an event.
// In debug mode, leave these for the debugger and don't catch them.
#ifdef NDEBUG
	#define CATCH_UNHANDLED_EXCEPTIONS 1
#else
	#define CATCH_UNHANDLED_EXCEPTIONS 0
#endif

/*
	Debug output
*/

#define DTIME (getTimestamp()+": ")

extern void debugstreams_init(bool disable_stderr, const char *filename);
extern void debugstreams_deinit();

// This is used to redirect output to /dev/null
class Nullstream : public std::ostream {
public:
	Nullstream():
		std::ostream(0)
	{
	}
private:
};

extern std::ostream dstream;
extern std::ostream dstream_no_stderr;
extern Nullstream dummyout;

/*
	Include assert.h and immediately undef assert so that it can't override
	our assert later on. leveldb/slice.h is a notable offender.
*/

#include <assert.h>
#undef assert

/*
	Assert
*/

__NORETURN extern void assert_fail(
		const char *assertion, const char *file,
		unsigned int line, const char *function);

#define ASSERT(expr)\
	((expr)\
	? (void)(0)\
	: assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))

#define assert(expr) ASSERT(expr)

/*
	DebugStack
*/

#define DEBUG_STACK_SIZE 50
#define DEBUG_STACK_TEXT_SIZE 300

extern void debug_stacks_init();
extern void debug_stacks_print_to(std::ostream &os);
extern void debug_stacks_print();

struct DebugStack;
class DebugStacker
{
public:
	DebugStacker(const char *text);
	~DebugStacker();

private:
	DebugStack *m_stack;
	bool m_overflowed;
};

#define DSTACK(msg)\
	DebugStacker __debug_stacker(msg);

#define DSTACKF(...)\
	char __buf[DEBUG_STACK_TEXT_SIZE];\
	snprintf(__buf,\
			DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
	DebugStacker __debug_stacker(__buf);

/*
	These should be put into every thread
*/

#if CATCH_UNHANDLED_EXCEPTIONS == 1
	#define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
	#define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
		}catch(std::exception &e){\
			logstream<<"ERROR: An unhandled exception occurred: "\
					<<e.what()<<std::endl;\
			assert(0);\
		}
	#ifdef _WIN32 // Windows
		#ifdef _MSC_VER // MSVC
void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
			#define BEGIN_DEBUG_EXCEPTION_HANDLER \
				BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
				_set_se_translator(se_trans_func);

			#define END_DEBUG_EXCEPTION_HANDLER(logstream) \
				END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
		#else // Probably mingw
			#define BEGIN_DEBUG_EXCEPTION_HANDLER\
				BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
			#define END_DEBUG_EXCEPTION_HANDLER(logstream)\
				END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
		#endif
	#else // Posix
		#define BEGIN_DEBUG_EXCEPTION_HANDLER\
			BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
		#define END_DEBUG_EXCEPTION_HANDLER(logstream)\
			END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
	#endif
#else
	// Dummy ones
	#define BEGIN_DEBUG_EXCEPTION_HANDLER
	#define END_DEBUG_EXCEPTION_HANDLER(logstream)
#endif

#endif // DEBUG_HEADER


light or nothing in here. */ u8 param1; /* The second parameter. Initialized to 0. E.g. direction for torches and flowing water. */ u8 param2; MapNode() { } MapNode(const MapNode & n) { *this = n; } MapNode(content_t content, u8 a_param1=0, u8 a_param2=0) : param0(content), param1(a_param1), param2(a_param2) { } // Create directly from a nodename // If name is unknown, sets CONTENT_IGNORE MapNode(INodeDefManager *ndef, const std::string &name, u8 a_param1=0, u8 a_param2=0); bool operator==(const MapNode &other) { return (param0 == other.param0 && param1 == other.param1 && param2 == other.param2); } // To be used everywhere content_t getContent() const { return param0; } void setContent(content_t c) { param0 = c; } u8 getParam1() const { return param1; } void setParam1(u8 p) { param1 = p; } u8 getParam2() const { return param2; } void setParam2(u8 p) { param2 = p; } void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr); /** * Check if the light value for night differs from the light value for day. * * @return If the light values are equal, returns true; otherwise false */ bool isLightDayNightEq(INodeDefManager *nodemgr) const; u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const; /** * This function differs from getLight(enum LightBank bank, INodeDefManager *nodemgr) * in that the ContentFeatures of the node in question are not retrieved by * the function itself. Thus, if you have already called nodemgr->get() to * get the ContentFeatures you pass it to this function instead of the * function getting ContentFeatures itself. Since INodeDefManager::get() * is relatively expensive this can lead to significant performance * improvements in some situations. Call this function if (and only if) * you have already retrieved the ContentFeatures by calling * INodeDefManager::get() for the node you're working with and the * pre-conditions listed are true. * * @pre f != NULL * @pre f->param_type == CPT_LIGHT */ u8 getLightNoChecks(LightBank bank, const ContentFeatures *f) const; bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const; // 0 <= daylight_factor <= 1000 // 0 <= return value <= LIGHT_SUN u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr) const { u8 lightday = 0; u8 lightnight = 0; getLightBanks(lightday, lightnight, nodemgr); return blend_light(daylight_factor, lightday, lightnight); } u8 getFaceDir(INodeDefManager *nodemgr) const; u8 getWallMounted(INodeDefManager *nodemgr) const; v3s16 getWallMountedDir(INodeDefManager *nodemgr) const; void rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot); /* Gets list of node boxes (used for rendering (NDT_NODEBOX)) */ std::vector<aabb3f> getNodeBoxes(INodeDefManager *nodemgr) const; /* Gets list of selection boxes */ std::vector<aabb3f> getSelectionBoxes(INodeDefManager *nodemgr) const; /* Gets list of collision boxes */