1/*
2 * Copyright (c) 2004 Apple Computer, 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.c - CommonCrypto debug macros
26 *
27 */
28
29
30#include "ccdebug.h"
31#include "ccGlobals.h"
32#include <stdlib.h>
33#include <asl.h>
34#include <stdarg.h>
35#include <string.h>
36#include <stdio.h>
37#include <dispatch/dispatch.h>
38
39
40static const char *std_log_prefix = "###CommonCrypto : %s - %s";
41static const char *std_ident = "CommonCrypto";
42static const char *std_facility = "CipherSuite";
43
44static void
45ccdebug_init(cc_globals_t globals) {
46    dispatch_once(&globals->debug_init, ^{
47        char *ccEnvStdErr = getenv("CC_STDERR");
48        uint32_t std_options = 0;
49
50        if(ccEnvStdErr != NULL && strncmp(ccEnvStdErr, "yes", 3) == 0) std_options |= ASL_OPT_STDERR;
51        globals->aslhandle = asl_open(std_ident, std_facility, std_options);
52        globals->msgptr = asl_new(ASL_TYPE_MSG);
53        asl_set(globals->msgptr, ASL_KEY_FACILITY, "com.apple.platformsec");
54    });
55}
56
57#define LINESIZE 256
58
59void
60ccdebug_imp(int level, const char *funcname, const char *format, ...) {
61	va_list argp;
62	char fmtbuffer[LINESIZE];
63
64	cc_globals_t globals = _cc_globals();
65	ccdebug_init(globals);
66
67	va_start(argp, format);
68	snprintf(fmtbuffer, LINESIZE, std_log_prefix, funcname, format);
69	asl_vlog(globals->aslhandle, globals->msgptr, level, fmtbuffer, argp);
70	va_end(argp);
71}
72
73