From ff235e2b96df6e13721aa7981b1b4b8b01adab43 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 12 Mar 2022 08:16:25 +0100 Subject: [PATCH] Log OpenVPN tunnel via SwiftyBeaver file This way debug log is updated without manual flush. Useful for immediate access. --- .../OpenVPNTunnelProvider.swift | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift index c106dfd..79aa4db 100644 --- a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift +++ b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift @@ -66,8 +66,8 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider { /// The log separator between sessions. public var logSeparator = "--- EOF ---" - /// The maximum number of lines in the log. - public var maxLogLines = 1000 + /// The maximum size of the log. + public var maxLogSize = 20000 /// The log level when `OpenVPNTunnelProvider.Configuration.shouldDebug` is enabled. public var debugLogLevel: SwiftyBeaver.Level = .debug @@ -100,8 +100,8 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider { // MARK: Constants - private let memoryLog = MemoryDestination() - + private var logFile: FileDestination? + private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility) private let prngSeedLength = 64 @@ -173,24 +173,16 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider { } // prepare for logging (append) - if let content = cfg.debugLog { - var existingLog = content.components(separatedBy: "\n") - if let i = existingLog.firstIndex(of: logSeparator) { - existingLog.removeFirst(i + 2) - } - - existingLog.append("") - existingLog.append(logSeparator) - existingLog.append("") - memoryLog.start(with: existingLog) - } configureLogging( debug: cfg.shouldDebug, customFormat: cfg.debugLogFormat ) - + // logging only ACTIVE from now on - + log.info("") + log.info(logSeparator) + log.info("") + // override library configuration CoreConfiguration.masksPrivateData = cfg.masksPrivateData if let versionIdentifier = cfg.versionIdentifier { @@ -832,19 +824,23 @@ extension OpenVPNTunnelProvider { console.format = logFormat log.addDestination(console) } - - let memory = memoryLog - memory.minLevel = logLevel - memory.format = logFormat - memory.maxLines = maxLogLines - log.addDestination(memoryLog) + + let file = FileDestination(logFileURL: cfg.urlForDebugLog) + file.minLevel = logLevel + file.format = logFormat + file.logFileMaxSize = maxLogSize + log.addDestination(file) + + logFile = file } private func flushLog() { log.debug("Flushing log...") - if let url = cfg.urlForDebugLog { - memoryLog.flush(to: url) - } + + // XXX: should enforce SwiftyBeaver flush? +// if let url = cfg.urlForDebugLog { +// memoryLog.flush(to: url) +// } } private func logCurrentSSID() {