Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
Created December 9, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkoptel/5dc18e90d93cf8b56c8f8abd8d4de2ad to your computer and use it in GitHub Desktop.
Save tomkoptel/5dc18e90d93cf8b56c8f8abd8d4de2ad to your computer and use it in GitHub Desktop.
import android.util.Log
import com.google.gson.Gson
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
const val TAG = "MyAppRx"
fun Completable.logEvents(hint: String? = null): Completable = this
.doOnError { it.logRxOnError(hint) }
.doOnSubscribe { logOnSubscribe(hint) }
.doOnDispose { logOnDispose(hint) }
fun <T : Any> Single<T>.logEvents(hint: String? = null): Single<T> = this
.doOnSuccess { it.logRxOnSuccess(hint) }
.doOnError { it.logRxOnError(hint) }
.doOnSubscribe { logOnSubscribe(hint) }
.doOnDispose { logOnDispose(hint) }
fun <T : Any> Observable<T>.logEvents(hint: String? = null): Observable<T> = this
.doOnNext { it.logRxOnNext(hint) }
.doOnError { it.logRxOnError(hint) }
.doOnSubscribe { logOnSubscribe(hint) }
.doOnDispose { logOnDispose(hint) }
fun <T : Any> Maybe<T>.logEvents(hint: String? = null): Maybe<T> = this
.doOnSubscribe { logOnSubscribe(hint) }
.doOnDispose { logOnDispose(hint) }
.doOnSuccess { it.logRxOnSuccess(hint) }
.doOnError { it.logRxOnError(hint) }
private fun logOnSubscribe(prefix: String? = null) {
Log.d(TAG, "prefix=$prefix onSubscribe")
}
private fun logOnDispose(prefix: String? = null) {
Log.d(TAG, "prefix=$prefix onDispose")
}
private fun Any.logRxOnSuccess(prefix: String? = null) {
Log.d(TAG, "prefix=$prefix onSuccess result=${toReadableResult()}")
}
private fun Any.logRxOnNext(prefix: String? = null) {
Log.d(TAG, "prefix=$prefix onNext result=${toReadableResult()}")
}
private fun Throwable.logRxOnError(prefix: String? = null) {
Log.d(TAG, "prefix=$prefix onError message=$message", cause)
}
private fun Any.toReadableResult() =
"${
if (this is List<*> && this.isNotEmpty()) "${javaClass.simpleName}<${this.first()?.javaClass?.simpleName}>"
else javaClass.simpleName
} - ${Gson().toJson(this)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment