io.bitrise.gradle.cache.connection.LoggingInterceptor.kt Maven / Gradle / Ivy
package io.bitrise.gradle.cache.connection
import io.grpc.CallOptions
import io.grpc.Channel
import io.grpc.ClientCall
import io.grpc.ClientInterceptor
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener
import io.grpc.Metadata
import io.grpc.MethodDescriptor
import io.grpc.Status
class LoggingInterceptor : ClientInterceptor {
override fun interceptCall(
method: MethodDescriptor,
callOptions: CallOptions,
next: Channel,
): ClientCall {
return LoggingClientCall(
next.newCall(
method,
callOptions,
),
)
}
}
private class LoggingClientCall(call: ClientCall?) :
SimpleForwardingClientCall(call) {
override fun start(responseListener: Listener, headers: Metadata) {
println(
"Client call with headers:\n${
headers.keys()
.filter { !it.endsWith("-bin") }
.joinToString("\n") { key ->
" $key: ${headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER))}"
}
}",
)
super.start(LoggingClientCallListener(responseListener), headers)
}
private inner class LoggingClientCallListener(responseListener: Listener?) :
SimpleForwardingClientCallListener(responseListener) {
override fun onHeaders(headers: Metadata) {
println(
"Response headers:\n${
headers.keys()
.filter { !it.endsWith("-bin") }
.joinToString("\n") { key ->
" $key: ${headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER))}"
}
}",
)
super.onHeaders(headers)
}
override fun onClose(status: Status, trailers: Metadata) {
println("Close status: ${status.code}")
println(
"Close headers:\n${
trailers.keys()
.filter { !it.endsWith("-bin") }
.joinToString("\n") { key ->
" $key: ${trailers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER))}"
}
}",
)
super.onClose(status, trailers)
}
}
}