Dump system logcat

This commit is contained in:
Ming Ming 2022-05-23 22:18:51 +08:00
parent d3c62611fa
commit 4295a011d3
4 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,32 @@
package com.nkming.nc_photos.plugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class LogcatChannelHandler : MethodChannel.MethodCallHandler {
companion object {
const val METHOD_CHANNEL = "${K.LIB_ID}/logcat_method"
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"dump" -> {
try {
dump(result)
} catch (e: Throwable) {
result.error("systemException", e.toString(), null)
}
}
}
}
private fun dump(result: MethodChannel.Result) {
val logs = StringBuilder()
val process = Runtime.getRuntime().exec("logcat -d")
process.inputStream.bufferedReader().use {
while (it.readLine()?.also(logs::appendLine) != null) {
}
}
result.success(logs.toString())
}
}

View file

@ -98,6 +98,13 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
PermissionChannelHandler.METHOD_CHANNEL
)
permissionMethodChannel.setMethodCallHandler(permissionChannelHandler)
val logcatChannelHandler = LogcatChannelHandler()
logcatMethodChannel = MethodChannel(
flutterPluginBinding.binaryMessenger,
LogcatChannelHandler.METHOD_CHANNEL
)
logcatMethodChannel.setMethodCallHandler(logcatChannelHandler)
}
override fun onDetachedFromEngine(
@ -114,6 +121,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
contentUriMethodChannel.setMethodCallHandler(null)
permissionChannel.setStreamHandler(null)
permissionMethodChannel.setMethodCallHandler(null)
logcatMethodChannel.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
@ -200,6 +208,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
private lateinit var contentUriMethodChannel: MethodChannel
private lateinit var permissionChannel: EventChannel
private lateinit var permissionMethodChannel: MethodChannel
private lateinit var logcatMethodChannel: MethodChannel
private lateinit var lockChannelHandler: LockChannelHandler
private lateinit var mediaStoreChannelHandler: MediaStoreChannelHandler

View file

@ -4,6 +4,7 @@ export 'src/content_uri.dart';
export 'src/exception.dart';
export 'src/image_processor.dart';
export 'src/lock.dart';
export 'src/logcat.dart';
export 'src/media_store.dart';
export 'src/native_event.dart';
export 'src/notification.dart';

View file

@ -0,0 +1,12 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:nc_photos_plugin/src/k.dart' as k;
class Logcat {
static Future<String> dump() async {
return await _methodChannel.invokeMethod("dump");
}
static const _methodChannel = MethodChannel("${k.libId}/logcat_method");
}