blob: c12fb851ca1c84d2a458b29a25409e0237f95ede [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// debug.h: Debugging utilities.
8
9#ifndef COMMON_DEBUG_H_
10#define COMMON_DEBUG_H_
11
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012#include <stdio.h>
13#include <assert.h>
14
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000015#include "common/angleutils.h"
16
17#if !defined(TRACE_OUTPUT_FILE)
18#define TRACE_OUTPUT_FILE "debug.txt"
19#endif
20
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021namespace gl
22{
23 // Outputs text to the debugging log
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000024 void trace(bool traceFileDebugOnly, const char *format, ...);
25
26 // Returns whether D3DPERF is active.
27 bool perfActive();
28
29 // Pairs a D3D begin event with an end event.
30 class ScopedPerfEventHelper
31 {
32 public:
33 ScopedPerfEventHelper(const char* format, ...);
34 ~ScopedPerfEventHelper();
35
36 private:
37 DISALLOW_COPY_AND_ASSIGN(ScopedPerfEventHelper);
38 };
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039}
40
41// A macro to output a trace of a function call and its arguments to the debugging log
Geoff Langf5713122013-10-07 17:06:30 -040042#if defined(ANGLE_ENABLE_TRACE) || defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000043#define TRACE(message, ...) gl::trace(true, "trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040044#else
45#define TRACE(message, ...) (void(0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046#endif
47
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000048// A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
Geoff Langf5713122013-10-07 17:06:30 -040049#if defined(ANGLE_ENABLE_TRACE) || defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000050#define FIXME(message, ...) gl::trace(false, "fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040051#else
52#define FIXME(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000053#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000055// A macro to output a function call and its arguments to the debugging log, in case of error.
Geoff Langf5713122013-10-07 17:06:30 -040056#if defined(ANGLE_ENABLE_TRACE) || defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000057#define ERR(message, ...) gl::trace(false, "err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040058#else
59#define ERR(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000060#endif
61
62// A macro to log a performance event around a scope.
Geoff Langf5713122013-10-07 17:06:30 -040063#if defined(ANGLE_ENABLE_TRACE) || defined(ANGLE_ENABLE_PERF)
64#if defined(_MSC_VER)
daniel@transgaming.com3d8115f2011-03-09 15:07:33 +000065#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper ## __LINE__(__FUNCTION__ message "\n", __VA_ARGS__);
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000066#else
67#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper(message "\n", ##__VA_ARGS__);
Geoff Langf5713122013-10-07 17:06:30 -040068#endif // _MSC_VER
69#else
70#define EVENT(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000071#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072
73// A macro asserting a condition and outputting failures to the debug log
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000074#if !defined(NDEBUG)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075#define ASSERT(expression) do { \
76 if(!(expression)) \
77 ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000078 assert(expression); \
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079 } while(0)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000080#else
81#define ASSERT(expression) (void(0))
82#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
84// A macro to indicate unimplemented functionality
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +000085
86// Define NOASSERT_UNIMPLEMENTED to non zero to skip the assert fail in the unimplemented checks
87// This will allow us to test with some automated test suites (eg dEQP) without crashing
88#ifndef NOASSERT_UNIMPLEMENTED
89#define NOASSERT_UNIMPLEMENTED 0
90#endif
91
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000092#if !defined(NDEBUG)
93#define UNIMPLEMENTED() do { \
94 FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +000095 assert(NOASSERT_UNIMPLEMENTED); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000096 } while(0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097#else
98 #define UNIMPLEMENTED() FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
99#endif
100
101// A macro for code which is not expected to be reached under valid assumptions
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000102#if !defined(NDEBUG)
103#define UNREACHABLE() do { \
104 ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \
105 assert(false); \
106 } while(0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107#else
108 #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
109#endif
110
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000111// A macro that determines whether an object has a given runtime type.
Geoff Lang8690da82013-10-08 10:57:09 -0400112#if !defined(NDEBUG) && (!defined(_MSC_VER) || defined(_CPPRTTI)) && (!defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || defined(__GXX_RTTI))
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000113#define HAS_DYNAMIC_TYPE(type, obj) (dynamic_cast<type >(obj) != NULL)
114#else
115#define HAS_DYNAMIC_TYPE(type, obj) true
116#endif
117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118// A macro functioning as a compile-time assert to validate constant conditions
Geoff Langd0f8e822013-09-30 15:21:53 -0400119#if defined(_MSC_VER) && _MSC_VER >= 1600
120#define META_ASSERT_MSG(condition, msg) static_assert(condition, msg)
121#else
Jamie Madill4f267862014-04-17 15:53:37 -0400122#define META_ASSERT_CONCAT(a, b) a ## b
123#define META_ASSERT_CONCAT2(a, b) META_ASSERT_CONCAT(a, b)
124#define META_ASSERT_MSG(condition, msg) typedef int META_ASSERT_CONCAT2(COMPILE_TIME_ASSERT_, __LINE__)[static_cast<bool>(condition)?1:-1]
Geoff Langd0f8e822013-09-30 15:21:53 -0400125#endif
126#define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
128#endif // COMMON_DEBUG_H_