Skip to content

Instantly share code, notes, and snippets.

@Cassie-von-Clausewitz
Created June 27, 2017 07:28
Show Gist options
  • Save Cassie-von-Clausewitz/b361fd9f13924809a52aeb77c8ac16a8 to your computer and use it in GitHub Desktop.
Save Cassie-von-Clausewitz/b361fd9f13924809a52aeb77c8ac16a8 to your computer and use it in GitHub Desktop.
Android Loggers
val callStackPosition: Int = 2
fun Activity.trace(message: String) {
if (this is BaseActivity) {
this.logger.trace(message)
} else {
Log.v(Thread.currentThread().stackTrace[callStackPosition].className, message)
}
}
fun Activity.trace(message: String, vararg args: Any) {
if (this is BaseActivity) {
this.logger.trace(message, args)
} else {
Log.v(Thread.currentThread().stackTrace[callStackPosition].className, String.format(message, args))
}
}
fun Activity.trace(throwable: Throwable, message: String) {
if (this is BaseActivity) {
this.logger.trace(throwable, message)
} else {
Log.v(Thread.currentThread().stackTrace[callStackPosition].className, message, throwable)
}
}
fun Activity.debug(message: String) {
if (this is BaseActivity) {
this.logger.debug(message)
} else {
Log.d(Thread.currentThread().stackTrace[callStackPosition].className, message)
}
}
fun Activity.debug(message: String, vararg args: Any) {
if (this is BaseActivity) {
this.logger.debug(message, args)
} else {
Log.d(Thread.currentThread().stackTrace[callStackPosition].className, String.format(message, args))
}
}
fun Activity.debug(throwable: Throwable, message: String) {
if (this is BaseActivity) {
this.logger.debug(throwable, message)
} else {
Log.d(Thread.currentThread().stackTrace[callStackPosition].className, message, throwable)
}
}
fun Activity.info(message: String) {
if (this is BaseActivity) {
this.logger.info(message)
} else {
Log.i(Thread.currentThread().stackTrace[callStackPosition].className, message)
}
}
fun Activity.info(message: String, vararg args: Any) {
if (this is BaseActivity) {
this.logger.info(message, args)
} else {
Log.i(Thread.currentThread().stackTrace[callStackPosition].className, String.format(message, args))
}
}
fun Activity.info(throwable: Throwable, message: String) {
if (this is BaseActivity) {
this.logger.info(throwable, message)
} else {
Log.i(Thread.currentThread().stackTrace[callStackPosition].className, message, throwable)
}
}
fun Activity.warn(message: String) {
if (this is BaseActivity) {
this.logger.warn(message)
} else {
Log.w(Thread.currentThread().stackTrace[callStackPosition].className, message)
}
}
fun Activity.warn(message: String, vararg args: Any) {
if (this is BaseActivity) {
this.logger.warn(message, args)
} else {
Log.w(Thread.currentThread().stackTrace[callStackPosition].className, String.format(message, args))
}
}
fun Activity.warn(throwable: Throwable, message: String) {
if (this is BaseActivity) {
this.logger.warn(throwable, message)
} else {
Log.w(Thread.currentThread().stackTrace[callStackPosition].className, message, throwable)
}
}
fun Activity.error(message: String) {
if (this is BaseActivity) {
this.logger.error(message)
} else {
Log.e(Thread.currentThread().stackTrace[callStackPosition].className, message)
}
}
fun Activity.error(message: String, vararg args: Any) {
if (this is BaseActivity) {
this.logger.error(message, args)
} else {
Log.e(Thread.currentThread().stackTrace[callStackPosition].className, String.format(message, args))
}
}
fun Activity.error(throwable: Throwable, message: String) {
if (this is BaseActivity) {
this.logger.error(throwable, message)
} else {
Log.e(Thread.currentThread().stackTrace[callStackPosition].className, message, throwable)
}
}
fun Context.getActivity() : Activity? {
var context = this
while (context is ContextWrapper) {
if (context is Activity) {
return context
}
context = context.baseContext
}
return null
}
context.getActivity()?.debug("Thumbnail Url %s", url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment