-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggerText.kt
299 lines (270 loc) · 7.18 KB
/
LoggerText.kt
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xuexiang.uithemesample.widget
import android.content.Context
import android.os.Looper
import android.text.Spannable
import android.text.SpannableString
import android.text.method.ScrollingMovementMethod
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.xuexiang.uithemesample.R
import com.xuexiang.xui.utils.ResUtils
/**
* 日志打印显示控件【继承组件的方式】
*
* @author xuexiang
* @since 2020/12/10 12:32 AM
*/
class LoggerText @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.LoggerTextStyle
) : AppCompatTextView(
context, attrs, defStyleAttr
) {
/**
* 日志格式化接口
*/
private var mLogFormatter: ILogFormatter? = DefaultLogFormatter()
/**
* 日志装饰接口
*/
private var mLogDecorator: ILogDecorator? = DefaultLogDecorator()
/**
* 设置日志格式化接口
*
* @param logFormatter 日志格式化接口
* @return 日志打印显示控件
*/
fun setLogFormatter(logFormatter: ILogFormatter): LoggerText {
mLogFormatter = logFormatter
return this
}
/**
* 设置日志装饰接口
*
* @param logDecorator 日志装饰接口
* @return 日志打印显示控件
*/
fun setLogDecorator(logDecorator: ILogDecorator): LoggerText {
mLogDecorator = logDecorator
return this
}
/**
* 添加普通日志
*
* @param logContent 日志内容
*/
fun logNormal(logContent: String) {
addLog(logContent, LogType.NORMAL)
}
/**
* 添加成功日志
*
* @param logContent 日志内容
*/
fun logSuccess(logContent: String) {
addLog(logContent, LogType.SUCCESS)
}
/**
* 添加错误日志
*
* @param logContent 日志内容
*/
fun logError(logContent: String) {
addLog(logContent, LogType.ERROR)
}
/**
* 添加警告日志
*
* @param logContent 日志内容
*/
fun logWarning(logContent: String) {
addLog(logContent, LogType.WARNING)
}
/**
* 添加自定义等级日志
*
* @param logContent 日志内容
*/
fun logCustom(logContent: String) {
addLog(logContent, LogType.CUSTOM)
}
/**
* 添加日志
*
* @param logContent 日志内容
* @param logType 日志类型
*/
fun addLog(logContent: String, logType: LogType?) {
val spannableString =
logDecorator.decorate(logFormatter.format(logContent, logType), logType)
appendLogInMainThread(spannableString)
}
private fun appendLogInMainThread(spannableString: SpannableString) {
if (Looper.getMainLooper() == Looper.myLooper()) {
appendLog(spannableString)
} else {
post { appendLog(spannableString) }
}
}
private val logDecorator by lazy {
DefaultLogDecorator()
}
/**
* 获取日志格式化接口
*
* @return 日志格式化接口
*/
private val logFormatter by lazy {
DefaultLogFormatter()
}
private fun appendLog(spannableString: SpannableString) {
append(spannableString)
append("\r\n")
scrollToEnd()
}
/**
* 滑动至最后一行
*/
private fun scrollToEnd() {
val offset = textRealHeight
if (offset > height) {
scrollTo(0, offset - height)
}
}
/**
* @return 获取当前TextView文字的真实高度
*/
private val textRealHeight: Int
get() {
val layout = layout
return (layout?.getLineTop(lineCount) ?: 0) + compoundPaddingTop + compoundPaddingBottom
}
/**
* 清除日志
*/
fun clearLog() {
if (Looper.getMainLooper() == Looper.myLooper()) {
text = ""
scrollTo(0, 0)
} else {
post {
text = ""
scrollTo(0, 0)
}
}
}
/**
* 默认日志格式化接口
*
* @author xuexiang
* @since 2021/4/14 1:48 AM
*/
class DefaultLogFormatter : ILogFormatter {
override fun format(logContent: String, logType: LogType?): String {
return logContent
}
}
/**
* 日志格式化接口
*
* @author xuexiang
* @since 2021/4/14 1:30 AM
*/
interface ILogFormatter {
/**
* 格式化日志内容
*
* @param logContent 日志内容
* @param logType 日志类型
* @return 格式化后的日志
*/
fun format(logContent: String, logType: LogType?): String
}
/**
* 默认日志装饰接口
*
* @author xuexiang
* @since 2021/4/14 1:49 AM
*/
class DefaultLogDecorator : ILogDecorator {
override fun decorate(logContent: String, logType: LogType?): SpannableString {
val spannableString = SpannableString(logContent)
val colorSpan = when (logType) {
LogType.ERROR -> ForegroundColorSpan(ResUtils.getColor(R.color.xui_config_color_error))
LogType.SUCCESS -> ForegroundColorSpan(ResUtils.getColor(R.color.xui_config_color_success))
LogType.WARNING -> ForegroundColorSpan(ResUtils.getColor(R.color.xui_config_color_waring))
else -> null
}
colorSpan?.let {
spannableString.setSpan(
it, 0,
logContent.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
return spannableString
}
}
/**
* 日志装饰接口
*
* @author xuexiang
* @since 2021/4/14 1:30 AM
*/
interface ILogDecorator {
/**
* 装饰日志内容
*
* @param logContent 日志内容
* @param logType 日志类型
* @return 装饰后的日志
*/
fun decorate(logContent: String, logType: LogType?): SpannableString
}
/**
* 日志类型
*/
enum class LogType {
/**
* 普通日志
*/
NORMAL,
/**
* 成功日志
*/
SUCCESS,
/**
* 出错日志
*/
ERROR,
/**
* 警告日志
*/
WARNING,
/**
* 自定义等级日志
*/
CUSTOM
}
init {
movementMethod = ScrollingMovementMethod()
}
}