Hello, is there no way to get current function / l...
# multiplatform
s
Hello, is there no way to get current function / line / file in KMP ? In JVM targets you can use Thread.currentThread().stackTrace to compute these info but not in KMP as the stacktrace is just a string (from the stacktrace platform) I made some parser for it some time ago but it's really not the proper way of doing it. Isn't there a compiler intrinsic for this ? Like in C with
__LINE__
__FILE__
__function__
There is a lot of compile time information that could me handy for debug/logging in Kotlin so some intrinsic function for these would be nice. For the current class i can do a simple extension prop :
Copy code
val Any.TAG get() = this::class.simpleName
But didn't found alternative for other
c
s
Look nice, I don't really like to resort to not first party stuff for basic feature but it should do the trick, thank you
s
Copy code
abstract fun log(
		level: LogLevel,
		tag: String?,
		message: String,
		throwable: Throwable?,
		lineNumber: Int?,
		fileName: String?,
		methodName: String?,
		typeName: String?
	)

	@JvmOverloads
	inline fun logv(tag: String? = null, message: String, throwable: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)

	@JvmOverloads
	inline fun logd(tag: String? = null, message: String, throwable: Throwable? = null) = log(LogLevel.DEBUG, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)

	@JvmOverloads
	inline fun logi(tag: String? = null, message: String, throwable: Throwable? = null) = log(<http://LogLevel.INFO|LogLevel.INFO>, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)

	@JvmOverloads
	inline fun logw(tag: String? = null, message: String, throwable: Throwable? = null) = log(LogLevel.WARNING, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)

	@JvmOverloads
	inline fun loge(tag: String? = null, message: String, throwable: Throwable? = null) = log(LogLevel.ERROR, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)

	@JvmOverloads
	inline fun logwtf(tag: String? = null, message: String, throwable: Throwable? = null) = log(LogLevel.CRITICAL, tag, message, throwable, __LINE__, __FILE__, __MEMBER__, __TYPE__)
This doesn't work it seem the plugin work before inlining 😞
Maybe a class with all info and a function INFO() that a plugin replace with
INFO(__LINE__, __FILE__, ...)
So you can optin with INFO() on any log