1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import android.util.Log import org.json.JSONArray import org.json.JSONException import org.json.JSONObject
object L { enum class LogLevel { ERROR { override val value: Int get() = 0 }, WARN { override val value: Int get() = 1 }, INFO { override val value: Int get() = 2 }, DEBUG { override val value: Int get() = 3 };
abstract val value: Int }
private var TAG = "SAF_L" var logLevel = LogLevel.DEBUG @JvmStatic fun init(clazz: Class<*>) { TAG = clazz.simpleName }
@JvmStatic fun init(tag: String) { TAG = tag }
@JvmStatic fun e(msg: String) { if (LogLevel.ERROR.value <= logLevel.value) { if (msg.isNotBlank()) { val s = getMethodNames() Log.e(TAG, String.format(s, msg)) } } }
@JvmStatic fun w(msg: String) { if (LogLevel.WARN.value <= logLevel.value) { if (msg.isNotBlank()) { val s = getMethodNames() Log.e(TAG, String.format(s, msg)) } } }
@JvmStatic fun i(msg: String) { if (LogLevel.INFO.value <= logLevel.value) { if (msg.isNotBlank()) { val s = getMethodNames() Log.i(TAG, String.format(s, msg)) } } }
@JvmStatic fun d(msg: String) { if (LogLevel.DEBUG.value <= logLevel.value) { if (msg.isNotBlank()) { val s = getMethodNames() Log.d(TAG, String.format(s, msg)) } } }
@JvmStatic fun json(json: String) { var json = json if (json.isBlank()) { d("Empty/Null json content") return } try { json = json.trim { it <= ' ' } if (json.startsWith("{")) { val jsonObject = JSONObject(json) var message = jsonObject.toString(LoggerPrinter.JSON_INDENT) message = message.replace("\n".toRegex(), "\n║ ") val s = getMethodNames() println(String.format(s, message)) return } if (json.startsWith("[")) { val jsonArray = JSONArray(json) var message = jsonArray.toString(LoggerPrinter.JSON_INDENT) message = message.replace("\n".toRegex(), "\n║ ") val s = getMethodNames() println(String.format(s, message)) return } e("Invalid Json") } catch (e: JSONException) { e("Invalid Json") } }
private fun getMethodNames(): String { val sElements = Thread.currentThread().stackTrace var stackOffset = LoggerPrinter.getStackOffset(sElements) stackOffset++ val builder = StringBuilder() builder.append(LoggerPrinter.TOP_BORDER).append("\r\n") .append("║ " + "Thread: " + Thread.currentThread().name).append("\r\n") .append(LoggerPrinter.MIDDLE_BORDER).append("\r\n") .append("║ ") .append(sElements[stackOffset].className) .append(".") .append(sElements[stackOffset].methodName) .append(" ") .append(" (") .append(sElements[stackOffset].fileName) .append(":") .append(sElements[stackOffset].lineNumber) .append(")") .append("\r\n") .append(LoggerPrinter.MIDDLE_BORDER).append("\r\n") .append("║ ").append("%s").append("\r\n") .append(LoggerPrinter.BOTTOM_BORDER).append("\r\n") return builder.toString() }
fun String.isBlank(msg: String): Boolean { return msg == null || msg.length == 0; }
fun String.isNotBlank(msg: String): Boolean { return !msg.isBlank(); } }
|