• kotlin:LogKit


    看到别人的一个代码,觉得有点意思,就复制过来。

    1. package robat
    2. import android.util.Log
    3. import java.util.*
    4. object LogKit {
    5. private val MIN_STACK_OFFSET = 3
    6. var defaultTag = "LogKit"
    7. private val lineSeparator = System.getProperty("line.separator", "/n")
    8. val V = Log.VERBOSE
    9. val D = Log.DEBUG
    10. val I = Log.INFO
    11. val W = Log.WARN
    12. val E = Log.ERROR
    13. val A = Log.ASSERT
    14. private val TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════"
    15. private val LEFT_BORDER = "║ "
    16. private val BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════"
    17. private val MAX_LEN = 1000
    18. var open = true
    19. private fun processTagAndHead(): String {
    20. val elements = Thread.currentThread().stackTrace
    21. val offset = getStackOffset(elements)
    22. val targetElement = elements[offset]
    23. val head = Formatter()
    24. .format("%s [%s(%s:%d)]",
    25. "In Thread: " + Thread.currentThread().name,
    26. targetElement.methodName,
    27. targetElement.fileName,
    28. targetElement.lineNumber)
    29. return head.toString()
    30. }
    31. private fun processMsgBody(msg: String, flag: Int, tag: String = defaultTag) {
    32. printTop(flag, tag)
    33. // 首先打印调用信息
    34. printLog(flag, tag)
    35. val lineCount = msg.length / MAX_LEN
    36. if (lineCount == 0) {
    37. printLog(flag, tag, msg)
    38. } else {
    39. var index = 0
    40. var i = 0
    41. while (true) {
    42. printLog(flag, tag, msg.substring(index, index + MAX_LEN))
    43. index += MAX_LEN
    44. if ((++i) >= lineCount)
    45. break
    46. }
    47. }
    48. printBottom(flag, tag)
    49. }
    50. fun getStackOffset(trace: Array<StackTraceElement>): Int {
    51. var i = MIN_STACK_OFFSET
    52. while (i < trace.size) {
    53. val e = trace[i]
    54. val name = e.className
    55. if (name != LogKit::class.java.name) {
    56. return i
    57. }
    58. i++
    59. }
    60. return 2
    61. }
    62. /* 虽然 kotlin 有默认值这种操作,但是 Log.i(tag,msg) 这种比较符合平时的操作,所以还是提供类似的重载,
    63. * 而非 LogUtil.i(msg: String,tag: String = defaultTAG) 这种带默认值参数的方法 */
    64. fun v(msg: String) {
    65. v(defaultTag, msg)
    66. }
    67. fun i(msg: String) {
    68. i(defaultTag, msg)
    69. }
    70. fun d(msg: String) {
    71. d(defaultTag, msg)
    72. }
    73. fun w(msg: String) {
    74. w(defaultTag, msg)
    75. }
    76. fun e(msg: String) {
    77. e(defaultTag, msg)
    78. }
    79. fun v(tag: String, msg: String) {
    80. if (!open) {
    81. return
    82. }
    83. processMsgBody(msg, V, tag)
    84. }
    85. fun i(tag: String, msg: String) {
    86. if (!open) {
    87. return
    88. }
    89. processMsgBody(msg, I, tag)
    90. }
    91. fun d(tag: String, msg: String) {
    92. if (!open) {
    93. return
    94. }
    95. processMsgBody(msg, D, tag)
    96. }
    97. fun w(tag: String, msg: String) {
    98. if (!open) {
    99. return
    100. }
    101. processMsgBody(msg, W, tag)
    102. }
    103. fun e(tag: String, msg: String) {
    104. if (!open) {
    105. return
    106. }
    107. processMsgBody(msg, E, tag)
    108. }
    109. fun printLog(flag: Int, tag: String, msg: String = processTagAndHead()) {
    110. Log.println(flag, tag, LEFT_BORDER + msg)
    111. }
    112. fun printBottom(flag: Int, tag: String) {
    113. Log.println(flag, tag, BOTTOM_BORDER)
    114. }
    115. fun printTop(flag: Int, tag: String) {
    116. Log.println(flag, tag, TOP_BORDER)
    117. }
    118. fun closeLog() {
    119. this.open = false
    120. }
    121. }

  • 相关阅读:
    k8s存储
    @Builder注解有什么用?怎么用?
    QT+OSG/osgEarth编译之四十:osg+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5核心库osg)
    (4) OpenCV图像处理kNN近邻算法-识别数字0和1
    Linux 环境变量
    CodeWhisperer 使用经验分享
    c++ 学习 之 指针常量 和 常量指针
    多线程&并发篇---第五篇
    【Spring Cloud】网关Gateway的请求过滤工厂RequestRateLimiterGatewayFilterFactory
    ORM--查询类型,关联查询
  • 原文地址:https://blog.csdn.net/quantum7/article/details/133673673