FlutterXLogPlugin.ets
2.3 KB
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
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
} from '@ohos/flutter_ohos';
import common from '@ohos.app.ability.common';
import { AppednerMode, CompressLevel, CompressMode, XLog } from '../xlog/XLog';
import { Log } from '../xlog/Log';
/** MMKVPlugin **/
export default class FlutterXLogPlugin implements FlutterPlugin, MethodCallHandler {
private channel: MethodChannel | null = null;
private context: common.Context | null = null;
private xlog: XLog = new XLog();
constructor(context?: common.Context) {
// hilog.info(0x0000, 'start', 'MMKVPlugin constructor: %{public}s', context);
if (context) {
this.context = context;
}
}
getUniqueClassName(): string {
return "FlutterXLogPlugin"
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_xlog");
this.channel.setMethodCallHandler(this)
this.context = binding.getApplicationContext();
// hilog.info(0x0000, 'start', 'MMKVPlugin onAttachedToEngine: %{public}s', this.context);
}
onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
}
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "open") {
let level = call.argument("level") as number;
let cacheDir = call.argument("cacheDir") as string;
let logDir = call.argument("logDir") as string;
let namePrefix = call.argument("namePrefix") as string;
let cacheDays = call.argument("cacheDays") as number;
let pubKey = call.argument("pubKey") as string;
let consoleLogOpen = call.argument("consoleLogOpen") as boolean;
Log.setLogImp(this.xlog);
Log.setLevel(level);
Log.setConsoleLogOpen(consoleLogOpen);
XLog.appenderOpen(level, AppednerMode.AppednerModeAsync, cacheDir, logDir, namePrefix, cacheDays, pubKey,
CompressMode.ZLIB_MODE, CompressLevel.COMPRESS_LEVEL0);
result.success(null);
} else if (call.method == "flush") {
Log.appenderFlush(true);
result.success(null);
} else if (call.method == "close") {
Log.appenderClose()
result.success(null);
} else {
result.notImplemented()
}
}
}