Skip to content

Commit 977059e

Browse files
committed
feat: decode 输出日志中加入别名,方便排查
1 parent 000aa61 commit 977059e

File tree

1 file changed

+127
-50
lines changed

1 file changed

+127
-50
lines changed

library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt

Lines changed: 127 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class SVGAParser(context: Context?) {
142142
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
143143
return
144144
}
145-
LogUtils.info(TAG, "================ decode from assets ================")
145+
LogUtils.info(TAG, "================ decode $name from assets ================")
146146
threadPoolExecutor.execute {
147147
try {
148148
mContext?.assets?.open(name)?.let {
@@ -151,54 +151,82 @@ class SVGAParser(context: Context?) {
151151
SVGACache.buildCacheKey("file:///assets/$name"),
152152
callback,
153153
true,
154-
playCallback
154+
playCallback,
155+
alias = name
155156
)
156157
}
157158
} catch (e: Exception) {
158-
this.invokeErrorCallback(e, callback)
159+
this.invokeErrorCallback(e, callback, name)
159160
}
160161
}
161162

162163
}
163164

164-
fun decodeFromURL(url: URL, callback: ParseCompletion?,playCallback: PlayCallback?=null): (() -> Unit)? {
165+
fun decodeFromURL(
166+
url: URL,
167+
callback: ParseCompletion?,
168+
playCallback: PlayCallback? = null
169+
): (() -> Unit)? {
165170
if (mContext == null) {
166171
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
167172
return null
168173
}
169-
LogUtils.info(TAG, "================ decode from url ================")
174+
val urlPath = url.toString()
175+
LogUtils.info(TAG, "================ decode from url: $urlPath ================")
170176
val cacheKey = SVGACache.buildCacheKey(url);
171177
return if (SVGACache.isCached(cacheKey)) {
172178
LogUtils.info(TAG, "this url cached")
173179
threadPoolExecutor.execute {
174180
if (SVGACache.isDefaultCache()) {
175-
this.decodeFromCacheKey(cacheKey, callback)
181+
this.decodeFromCacheKey(cacheKey, callback, alias = urlPath)
176182
} else {
177-
this._decodeFromCacheKey(cacheKey, callback,playCallback)
183+
this._decodeFromCacheKey(cacheKey, callback, playCallback, alias = urlPath)
178184
}
179185
}
180186
return null
181187
} else {
182188
LogUtils.info(TAG, "no cached, prepare to download")
183189
fileDownloader.resume(url, {
184190
if (SVGACache.isDefaultCache()) {
185-
this.decodeFromInputStream(it, cacheKey, callback,false,playCallback)
191+
this.decodeFromInputStream(
192+
it,
193+
cacheKey,
194+
callback,
195+
false,
196+
playCallback,
197+
alias = urlPath
198+
)
186199
} else {
187-
this._decodeFromInputStream(it, cacheKey, callback,playCallback)
200+
this._decodeFromInputStream(
201+
it,
202+
cacheKey,
203+
callback,
204+
playCallback,
205+
alias = urlPath
206+
)
188207
}
189208
}, {
190-
this.invokeErrorCallback(it, callback)
209+
LogUtils.error(
210+
TAG,
211+
"================ svga file: $url download fail ================"
212+
)
213+
this.invokeErrorCallback(it, callback, alias = urlPath)
191214
})
192215
}
193216
}
194217

195218
/**
196219
* 读取解析本地缓存的svga文件.
197220
*/
198-
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?,playCallback: PlayCallback?) {
221+
fun _decodeFromCacheKey(
222+
cacheKey: String,
223+
callback: ParseCompletion?,
224+
playCallback: PlayCallback?,
225+
alias: String? = null
226+
) {
199227
val svga = SVGACache.buildSvgaFile(cacheKey)
200228
try {
201-
LogUtils.info(TAG, "cache.binary change to entity")
229+
LogUtils.info(TAG, "$alias cache.binary change to entity")
202230
FileInputStream(svga).use { inputStream ->
203231
try {
204232
readAsBytes(inputStream)?.let { bytes ->
@@ -213,28 +241,33 @@ class SVGAParser(context: Context?) {
213241
)
214242
videoItem.prepare({
215243
LogUtils.info(TAG, "cache.prepare success")
216-
this.invokeCompleteCallback(videoItem, callback)
244+
this.invokeCompleteCallback(videoItem, callback, alias)
217245
},playCallback)
218-
} ?: doError("cache.inflate(bytes) cause exception", callback)
219-
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback)
246+
} ?: doError("cache.inflate(bytes) cause exception", callback, alias)
247+
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback, alias)
220248
} catch (e: Exception) {
221-
this.invokeErrorCallback(e, callback)
249+
this.invokeErrorCallback(e, callback, alias)
222250
} finally {
223251
inputStream.close()
224252
}
225253
}
226254
} catch (e: Exception) {
227-
LogUtils.error(TAG, "cache.binary change to entity fail", e)
255+
LogUtils.error(TAG, "$alias cache.binary change to entity fail", e)
228256
svga.takeIf { it.exists() }?.delete()
229-
this.invokeErrorCallback(e, callback)
257+
this.invokeErrorCallback(e, callback, alias)
230258
}
231259
}
232260

233-
fun doError(error: String, callback: ParseCompletion?) {
261+
private fun doError(
262+
error: String,
263+
callback: ParseCompletion?,
264+
alias: String?
265+
) {
234266
LogUtils.info(TAG, error)
235267
this.invokeErrorCallback(
236268
Exception(error),
237-
callback
269+
callback,
270+
alias
238271
)
239272
}
240273

@@ -245,11 +278,12 @@ class SVGAParser(context: Context?) {
245278
inputStream: InputStream,
246279
cacheKey: String,
247280
callback: ParseCompletion?,
248-
playCallback: PlayCallback?
281+
playCallback: PlayCallback?,
282+
alias: String? = null
249283
) {
250284
threadPoolExecutor.execute {
251285
try {
252-
LogUtils.info(TAG, "Input.binary change to entity")
286+
LogUtils.info(TAG, "================ $alias _inputStream change to entity ================")
253287
readAsBytes(inputStream)?.let { bytes ->
254288
threadPoolExecutor.execute {
255289
SVGACache.buildSvgaFile(cacheKey).let { cacheFile ->
@@ -262,9 +296,9 @@ class SVGAParser(context: Context?) {
262296
}
263297
}
264298
}
265-
LogUtils.info(TAG, "Input.inflate start")
299+
LogUtils.info(TAG, "inputStream inflate start")
266300
inflate(bytes)?.let { inflateBytes ->
267-
LogUtils.info(TAG, "Input.inflate success")
301+
LogUtils.info(TAG, "inputStream inflate success")
268302
val videoItem = SVGAVideoEntity(
269303
MovieEntity.ADAPTER.decode(inflateBytes),
270304
File(cacheKey),
@@ -276,16 +310,18 @@ class SVGAParser(context: Context?) {
276310
// 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
277311
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
278312
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
313+
LogUtils.info(TAG, "SVGAVideoEntity prepare start")
279314
videoItem.prepare({
280-
LogUtils.info(TAG, "cache.prepare success")
281-
this.invokeCompleteCallback(videoItem, callback)
315+
LogUtils.info(TAG, "SVGAVideoEntity prepare success")
316+
this.invokeCompleteCallback(videoItem, callback, alias)
282317
}, playCallback)
283-
} ?: doError("Input.inflate(bytes) cause exception", callback)
284-
} ?: doError("Input.readAsBytes(inputStream) cause exception", callback)
318+
} ?: doError("inflate(bytes) cause exception", callback, alias)
319+
} ?: doError("readAsBytes(inputStream) cause exception", callback, alias)
285320
} catch (e: Exception) {
286-
this.invokeErrorCallback(e, callback)
321+
this.invokeErrorCallback(e, callback, alias)
287322
} finally {
288323
inputStream.close()
324+
LogUtils.info(TAG, "================ $alias _inputStream change to entity end ================")
289325
}
290326
}
291327
}
@@ -295,13 +331,14 @@ class SVGAParser(context: Context?) {
295331
cacheKey: String,
296332
callback: ParseCompletion?,
297333
closeInputStream: Boolean = false,
298-
playCallback: PlayCallback?=null
334+
playCallback: PlayCallback?=null,
335+
alias: String? = null
299336
) {
300337
if (mContext == null) {
301338
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
302339
return
303340
}
304-
LogUtils.info(TAG, "================ decode from input stream ================")
341+
LogUtils.info(TAG, "================ decode $alias from input stream ================")
305342
threadPoolExecutor.execute {
306343
try {
307344
readAsBytes(inputStream)?.let { bytes ->
@@ -320,36 +357,41 @@ class SVGAParser(context: Context?) {
320357
}
321358
}
322359
}
323-
this.decodeFromCacheKey(cacheKey, callback)
360+
this.decodeFromCacheKey(cacheKey, callback, alias)
324361
} else {
325-
LogUtils.info(TAG, "decode from input stream, inflate start")
362+
LogUtils.info(TAG, "inflate start")
326363
inflate(bytes)?.let {
364+
LogUtils.info(TAG, "inflate complete")
327365
val videoItem = SVGAVideoEntity(
328366
MovieEntity.ADAPTER.decode(it),
329367
File(cacheKey),
330368
mFrameWidth,
331369
mFrameHeight
332370
)
371+
LogUtils.info(TAG, "SVGAVideoEntity prepare start")
333372
videoItem.prepare({
334-
LogUtils.info(TAG, "cache.prepare success")
335-
this.invokeCompleteCallback(videoItem, callback)
373+
LogUtils.info(TAG, "SVGAVideoEntity prepare success")
374+
this.invokeCompleteCallback(videoItem, callback, alias)
336375
},playCallback)
337376

338377
} ?: this.invokeErrorCallback(
339378
Exception("inflate(bytes) cause exception"),
340-
callback
379+
callback,
380+
alias
341381
)
342382
}
343383
} ?: this.invokeErrorCallback(
344384
Exception("readAsBytes(inputStream) cause exception"),
345-
callback
385+
callback,
386+
alias
346387
)
347388
} catch (e: java.lang.Exception) {
348-
this.invokeErrorCallback(e, callback)
389+
this.invokeErrorCallback(e, callback, alias)
349390
} finally {
350391
if (closeInputStream) {
351392
inputStream.close()
352393
}
394+
LogUtils.info(TAG, "================ decode $alias from input stream end ================")
353395
}
354396
}
355397
}
@@ -374,28 +416,45 @@ class SVGAParser(context: Context?) {
374416
* @deprecated from 2.4.0
375417
*/
376418
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)"))
377-
fun parse(inputStream: InputStream, cacheKey: String, callback: ParseCompletion?, closeInputStream: Boolean = false) {
419+
fun parse(
420+
inputStream: InputStream,
421+
cacheKey: String,
422+
callback: ParseCompletion?,
423+
closeInputStream: Boolean = false
424+
) {
378425
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream,null)
379426
}
380427

