Created
December 9, 2021 14:45
-
-
Save tomkoptel/5dc18e90d93cf8b56c8f8abd8d4de2ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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