Skip to content

Instantly share code, notes, and snippets.

@Kraiden
Created October 24, 2017 04:18
Show Gist options
  • Save Kraiden/52d77aefe2bde00a978cb2c6428d7723 to your computer and use it in GitHub Desktop.
Save Kraiden/52d77aefe2bde00a978cb2c6428d7723 to your computer and use it in GitHub Desktop.
import android.util.Log
class ELog() {
companion object {
@JvmStatic fun d(tag: String, msg: String) = Log.d(tag, prependFile(msg))
@JvmStatic fun d(tag: String, msg: String, tr: Throwable) = Log.d(tag, prependFile(msg),tr)
@JvmStatic fun i(tag: String, msg: String) = Log.i(tag, prependFile(msg))
@JvmStatic fun i(tag: String, msg: String, tr:Throwable) = Log.i(tag, prependFile(msg),tr)
@JvmStatic fun v(tag: String, msg: String) = Log.v(tag, prependFile(msg))
@JvmStatic fun v(tag: String, msg: String, tr:Throwable) = Log.v(tag, prependFile(msg),tr)
@JvmStatic fun e(tag: String, msg: String) = Log.e(tag, prependFile(msg))
@JvmStatic fun e(tag: String, msg: String, tr:Throwable) = Log.e(tag, prependFile(msg),tr)
@JvmStatic fun w(tag: String, msg: String) = Log.w(tag, prependFile(msg))
@JvmStatic fun w(tag: String, msg: String, tr:Throwable) = Log.w(tag, prependFile(msg),tr)
@JvmStatic fun wtf(tag: String, msg: String) = Log.wtf(tag, prependFile(msg))
@JvmStatic fun wtf(tag: String, msg: String, tr:Throwable) = Log.wtf(tag, prependFile(msg),tr)
private inline fun prependFile(str: String): String {
val frame = Exception().stackTrace[1]
return ".(${frame.fileName}:${frame.lineNumber}) - $str"
}
}
}
@Kraiden
Copy link
Author

Kraiden commented Oct 24, 2017

Using this will prepend the filename and line number to your log message, making them clickable in Android Studio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment