1/*
2 * Copyright (c) 2000-2001,2011,2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19/*
20	File:		cspdebugging.h
21
22	Contains:	Debugging macros.
23
24
25	Copyright (c) 1998,2011,2013-2014 Apple Inc. All Rights Reserved.
26
27	Change History (most recent first):
28
29		06/02/98	dpm		Added DEBUG_THREAD_YIELD.
30		03/10/98	dpm		Created.
31
32*/
33
34#ifndef	_CSPDEBUGGING_H_
35#define _CSPDEBUGGING_H_
36
37#ifdef	NDEBUG
38#define DEBUG_ENABLE		0
39#define ERROR_LOG_ENABLE	0
40#else
41#define DEBUG_ENABLE		1
42#define ERROR_LOG_ENABLE	1
43#endif
44
45/* any other way? */
46#define LOG_VIA_PRINTF		1
47
48#if		DEBUG_ENABLE || ERROR_LOG_ENABLE
49
50#include <stdio.h>
51#include <stdlib.h>
52
53#if		!LOG_VIA_PRINTF
54
55#error Hey, figure out a debug mechanism
56
57#include <string.h>
58#include <TextUtils.h>
59
60/* common log macros */
61
62/* remaining ones can take constant strings */
63
64#ifdef	__cplusplus
65extern "C" {
66#endif
67
68extern void dblog0(char *str);
69extern void dblog1(char *str, void * arg1);
70extern void dblog2(char *str, void * arg1, void * arg2);
71extern void dblog3(char *str, void * arg1, void * arg2, void * arg3);
72extern void dblog4(char *str, void * arg1, void * arg2, void * arg3, void * arg4);
73
74#ifdef	__cplusplus
75}
76#endif
77
78
79#else	/* LOG_VIA_PRINTF */
80
81#define dblog0(str)								printf(str)
82#define dblog1(str, arg1)						printf(str, arg1)
83#define dblog2(str, arg1, arg2)					printf(str, arg1, arg2)
84#define dblog3(str, arg1, arg2, arg3)			printf(str, arg1, arg2, arg3)
85#define dblog4(str, arg1, arg2, arg3, arg4)		printf(str, arg1, arg2, arg3, arg4)
86
87#endif	/* LOG_VIA_PRINTF */
88
89#else	/* log macros disabled */
90
91#define dblog0(str)
92#define dblog1(str, arg1)
93#define dblog2(str, arg1, arg2)
94#define dblog3(str, arg1, arg2, arg3)
95#define dblog4(str, arg1, arg2, arg3, arg4)
96
97#endif	/* DEBUG_ENABLE || ERROR_LOG_ENABLE */
98
99#if	DEBUG_ENABLE
100
101#define dprintf0(str)								dblog0(str)
102#define dprintf1(str, arg1)							dblog1(str, arg1)
103#define dprintf2(str, arg1, arg2)					dblog2(str, arg1, arg2)
104#define dprintf3(str, arg1, arg2, arg3)				dblog3(str, arg1, arg2, arg3)
105#define dprintf4(str, arg1, arg2, arg3, arg4)		dblog4(str, arg1, arg2, arg3,  arg4)
106
107#ifdef	__cplusplus
108extern "C" {
109#endif
110
111static inline void _panic(const char *str)
112{
113	printf("%s\n", str);
114	exit(1);
115}
116
117#ifdef	__cplusplus
118}
119#endif
120
121#define CASSERT(expression) 							\
122  ((expression) ? (void)0 : 							\
123   (dprintf1 ("Assertion failed: " #expression 			\
124      ", file " __FILE__ ", line %d.\n", __LINE__), 	\
125    _panic("Assertion Failure")))
126
127#else	/* DEBUG_ENABLE */
128
129#define dprintf0(str)
130#define dprintf1(str, arg1)
131#define dprintf2(str, arg1, arg2)
132#define dprintf3(str, arg1, arg2, arg3)
133#define dprintf4(str, arg1, arg2, arg3, arg4)
134
135#define CASSERT(expression)
136
137#endif	/* DEBUG_ENABLE */
138
139/*
140 * Error logging. This may well be platform dependent.
141 */
142#if		ERROR_LOG_ENABLE
143#define errorLog0(str)								dblog0(str);
144#define errorLog1(str, arg1)						dblog1(str, arg1)
145#define errorLog2(str, arg1, arg2)					dblog2(str, arg1, arg2)
146#define errorLog3(str, arg1, arg2, arg3)			dblog3(str, arg1, arg2, arg3)
147#define errorLog4(str, arg1, arg2, arg3, arg4)		dblog4(str, arg1, arg2, arg3, arg4)
148
149#else	/* ERROR_LOG_ENABLE */
150
151#define errorLog0(str)
152#define errorLog1(str, arg1)
153#define errorLog2(str, arg1, arg2)
154#define errorLog3(str, arg1, arg2, arg3)
155#define errorLog4(str, arg1, arg2, arg3, arg4)
156
157#endif	/* ERROR_LOG_ENABLE */
158
159#endif	/* _CSPDEBUGGING_H_ */
160