CdiReturnStatus CdiGlobalInitialization()

in src/cdi/internal.c [222:323]


CdiReturnStatus CdiGlobalInitialization(const CdiCoreConfigData* core_config_ptr)
{
    // NOTE: Since the caller is the application's thread, use SDK_LOG_GLOBAL() for any logging in this function.
    CdiReturnStatus rs = kCdiStatusOk;

    CdiOsStaticMutexLock(global_context_mutex_lock);

    if (cdi_global_context.sdk_initialized) {
        SDK_LOG_GLOBAL(kLogError, "SDK Already initialized.");
        rs = kCdiStatusNonFatal;
    }

    if (kCdiStatusOk == rs) {
        // Create a critical section used to protect access to connection_list.
        if (!CdiOsCritSectionCreate(&cdi_global_context.adapter_handle_list_lock)) {
            rs = kCdiStatusNotEnoughMemory;
        }
    }

    if (kCdiStatusOk == rs) {
        CdiListInit(&cdi_global_context.adapter_handle_list);
    }

    // Ensure the logger has been initialized.
    if (!CdiLoggerInitialize()) {
        rs = kCdiStatusFatal;
    }

    if (kCdiStatusOk == rs) {
        if (!CdiLoggerCreate(core_config_ptr->default_log_level, &cdi_global_context.logger_handle)) {
            rs = kCdiStatusFatal;
        }
    }

    if (kCdiStatusOk == rs) {
        if (!CdiLoggerCreateLog(cdi_global_context.logger_handle, NULL, core_config_ptr->global_log_method_data_ptr,
                                &cdi_global_context.global_log_handle)) {
            rs = kCdiStatusCreateLogFailed;
        }
    }

    // If CloudWatch pointer exists, set pointer to point to a copy of the settings and then save a copy of the
    // configuration strings. This is done so the caller can free the memory used by the data.
    if (kCdiStatusOk == rs && core_config_ptr->cloudwatch_config_ptr) {
#ifdef CLOUDWATCH_METRICS_ENABLED
        CdiCloudWatchConfigData cleaned_cloudwatch_config = { 0 };
        const CdiCloudWatchConfigData* cloudwatch_config_ptr = core_config_ptr->cloudwatch_config_ptr;

        // If a namespace string is not provided for cloudwatch use the CDI SDK default namespace string.
        if (!cloudwatch_config_ptr->namespace_str || ('\0' == cloudwatch_config_ptr->namespace_str[0])) {
            SDK_LOG_GLOBAL(kLogInfo, "CloudWatch namespace string not provided. Using default [%s].",
                           CLOUDWATCH_DEFAULT_NAMESPACE_STRING);
            cleaned_cloudwatch_config.namespace_str = CLOUDWATCH_DEFAULT_NAMESPACE_STRING;
        } else {
            cleaned_cloudwatch_config.namespace_str = cloudwatch_config_ptr->namespace_str;
        }

        // Region does not need any cleaning because the AWS SDK will automatically use the region called from if
        // a region is not set.
        cleaned_cloudwatch_config.region_str = cloudwatch_config_ptr->region_str;

        // A dimension domain string must be provided.
        if (!cloudwatch_config_ptr->dimension_domain_str || ('\0' == cloudwatch_config_ptr->dimension_domain_str[0])) {
            SDK_LOG_GLOBAL(kLogError, "CloudWatch dimension domain string cannot be NULL.");
            rs = kCdiStatusInvalidParameter;
        } else {
            cleaned_cloudwatch_config.dimension_domain_str = cloudwatch_config_ptr->dimension_domain_str;
        }

        if (kCdiStatusOk == rs) {
            rs = CloudWatchSdkMetricsCreate(&cleaned_cloudwatch_config, &cdi_global_context.cw_sdk_handle);
        }
#else  // CLOUDWATCH_METRICS_ENABLED
        SDK_LOG_GLOBAL(kLogError,
                       "Cannot use CloudWatch. The SDK was not built with CLOUDWATCH_METRICS_ENABLED defined.");
        rs = kCdiStatusCloudWatchNotEnabled;
#endif  // CLOUDWATCH_METRICS_ENABLED
    }

#ifdef METRICS_GATHERING_SERVICE_ENABLED
    if (kCdiStatusOk == rs) {
        bool use_default_dimension_string = (NULL == core_config_ptr->cloudwatch_config_ptr) ||
                                            (NULL == core_config_ptr->cloudwatch_config_ptr->dimension_domain_str) ||
                                            ('\0' == core_config_ptr->cloudwatch_config_ptr->dimension_domain_str[0]);
        const MetricsGathererConfigData config = {
            .dimension_domain_str = use_default_dimension_string ? "<none>" :
                                        core_config_ptr->cloudwatch_config_ptr->dimension_domain_str
        };
        rs = MetricsGathererCreate(&config, &cdi_global_context.metrics_gathering_sdk_handle);
    }
#endif  // METRICS_GATHERING_SERVICE_ENABLED

    if (kCdiStatusOk == rs) {
        cdi_global_context.sdk_initialized = true;
    } else {
        CleanupGlobalResources();
    }

    CdiOsStaticMutexUnlock(global_context_mutex_lock);

    return rs;
}