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#ifdef __ANDROID__
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
30namespace es
31{
32	// Outputs text to the debugging log
33	void trace(const char *format, ...);
34}
35
36// A macro to output a trace of a function call and its arguments to the debugging log
37#if defined(ANGLE_DISABLE_TRACE)
38#define TRACE(message, ...) (void(0))
39#else
40#define TRACE(message, ...) es::trace("trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
41#endif
42
43// A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
44#if defined(ANGLE_DISABLE_TRACE)
45#define FIXME(message, ...) (void(0))
46#else
47#define FIXME(message, ...) do {es::trace("fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
48#endif
49
50// A macro to output a function call and its arguments to the debugging log, in case of error.
51#if defined(ANGLE_DISABLE_TRACE)
52#define ERR(message, ...) (void(0))
53#else
54#define ERR(message, ...) do {es::trace("err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
55#endif
56
57// A macro asserting a condition and outputting failures to the debug log
58#undef ASSERT
59#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
60#define ASSERT(expression) do { \
61	if(!(expression)) { \
62		ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
63		assert(expression); \
64	} } while(0)
65#else
66#define ASSERT(expression) (void(0))
67#endif
68
69// A macro to indicate unimplemented functionality
70#undef UNIMPLEMENTED
71#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
72#define UNIMPLEMENTED() do { \
73	FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \
74	assert(false); \
75	} while(0)
76#else
77	#define UNIMPLEMENTED() FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
78#endif
79
80// A macro for code which is not expected to be reached under valid assumptions
81#undef UNREACHABLE
82#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
83#define UNREACHABLE(value) do { \
84	ERR("\t! Unreachable case reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value); \
85	assert(false); \
86	} while(0)
87#else
88	#define UNREACHABLE(value) ERR("\t! Unreachable reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value)
89#endif
90
91#endif   // !__ANDROID__
92
93// A macro asserting a condition and outputting failures to the debug log, or return when in release mode.
94#undef ASSERT_OR_RETURN
95#define ASSERT_OR_RETURN(expression) do { \
96	if(!(expression)) { \
97		ASSERT(expression); \
98		return; \
99	} } while(0)
100
101#endif   // COMMON_DEBUG_H_
102