static BOOL _LoadLoggingSupportFramework()

in TwitterLoggingServiceExt/TLSExt.m [226:285]


static BOOL _LoadLoggingSupportFramework(NSError * __nullable * __nullable outError)
{
    if (sLoggingSupportFrameworkHandle) {
        return YES;
    }

    sLoggingSupportFrameworkHandle = dlopen("/System/Library/PrivateFrameworks/LoggingSupport.framework/LoggingSupport", RTLD_NOW);
    if (!sLoggingSupportFrameworkHandle) {
        if (outError) {
            NSString *dlerrorStr = @(dlerror());
            *outError = [NSError errorWithDomain:TLSExtErrorDomain
                                            code:-1
                                        userInfo:(dlerrorStr ? @{ NSDebugDescriptionErrorKey : dlerrorStr } : nil)];
        }
        return NO;
    }

    (void)dlerror(); // flush past dlerrors

    NSString *dlsymErrorString = nil;
#define GET_DLSYM_ERR() ({ \
        if (!dlsymErrorString) { \
            char *dlsymError = dlerror(); \
            if (dlsymError) { \
                dlsymErrorString = @(dlsymError); \
            } \
        } \
    })
    s_func_os_activity_stream_for_pid = (tls_os_activity_stream_for_pid_t)dlsym(sLoggingSupportFrameworkHandle, "os_activity_stream_for_pid");
    GET_DLSYM_ERR();
    s_func_os_activity_stream_resume = (tls_os_activity_stream_resume_t)dlsym(sLoggingSupportFrameworkHandle, "os_activity_stream_resume");
    GET_DLSYM_ERR();
    s_func_os_activity_stream_cancel = (tls_os_activity_stream_cancel_t)dlsym(sLoggingSupportFrameworkHandle, "os_activity_stream_cancel");
    GET_DLSYM_ERR();
    s_func_os_log_copy_formatted_message = (tls_os_log_copy_formatted_message_t)dlsym(sLoggingSupportFrameworkHandle, "os_log_copy_formatted_message");
    GET_DLSYM_ERR();
    s_func_os_activity_stream_set_event_handler = (tls_os_activity_stream_set_event_handler_t)dlsym(sLoggingSupportFrameworkHandle, "os_activity_stream_set_event_handler");
    GET_DLSYM_ERR();
    s_func_os_log_get_type = (uint8_t(*)(void *))dlsym(sLoggingSupportFrameworkHandle, "os_log_get_type");
    GET_DLSYM_ERR();

    const BOOL didLoadAllSymbols =  s_func_os_activity_stream_set_event_handler != nil &&
                                    s_func_os_activity_stream_for_pid != nil &&
                                    s_func_os_activity_stream_cancel != nil &&
                                    s_func_os_activity_stream_resume != nil &&
                                    s_func_os_log_copy_formatted_message != nil &&
                                    s_func_os_log_get_type != nil;
    if (!didLoadAllSymbols) {
        if (outError) {
            *outError = [NSError errorWithDomain:TLSExtErrorDomain
                                            code:-2
                                        userInfo:(dlsymErrorString ? @{ NSDebugDescriptionErrorKey : dlsymErrorString } : nil)];
        }
        dlclose(sLoggingSupportFrameworkHandle);
        sLoggingSupportFrameworkHandle = NULL;
        return NO;
    }

    return YES;
}