Fix iOS Logger
This commit is contained in:
@@ -2,66 +2,41 @@ import Foundation
|
||||
import Combine
|
||||
import Libcore
|
||||
|
||||
class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
|
||||
static let shared: LogsEventHandler = LogsEventHandler()
|
||||
class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler {
|
||||
static let name = "\(Bundle.main.serviceIdentifier)/service.logs"
|
||||
|
||||
private var channel: FlutterEventChannel?
|
||||
|
||||
private var commandClient: LibboxCommandClient?
|
||||
private var commandClient: CommandClient?
|
||||
private var events: FlutterEventSink?
|
||||
private var maxLines: Int
|
||||
private var logList: [String] = []
|
||||
|
||||
private var lock: NSLock = NSLock()
|
||||
private var channel: FlutterEventChannel?
|
||||
private var cancellable: AnyCancellable?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = LogsEventHandler.shared
|
||||
instance.channel = FlutterEventChannel(name: Self.name, binaryMessenger: registrar.messenger())
|
||||
let instance = LogsEventHandler()
|
||||
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()
|
||||
}
|
||||
|
||||
deinit {
|
||||
try? commandClient?.disconnect()
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
events(logList)
|
||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
||||
self.events = events
|
||||
commandClient = CommandClient(.log)
|
||||
commandClient?.connect()
|
||||
cancellable = commandClient?.$logList.sink{ [self] logs in
|
||||
events(logs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
commandClient?.disconnect()
|
||||
cancellable?.cancel()
|
||||
events = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
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() {}
|
||||
@@ -71,3 +46,4 @@ extension LogsEventHandler {
|
||||
public func writeGroups(_ message: LibboxOutboundGroupIteratorProtocol?) {}
|
||||
public func writeStatus(_ message: LibboxStatusMessage?) {}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -86,7 +86,7 @@ class VPNManager: ObservableObject {
|
||||
do {
|
||||
try await loadVPNPreference()
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ class VPNManager: ObservableObject {
|
||||
try await newManager.loadFromPreferences()
|
||||
self.manager = newManager
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ class VPNManager: ObservableObject {
|
||||
try await manager.saveToPreferences()
|
||||
try await manager.loadFromPreferences()
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ class VPNManager: ObservableObject {
|
||||
}
|
||||
try await self?.loadVPNPreference()
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}.store(in: &cancelBag)
|
||||
@@ -188,7 +188,7 @@ class VPNManager: ObservableObject {
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ class VPNManager: ObservableObject {
|
||||
"DisableMemoryLimit": (disableMemoryLimit ? "YES" : "NO") as NSString,
|
||||
])
|
||||
} catch {
|
||||
LogsEventHandler.shared.writeLog(error.localizedDescription)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
connectTime = .now
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user