Log OpenVPN tunnel via SwiftyBeaver file

This way debug log is updated without manual flush.

Useful for immediate access.
This commit is contained in:
Davide De Rosa 2022-03-12 08:16:25 +01:00
parent 3807b4754b
commit ff235e2b96
1 changed files with 22 additions and 26 deletions

View File

@ -66,8 +66,8 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
/// The log separator between sessions. /// The log separator between sessions.
public var logSeparator = "--- EOF ---" public var logSeparator = "--- EOF ---"
/// The maximum number of lines in the log. /// The maximum size of the log.
public var maxLogLines = 1000 public var maxLogSize = 20000
/// The log level when `OpenVPNTunnelProvider.Configuration.shouldDebug` is enabled. /// The log level when `OpenVPNTunnelProvider.Configuration.shouldDebug` is enabled.
public var debugLogLevel: SwiftyBeaver.Level = .debug public var debugLogLevel: SwiftyBeaver.Level = .debug
@ -100,8 +100,8 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
// MARK: Constants // MARK: Constants
private let memoryLog = MemoryDestination() private var logFile: FileDestination?
private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility) private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility)
private let prngSeedLength = 64 private let prngSeedLength = 64
@ -173,24 +173,16 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
} }
// prepare for logging (append) // 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( configureLogging(
debug: cfg.shouldDebug, debug: cfg.shouldDebug,
customFormat: cfg.debugLogFormat customFormat: cfg.debugLogFormat
) )
// logging only ACTIVE from now on // logging only ACTIVE from now on
log.info("")
log.info(logSeparator)
log.info("")
// override library configuration // override library configuration
CoreConfiguration.masksPrivateData = cfg.masksPrivateData CoreConfiguration.masksPrivateData = cfg.masksPrivateData
if let versionIdentifier = cfg.versionIdentifier { if let versionIdentifier = cfg.versionIdentifier {
@ -832,19 +824,23 @@ extension OpenVPNTunnelProvider {
console.format = logFormat console.format = logFormat
log.addDestination(console) log.addDestination(console)
} }
let memory = memoryLog let file = FileDestination(logFileURL: cfg.urlForDebugLog)
memory.minLevel = logLevel file.minLevel = logLevel
memory.format = logFormat file.format = logFormat
memory.maxLines = maxLogLines file.logFileMaxSize = maxLogSize
log.addDestination(memoryLog) log.addDestination(file)
logFile = file
} }
private func flushLog() { private func flushLog() {
log.debug("Flushing log...") 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() { private func logCurrentSSID() {