Skip to content

Instantly share code, notes, and snippets.

@AndSky90
Created November 16, 2019 13:51
Show Gist options
  • Save AndSky90/0ed8bd1c9550263668c45ac136189805 to your computer and use it in GitHub Desktop.
Save AndSky90/0ed8bd1c9550263668c45ac136189805 to your computer and use it in GitHub Desktop.
Logger
/** отключаемый логгер */
object Logger {
private const val LOGGER_TAG = "===SIB_LOG==="
private val className: String = Logger::class.java.name
fun log() = log(null)
fun log(e: Exception) = log("$e")
/**LOGGING_ENABLED_BY_GRADLE зависит от типа сборки в "build.gradle" модуля "core_common"*/
fun log(message: String?) {
@Suppress("ConstantConditionIf")
if (LOGGING_ENABLED_BY_GRADLE && USER_LOGGING_ENABLED) {
val text = if (message != null) " => $message" else ""
Log.d(LOGGER_TAG, "${getLocation()} $text")
}
}
private fun getLocation(): String {
val traces: Array<StackTraceElement> = Thread.currentThread().stackTrace
var found = false
for (trace in traces) {
try {
if (found) {
if (!trace.className.startsWith(className)) {
val clazz = Class.forName(trace.className)
return " ${resolveClassName(clazz)} :: ${trace.methodName}" //: ${trace.lineNumber}
}
} else {
if (trace.className.startsWith(className)) {
found = true
continue
}
}
} catch (e: Throwable) {
return "[ClassNotFound]"
}
}
return "[]"
}
private fun resolveClassName(clazz: Class<*>?): String {
return if (clazz != null) {
if (!TextUtils.isEmpty(clazz.simpleName)) {
clazz.simpleName
} else {
resolveClassName(clazz.enclosingClass)
}
} else ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment