예제 #1
0
void __security_debug(const char *scope, const char *function,
    const char *file, int line, const char *format, ...)
{
#if !defined(NDEBUG)
	pthread_once(&__security_debug_once, __security_debug_init);

	CFStringRef scopeName = NULL;
	/* Scope NULL is always enabled. */
	if (scope) {
		/* Check if the scope is enabled. */
		if (scopeSet) {
			scopeName = copyScopeName(scope, strlen(scope));
			if (negate == CFSetContainsValue(scopeSet, scopeName)) {
				CFRelease(scopeName);
				return;
			}
		} else if (!negate) {
			return;
		}
	}

	CFStringRef formatStr = CFStringCreateWithCString(kCFAllocatorDefault,
		format, kCFStringEncodingUTF8);
	va_list args;
	va_start(args, format);
	CFStringRef message = CFStringCreateWithFormatAndArguments(
		kCFAllocatorDefault, NULL, formatStr, args);
	va_end(args);
	time_t now = time(NULL);
	char *date = ctime(&now);
	date[19] = '\0';
	CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
		CFSTR("%s %-*s %s %@\n"), date + 4, MAX_SCOPE_LENGTH - 1,
        scope ? scope : "", function, message);
	CFShow(logStr);
    char logMsg[4096];
    if (CFStringGetCString(logStr, logMsg, sizeof(logMsg), kCFStringEncodingUTF8)) {
#if 0
        asl_log(NULL, NULL, ASL_LEVEL_INFO, logMsg);
#else
        aslmsg msg = asl_new(ASL_TYPE_MSG);
        if (scope) {
            asl_set(msg, ASL_KEY_FACILITY, scope);
        }
        asl_set(msg, ASL_KEY_LEVEL, ASL_STRING_INFO);
        asl_set(msg, ASL_KEY_MSG, logMsg);
        asl_send(NULL, msg);
        asl_free(msg);
#endif
    }
	CFRelease(logStr);
	CFRelease(message);
	CFRelease(formatStr);
	if (scopeName)
		CFRelease(scopeName);
#endif
}
static CFMutableArrayRef get_log_handlers()
{
    static dispatch_once_t handlers_once;
    
    dispatch_once(&handlers_once, ^{
        sSecurityLogHandlers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
        
        CFArrayAppendValue(sSecurityLogHandlers, ^(const char *level, CFStringRef scope, const char *function,
                                                   const char *file, int line, CFStringRef message){
            CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ %s %@\n"), scope ? scope : CFSTR(""), function, message);
            CFStringPerformWithCString(logStr, ^(const char *logMsg) {
                aslmsg msg = asl_new(ASL_TYPE_MSG);
                if (scope) {
                    CFStringPerformWithCString(scope, ^(const char *scopeStr) {
                        asl_set(msg, ASL_KEY_FACILITY, scopeStr);
                    });
                }
                asl_set(msg, ASL_KEY_LEVEL, level);
                asl_set(msg, ASL_KEY_MSG, logMsg);
                asl_send(NULL, msg);
                asl_free(msg);
            });
            CFReleaseSafe(logStr);
        });