1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // debug.h: Debugging utilities.
16 
17 #ifndef COMMON_DEBUG_H_
18 #define COMMON_DEBUG_H_
19 
20 #if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD)
21 #include "../../Common/DebugAndroid.hpp"
22 #else
23 #include <stdio.h>
24 #include <assert.h>
25 
26 #if !defined(TRACE_OUTPUT_FILE)
27 #define TRACE_OUTPUT_FILE "debug.txt"
28 #endif
29 
30 namespace es
31 {
32 // Outputs text to the debugging log
33 void trace(const char *format, ...);
trace()34 inline void trace() {}
35 }
36 
37 // A macro to output a trace of a function call and its arguments to the debugging log
38 #if defined(ANGLE_DISABLE_TRACE)
39 #define TRACE(message, ...) (void(0))
40 #else
41 #define TRACE(message, ...) es::trace("trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
42 #endif
43 
44 // A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
45 #if defined(ANGLE_DISABLE_TRACE)
46 #define FIXME(message, ...) (void(0))
47 #else
48 #define FIXME(message, ...) do {es::trace("fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
49 #endif
50 
51 // A macro to output a function call and its arguments to the debugging log, in case of error.
52 #if defined(ANGLE_DISABLE_TRACE)
53 #define ERR(message, ...) (void(0))
54 #else
55 #define ERR(message, ...) do {es::trace("err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
56 #endif
57 
58 // A macro asserting a condition and outputting failures to the debug log
59 #undef ASSERT
60 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
61 #define ASSERT(expression) do { \
62 	if(!(expression)) { \
63 		ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
64 		assert(expression); \
65 	} } while(0)
66 #else
67 #define ASSERT(expression) (void(0))
68 #endif
69 
70 // A macro to indicate unimplemented functionality
71 #undef UNIMPLEMENTED
72 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
73 #define UNIMPLEMENTED(...) do { \
74 	es::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
75 	es::trace(__VA_ARGS__); \
76 	es::trace("\n"); \
77 	assert(false); \
78 	} while(0)
79 #else
80 	#define UNIMPLEMENTED(...) FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
81 #endif
82 
83 // A macro for code which is not expected to be reached under valid assumptions
84 #undef UNREACHABLE
85 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
86 #define UNREACHABLE(value) do { \
87 	ERR("\t! Unreachable case reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value); \
88 	assert(false); \
89 	} while(0)
90 #else
91 	#define UNREACHABLE(value) ERR("\t! Unreachable reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value)
92 #endif
93 
94 #endif   // !__ANDROID__
95 
96 // A macro asserting a condition and outputting failures to the debug log, or return when in release mode.
97 #undef ASSERT_OR_RETURN
98 #define ASSERT_OR_RETURN(expression) do { \
99 	if(!(expression)) { \
100 		ASSERT(expression); \
101 		return; \
102 	} } while(0)
103 
104 #endif   // COMMON_DEBUG_H_
105