381-
private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
428+
private fun invokeCompleteCallback(
429+
videoItem: SVGAVideoEntity,
430+
callback: ParseCompletion?,
431+
alias: String?
432+
) {
382433
Handler(Looper.getMainLooper()).post {
383-
LogUtils.info(TAG, "================ parser complete ================")
434+
LogUtils.info(TAG, "================ $alias parser complete ================")
384435
callback?.onComplete(videoItem)
385436
}
386437
}
387438

388-
private fun invokeErrorCallback(e: java.lang.Exception, callback: ParseCompletion?) {
439+
private fun invokeErrorCallback(
440+
e: Exception,
441+
callback: ParseCompletion?,
442+
alias: String?
443+
) {
389444
e.printStackTrace()
390-
LogUtils.error(TAG, "================ parser error ================")
391-
LogUtils.error(TAG, "error", e)
445+
LogUtils.error(TAG, "================ $alias parser error ================")
446+
LogUtils.error(TAG, "$alias parse error", e)
392447
Handler(Looper.getMainLooper()).post {
393448
callback?.onError()
394449
}
395450
}
396451

397-
private fun decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
398-
LogUtils.info(TAG, "================ decode from cache ================")
452+
private fun decodeFromCacheKey(
453+
cacheKey: String,
454+
callback: ParseCompletion?,
455+
alias: String?
456+
) {
457+
LogUtils.info(TAG, "================ decode $alias from cache ================")
399458
LogUtils.debug(TAG, "decodeFromCacheKey called with cacheKey : $cacheKey")
400459
if (mContext == null) {
401460
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
@@ -408,7 +467,16 @@ class SVGAParser(context: Context?) {
408467
LogUtils.info(TAG, "binary change to entity")
409468
FileInputStream(binaryFile).use {
410469
LogUtils.info(TAG, "binary change to entity success")
411-
this.invokeCompleteCallback(SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
470+
this.invokeCompleteCallback(
471+
SVGAVideoEntity(
472+
MovieEntity.ADAPTER.decode(it),
473+
cacheDir,
474+
mFrameWidth,
475+
mFrameHeight
476+
),
477+
callback,
478+
alias
479+
)
412480
}
413481

414482
} catch (e: Exception) {
@@ -434,20 +502,29 @@ class SVGAParser(context: Context?) {
434502
byteArrayOutputStream.toString().let {
435503
JSONObject(it).let {
436504
LogUtils.info(TAG, "spec change to entity success")
437-
this.invokeCompleteCallback(SVGAVideoEntity(it, cacheDir, mFrameWidth, mFrameHeight), callback)
505+
this.invokeCompleteCallback(
506+
SVGAVideoEntity(
507+
it,
508+
cacheDir,
509+
mFrameWidth,
510+
mFrameHeight
511+
),
512+
callback,
513+
alias
514+
)
438515
}
439516
}
440517
}
441518
}
442519
} catch (e: Exception) {
443-
LogUtils.error(TAG, "spec change to entity fail", e)
520+
LogUtils.error(TAG, "$alias movie.spec change to entity fail", e)
444521
cacheDir.delete()
445522
jsonFile.delete()
446523
throw e
447524
}
448525
}
449526
} catch (e: Exception) {
450-
this.invokeErrorCallback(e, callback)
527+
this.invokeErrorCallback(e, callback, alias)
451528
}
452529
}
453530

0 commit comments

Comments
 (0)