Refactor: extract native log package

This commit is contained in:
Ming Ming 2023-09-01 12:27:23 +08:00
parent dbea0e0a55
commit 6809503e0b
21 changed files with 241 additions and 46 deletions

View file

@ -1025,6 +1025,13 @@ packages:
relative: true
source: path
version: "0.0.1"
np_platform_log:
dependency: "direct main"
description:
path: "../np_platform_log"
relative: true
source: path
version: "0.0.1"
np_platform_permission:
dependency: "direct main"
description:

View file

@ -113,6 +113,8 @@ dependencies:
path: ../np_platform_image_processor
np_platform_lock:
path: ../np_platform_lock
np_platform_log:
path: ../np_platform_log
np_platform_permission:
path: ../np_platform_permission
np_platform_raw_image:

30
np_platform_log/.gitignore vendored Normal file
View file

@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/

30
np_platform_log/.metadata Normal file
View file

@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
version:
revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
channel: stable
project_type: plugin
# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: android
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
# User provided section
# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'

View file

@ -0,0 +1 @@
include: package:np_lints/np.yaml

9
np_platform_log/android/.gitignore vendored Normal file
View file

@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx

View file

@ -0,0 +1,52 @@
group 'com.nkming.nc_photos.np_platform_log'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.8.20'
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
namespace 'com.nkming.nc_photos.np_platform_log'
compileSdk 31
defaultConfig {
minSdk 21
}
buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "androidx.annotation:annotation:1.6.0"
}

View file

@ -0,0 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true

View file

@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View file

@ -0,0 +1 @@
rootProject.name = 'np_platform_log'

View file

@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nkming.nc_photos.np_platform_log">
</manifest>

View file

@ -0,0 +1,7 @@
package com.nkming.nc_photos.np_platform_log
internal interface K {
companion object {
const val LIB_ID = "com.nkming.nc_photos.np_platform_log"
}
}

View file

@ -0,0 +1,34 @@
package com.nkming.nc_photos.np_platform_log
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
internal 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)
}
}
else -> result.notImplemented()
}
}
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

@ -0,0 +1,26 @@
package com.nkming.nc_photos.np_platform_log
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodChannel
class NpPlatformLogPlugin : FlutterPlugin {
override fun onAttachedToEngine(
@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding
) {
val logcatChannelHandler = LogcatChannelHandler()
logcatMethodChannel = MethodChannel(
flutterPluginBinding.binaryMessenger,
LogcatChannelHandler.METHOD_CHANNEL
)
logcatMethodChannel.setMethodCallHandler(logcatChannelHandler)
}
override fun onDetachedFromEngine(
@NonNull binding: FlutterPlugin.FlutterPluginBinding
) {
logcatMethodChannel.setMethodCallHandler(null)
}
private lateinit var logcatMethodChannel: MethodChannel
}

View file

@ -0,0 +1,3 @@
library np_platform_log;
export 'src/platform_log.dart';

View file

@ -0,0 +1 @@
const libId = "com.nkming.nc_photos.np_platform_log";

View file

@ -1,9 +1,10 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:nc_photos_plugin/src/k.dart' as k;
import 'package:np_platform_log/src/k.dart' as k;
class Logcat {
class PlatformLog {
/// Get the current native logs
static Future<String> dump() async {
return await _methodChannel.invokeMethod("dump");
}

View file

@ -0,0 +1,24 @@
name: np_platform_log
description: Retrieve logs from native platforms
version: 0.0.1
homepage:
publish_to: none
environment:
sdk: '>=2.19.6 <3.0.0'
flutter: ">=3.3.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
np_lints:
path: ../np_lints
flutter:
plugin:
platforms:
android:
package: com.nkming.nc_photos.np_platform_log
pluginClass: NpPlatformLogPlugin

View file

@ -1,34 +0,0 @@
package com.nkming.nc_photos.plugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
internal 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)
}
}
else -> result.notImplemented()
}
}
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

@ -53,13 +53,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
ContentUriChannelHandler(flutterPluginBinding.applicationContext)
)
val logcatChannelHandler = LogcatChannelHandler()
logcatMethodChannel = MethodChannel(
flutterPluginBinding.binaryMessenger,
LogcatChannelHandler.METHOD_CHANNEL
)
logcatMethodChannel.setMethodCallHandler(logcatChannelHandler)
val preferenceChannelHandler =
PreferenceChannelHandler(flutterPluginBinding.applicationContext)
preferenceMethodChannel = MethodChannel(
@ -76,7 +69,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
mediaStoreChannel.setStreamHandler(null)
mediaStoreMethodChannel.setMethodCallHandler(null)
contentUriMethodChannel.setMethodCallHandler(null)
logcatMethodChannel.setMethodCallHandler(null)
preferenceMethodChannel.setMethodCallHandler(null)
}
@ -129,7 +121,6 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
private lateinit var mediaStoreChannel: EventChannel
private lateinit var mediaStoreMethodChannel: MethodChannel
private lateinit var contentUriMethodChannel: MethodChannel
private lateinit var logcatMethodChannel: MethodChannel
private lateinit var preferenceMethodChannel: MethodChannel
private lateinit var mediaStoreChannelHandler: MediaStoreChannelHandler

View file

@ -2,7 +2,6 @@ library nc_photos_plugin;
export 'src/content_uri.dart';
export 'src/exception.dart';
export 'src/logcat.dart';
export 'src/media_store.dart';
export 'src/native_event.dart';
export 'src/notification.dart';