Helper function to log from anywhere in the app :
fun log(
message: String,
tag: String? = null,
isError: Boolean = false,
buildType: String
)
-
when
isError
is passed astrue
, log is printed on the error stream otherwise on the info stream. -
buildType
is taken as input to decide whether log should be printed. Pass your app'sBuildConfig.BUILD_TYPE
, which if equals"debug"
then only log is printed.
If
BuildConfig
class is not resolved, setandroid.buildFeatures.buildConfig
totrue
in your app's Gradle file and rebuild the project.
Helper function to get a CoroutineContext
in built with exception handling :
fun defaultContext(
tag: String? = null,
onExceptionOccurred: (Throwable) -> Unit = {},
buildType: String
): CoroutineContext
- uses
Dispatchers.IO
by default
When exception occurs,
-
exception message & stack trace is logged (if
"debug"
buildType) -
onExceptionOccurred
lambda is invoked
Or if you prefer to get only an exception handler, call this function :
fun defaultExceptionHandler(
tag: String? = null,
onExceptionOccurred: (Throwable) -> Unit = {},
buildType: String
): CoroutineExceptionHandler
Using the CoroutineContext
& ExceptionHandler
returned by the above described unctions, if you want to execute a suspend
lambda, use this function :
fun defaultExecuteHandlingError(
tag: String? = null,
onExceptionOccurred: (Throwable) -> Unit = {},
lambda: suspend CoroutineScope.() -> Unit,
buildType: String
)
defaultContext()
,defaultExceptionHandler()
,defaultExecuteHandlingError()
functions might be useful in different Android components other thanActivity
&ViewModel
(becauseBaseActivity
&BaseViewModel
helper functions can be accessed there) likeService
,BroadcastReceiver
etc.