1/*
2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 *  ccdebug.h - CommonCrypto debug macros
26 *
27 */
28
29#if defined(COMMON_DIGEST_FUNCTIONS) || defined(COMMON_CMAC_FUNCTIONS) || defined(COMMON_GCM_FUNCTIONS) \
30    || defined (COMMON_AESSHOEFLY_FUNCTIONS) || defined(COMMON_CASTSHOEFLY_FUNCTIONS) \
31    || defined (COMMON_CRYPTOR_FUNCTIONS) || defined(COMMON_HMAC_FUNCTIONS) \
32    || defined(COMMON_KEYDERIVATION_FUNCTIONS) || defined(COMMON_SYMMETRIC_KEYWRAP_FUNCTIONS) \
33    || defined(COMMON_RSA_FUNCTIONS) || defined(COMMON_EC_FUNCTIONS) || defined(COMMON_DH_FUNCTIONS) \
34    || defined(COMMON_BIGNUM_FUNCTIONS) || defined(COMMON_RANDOM_FUNCTIONS)
35
36#define DIAGNOSTIC
37#endif
38
39#ifdef KERNEL
40#include <stdarg.h>
41
42#define	CC_DEBUG		 1
43#define	CC_DEBUG_BUG		2
44#define	CC_DEBUG_FAILURE	3
45
46#if DIAGNOSTIC
47#define CC_DEBUG_LOG(lvl, fmt, ...) do {				      \
48	const char *lvl_type[] = { "INVALID", "DEBUG", "ERROR", "FAILURE" };  \
49	char fmtbuffer[256]; 						      \
50	int l = lvl;							      \
51									      \
52	if (l < 0 || l > 3) l = 0;					      \
53	snprintf(fmtbuffer, sizeof(fmtbuffer),				      \
54	    "CommonCrypto Function: %s:%d (%s) - %s", __FILE__, __LINE__,     \
55	    lvl_type[l], fmt);					              \
56	printf(fmtbuffer, __VA_ARGS__);		      			      \
57} while (0)
58
59#else
60
61#define CC_DEBUG_LOG(lvl,fmt,...) {}
62
63#endif /* DIAGNOSTIC */
64
65#else
66
67#include <asl.h>
68#include <stdarg.h>
69
70#define CC_DEBUG_FAILURE		ASL_LEVEL_EMERG
71#define CC_DEBUG_BUG			ASL_LEVEL_ERR
72#define CC_DEBUG			ASL_LEVEL_ERR
73
74void ccdebug_imp(int level, char *funcname, const char *format, ...);
75
76#ifdef DIAGNOSTIC
77
78#define CC_DEBUG_LOG(lvl,...) ccdebug_imp(lvl, __FUNCTION__, __VA_ARGS__)
79
80#else
81
82#define CC_DEBUG_LOG(lvl,...) {}
83#endif /* DEBUG */
84
85#endif /* KERNEL */
86
87