All Downloads are FREE. Search and download functionalities are using the official Maven repository.

androidMain.aws.sdk.kotlin.crt.CRT.kt Maven / Gradle / Ivy

There is a newer version: 0.8.9
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package aws.sdk.kotlin.crt

import software.amazon.awssdk.crt.Log
import software.amazon.awssdk.crt.http.HttpClientConnection
import software.amazon.awssdk.crt.http.HttpException
import java.util.concurrent.atomic.AtomicInteger as AtomicInt
import software.amazon.awssdk.crt.CRT as crtJni

public actual object CRT {
    private val initialized: AtomicInt = AtomicInt(0)
    public actual fun initRuntime(block: Config.() -> Unit) {
        if (!initialized.compareAndSet(0, 1)) return

        System.setProperty("aws.crt.memory.tracing", "${CrtDebug.traceLevel}")
        // load the JNI library
        crtJni()
        val config = Config().apply(block)
        val logLevel = Log.LogLevel.valueOf(config.logLovel.name)
        when (config.logDestination) {
            LogDestination.None -> return
            LogDestination.Stdout -> Log.initLoggingToStdout(logLevel)
            LogDestination.Stderr -> Log.initLoggingToStderr(logLevel)
            LogDestination.File -> {
                val logfile = config.logFile
                requireNotNull(logfile) { "log filename must be specified when LogDestination.File is specified" }
                Log.initLoggingToFile(logLevel, logfile)
            }
        }
    }

    /**
     * Returns the last error on the current thread.
     * @return Last error code recorded in this thread
     */
    public actual fun lastError(): Int = crtJni.awsLastError()

    /**
     * Given an integer error code from an internal operation
     * @param errorCode An error code returned from an exception or other native function call
     * @return A user-friendly description of the error
     */
    public actual fun errorString(errorCode: Int): String? = crtJni.awsErrorString(errorCode)

    /**
     * Given an integer error code from an internal operation
     *
     * @param errorCode An error code returned from an exception or other native
     * function call
     * @return A string identifier for the error
     */
    public actual fun errorName(errorCode: Int): String? = crtJni.awsErrorName(errorCode)

    public actual fun isHttpErrorRetryable(errorCode: Int): Boolean {
        // An exception subtype that doesn't create a stack, which saves a bunch of time
        class StacklessHttpException(errorCode: Int) : HttpException(errorCode) {
            // Changing `fillInStackTrace` to a no-op skips filling in the stack
            override fun fillInStackTrace(): Throwable = this
        }

        val phonyException = StacklessHttpException(errorCode)
        return HttpClientConnection.isErrorRetryable(phonyException)
    }

    /**
     * @return The number of bytes allocated in native resources. If aws.crt.memory.tracing is 1 or 2, this will
     * be a non-zero value. Otherwise, no tracing will be done, and the value will always be 0
     */
    public actual fun nativeMemory(): Long = crtJni.nativeMemory()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy