2018-12-01 00:02:18 +01:00
|
|
|
package jp.juggler.util
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
import android.content.res.Resources
|
2018-08-28 14:56:09 +02:00
|
|
|
import android.util.Log
|
2021-06-13 05:30:40 +02:00
|
|
|
import androidx.annotation.StringRes
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2022-06-04 20:35:07 +02:00
|
|
|
fun Throwable.withCaption(caption: String? = null) =
|
2022-05-30 14:32:12 +02:00
|
|
|
when {
|
|
|
|
caption.isNullOrBlank() -> "${javaClass.simpleName} $message"
|
|
|
|
else -> "$caption :${javaClass.simpleName} $message"
|
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
|
|
|
|
fun Throwable.withCaption(resources: Resources, stringId: Int, vararg args: Any) =
|
|
|
|
"${resources.getString(stringId, *args)}: ${javaClass.simpleName} $message"
|
|
|
|
|
2022-12-27 03:54:52 +01:00
|
|
|
// cause 付きの IllegalStateException を投げる
|
|
|
|
fun errorEx(cause: Throwable, caption: String): Nothing =
|
|
|
|
throw IllegalStateException(caption, cause)
|
2021-06-13 05:30:40 +02:00
|
|
|
|
2022-12-27 03:54:52 +01:00
|
|
|
class LogCategory(val category: String) {
|
2021-06-13 05:30:40 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "SubwayTooter"
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// string
|
|
|
|
|
2021-10-27 22:58:19 +02:00
|
|
|
private fun msg(priority: Int, msg: String): Boolean {
|
2022-12-27 03:54:52 +01:00
|
|
|
Log.println(priority, TAG, "$category/$msg")
|
2021-06-13 05:30:40 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fun e(msg: String) = msg(Log.ERROR, msg)
|
|
|
|
fun w(msg: String) = msg(Log.WARN, msg)
|
|
|
|
fun i(msg: String) = msg(Log.INFO, msg)
|
|
|
|
fun d(msg: String) = msg(Log.DEBUG, msg)
|
|
|
|
fun v(msg: String) = msg(Log.VERBOSE, msg)
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// Resources.getString()
|
|
|
|
|
2022-06-04 20:35:07 +02:00
|
|
|
private fun msg(
|
|
|
|
priority: Int,
|
|
|
|
res: Resources,
|
|
|
|
@StringRes stringId: Int,
|
|
|
|
args: Array<out Any?>,
|
|
|
|
) = msg(priority, res.getString(stringId, *args))
|
2021-06-13 05:30:40 +02:00
|
|
|
|
2022-06-03 16:23:44 +02:00
|
|
|
fun e(res: Resources, @StringRes stringId: Int, vararg args: Any?) =
|
2021-06-13 05:30:40 +02:00
|
|
|
msg(Log.ERROR, res, stringId, args)
|
|
|
|
|
2022-06-03 16:23:44 +02:00
|
|
|
fun w(res: Resources, @StringRes stringId: Int, vararg args: Any?) =
|
2021-06-13 05:30:40 +02:00
|
|
|
msg(Log.WARN, res, stringId, args)
|
|
|
|
|
2022-06-03 16:23:44 +02:00
|
|
|
fun i(res: Resources, @StringRes stringId: Int, vararg args: Any?) =
|
2021-06-13 05:30:40 +02:00
|
|
|
msg(Log.INFO, res, stringId, args)
|
|
|
|
|
2022-06-03 16:23:44 +02:00
|
|
|
fun d(res: Resources, @StringRes stringId: Int, vararg args: Any?) =
|
2021-06-13 05:30:40 +02:00
|
|
|
msg(Log.DEBUG, res, stringId, args)
|
|
|
|
|
2022-06-03 16:23:44 +02:00
|
|
|
fun v(res: Resources, @StringRes stringId: Int, vararg args: Any?) =
|
2021-06-13 05:30:40 +02:00
|
|
|
msg(Log.VERBOSE, res, stringId, args)
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// stack trace
|
|
|
|
|
2022-12-27 03:54:52 +01:00
|
|
|
fun e(ex: Throwable, caption: String?) =
|
|
|
|
false.also { Log.e(TAG, "$category/${ex.withCaption(caption)}", ex) }
|
|
|
|
|
|
|
|
fun w(ex: Throwable, caption: String?) =
|
|
|
|
false.also { Log.w(TAG, "$category/${ex.withCaption(caption)}", ex) }
|
2021-06-13 05:30:40 +02:00
|
|
|
}
|