2023-10-24 18:29:53 +03:30
|
|
|
import Foundation
|
2024-02-03 21:21:46 +03:30
|
|
|
import Combine
|
2024-02-04 02:50:54 +03:30
|
|
|
import Libcore
|
2023-10-24 18:29:53 +03:30
|
|
|
|
2024-02-04 02:50:54 +03:30
|
|
|
class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
|
2024-02-04 19:16:55 +03:30
|
|
|
static let shared: LogsEventHandler = LogsEventHandler()
|
2023-12-26 22:26:06 +03:30
|
|
|
static let name = "\(Bundle.main.serviceIdentifier)/service.logs"
|
2023-10-24 18:29:53 +03:30
|
|
|
|
|
|
|
|
private var channel: FlutterEventChannel?
|
2024-02-04 02:50:54 +03:30
|
|
|
|
|
|
|
|
private var commandClient: LibboxCommandClient?
|
|
|
|
|
private var events: FlutterEventSink?
|
|
|
|
|
private var maxLines: Int
|
|
|
|
|
private var logList: [String] = []
|
|
|
|
|
|
|
|
|
|
private var lock: NSLock = NSLock()
|
|
|
|
|
|
2023-10-24 18:29:53 +03:30
|
|
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
2024-02-04 19:16:55 +03:30
|
|
|
let instance = LogsEventHandler.shared
|
2024-02-04 02:50:54 +03:30
|
|
|
instance.channel = FlutterEventChannel(name: Self.name, binaryMessenger: registrar.messenger())
|
|
|
|
|
instance.channel?.setStreamHandler(instance)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(maxLines: Int = 32) {
|
|
|
|
|
self.maxLines = maxLines
|
|
|
|
|
super.init()
|
|
|
|
|
let opts = LibboxCommandClientOptions()
|
|
|
|
|
opts.command = LibboxCommandLog
|
|
|
|
|
opts.statusInterval = Int64(2 * NSEC_PER_SEC)
|
|
|
|
|
commandClient = LibboxCommandClient(self, options: opts)
|
|
|
|
|
try? commandClient?.connect()
|
2023-10-24 18:29:53 +03:30
|
|
|
}
|
|
|
|
|
|
2024-02-04 02:50:54 +03:30
|
|
|
deinit {
|
|
|
|
|
try? commandClient?.disconnect()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 18:29:53 +03:30
|
|
|
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
2024-02-04 02:50:54 +03:30
|
|
|
events(logList)
|
|
|
|
|
self.events = events
|
2023-10-24 18:29:53 +03:30
|
|
|
return nil
|
|
|
|
|
}
|
2024-02-04 02:50:54 +03:30
|
|
|
|
2023-10-24 18:29:53 +03:30
|
|
|
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
2024-02-04 02:50:54 +03:30
|
|
|
events = nil
|
2023-10-24 18:29:53 +03:30
|
|
|
return nil
|
|
|
|
|
}
|
2024-02-04 02:50:54 +03:30
|
|
|
|
|
|
|
|
func writeLog(_ message: String?) {
|
|
|
|
|
guard let message else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
lock.withLock { [self] in
|
|
|
|
|
if logList.count > maxLines {
|
|
|
|
|
logList.removeFirst()
|
|
|
|
|
}
|
|
|
|
|
logList.append(message)
|
|
|
|
|
DispatchQueue.main.async { [self] () in
|
|
|
|
|
events?(logList)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension LogsEventHandler {
|
|
|
|
|
public func clearLog() {}
|
|
|
|
|
public func connected() {}
|
|
|
|
|
public func disconnected(_ message: String?) {}
|
|
|
|
|
public func initializeClashMode(_ modeList: LibboxStringIteratorProtocol?, currentMode: String?) {}
|
|
|
|
|
public func updateClashMode(_ newMode: String?) {}
|
|
|
|
|
public func writeGroups(_ message: LibboxOutboundGroupIteratorProtocol?) {}
|
|
|
|
|
public func writeStatus(_ message: LibboxStatusMessage?) {}
|
2023-10-24 18:29:53 +03:30
|
|
|
}
|