diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-08-30 13:29:54 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-08-30 13:29:54 +0200 |
| commit | 7eaebdb1ce52d602363735d3e9f7f19616bd7f46 (patch) | |
| tree | 9dfc7caf1b33873df91a42b1536fff37fd82875a | |
| parent | eb3fe0d9a92109ce489c7a9e8cdd1e1bdf41c060 (diff) | |
| parent | c24eda32eb54ce29b842c7e0fa6b448a12e21663 (diff) | |
| download | mullvadvpn-7eaebdb1ce52d602363735d3e9f7f19616bd7f46.tar.xz mullvadvpn-7eaebdb1ce52d602363735d3e9f7f19616bd7f46.zip | |
Merge branch 'add-oslog'
| -rw-r--r-- | ios/MullvadVPN.xcodeproj/project.pbxproj | 6 | ||||
| -rw-r--r-- | ios/MullvadVPN/Logging/Logging.swift | 19 | ||||
| -rw-r--r-- | ios/MullvadVPN/Logging/OSLogHandler.swift | 88 |
3 files changed, 106 insertions, 7 deletions
diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 5b7c8e0bfd..df43bbe265 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ 581503A724D6F4AE00C9C50E /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 581503A524D6F4AE00C9C50E /* Logging.swift */; }; 581CBCEE229826FD00727D7F /* StaticTableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 581CBCED229826FD00727D7F /* StaticTableViewDataSource.swift */; }; 581FC4FA2695ACE100AA97BA /* Account.strings in Resources */ = {isa = PBXBuildFile; fileRef = 581FC4F82695ACE100AA97BA /* Account.strings */; }; + 5823FA5026CA690600283BF8 /* OSLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */; }; 58293FAE2510CA58005D0BB5 /* ProblemReportViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FAC2510CA58005D0BB5 /* ProblemReportViewController.swift */; }; 58293FB125124117005D0BB5 /* CustomTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FB025124117005D0BB5 /* CustomTextField.swift */; }; 58293FB3251241B4005D0BB5 /* CustomTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FB2251241B3005D0BB5 /* CustomTextView.swift */; }; @@ -213,6 +214,7 @@ 58CE5E81224146470008646E /* PacketTunnel.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 58CE5E79224146470008646E /* PacketTunnel.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 58D0C79E23F1CEBA00FE9BA7 /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58D0C79D23F1CEBA00FE9BA7 /* SnapshotHelper.swift */; }; 58D0C7A223F1CECF00FE9BA7 /* MullvadVPNScreenshots.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58D0C7A023F1CECF00FE9BA7 /* MullvadVPNScreenshots.swift */; }; + 58D67A0A26D7AE3300557C3C /* OSLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */; }; 58DF28A52417CB4B00E836B0 /* AppStorePaymentManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58DF28A42417CB4B00E836B0 /* AppStorePaymentManager.swift */; }; 58E6771F24ADFE7800AA26E7 /* SettingsNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58E6771E24ADFE7800AA26E7 /* SettingsNavigationController.swift */; }; 58EF580B25D69D7A00AEBA94 /* ProblemReportSubmissionOverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58EF580A25D69D7A00AEBA94 /* ProblemReportSubmissionOverlayView.swift */; }; @@ -336,6 +338,7 @@ 581503A524D6F4AE00C9C50E /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; }; 581CBCED229826FD00727D7F /* StaticTableViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StaticTableViewDataSource.swift; sourceTree = "<group>"; }; 581FC4F92695ACE100AA97BA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Account.strings; sourceTree = "<group>"; }; + 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLogHandler.swift; sourceTree = "<group>"; }; 58293FAC2510CA58005D0BB5 /* ProblemReportViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProblemReportViewController.swift; sourceTree = "<group>"; }; 58293FB025124117005D0BB5 /* CustomTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextField.swift; sourceTree = "<group>"; }; 58293FB2251241B3005D0BB5 /* CustomTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextView.swift; sourceTree = "<group>"; }; @@ -569,6 +572,7 @@ 58C3B06824EAA25000C0348E /* StringStreamIterator.swift */, 5815039C24D6ECE600C9C50E /* TextFileOutputStream.swift */, 58141EC824DAC0ED0013F79C /* TextFileStream.swift */, + 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */, ); path = Logging; sourceTree = "<group>"; @@ -1220,6 +1224,7 @@ 58B43C1925F77DB60002C8C3 /* ConnectMainContentView.swift in Sources */, 58561C99239A5D1500BD6B5E /* IPEndpoint.swift in Sources */, 58FD5BF22424F7D700112C88 /* UserInterfaceInteractionRestriction.swift in Sources */, + 5823FA5026CA690600283BF8 /* OSLogHandler.swift in Sources */, 5811DE50239014550011EB53 /* NEVPNStatus+Debug.swift in Sources */, 58141EC924DAC0ED0013F79C /* TextFileStream.swift in Sources */, 58C3A4B222456F1B00340BDB /* AccountInputGroupView.swift in Sources */, @@ -1275,6 +1280,7 @@ 584E96BD240FD4DA00D3334F /* Location.swift in Sources */, 58FAEDF8245088E100CB0F5B /* Keychain.swift in Sources */, 58F840B32464491D0044E708 /* ChainedError.swift in Sources */, + 58D67A0A26D7AE3300557C3C /* OSLogHandler.swift in Sources */, 580EE20A24B3224200F9D8A1 /* RetryOperation.swift in Sources */, 58561C9A239A5D1500BD6B5E /* IPEndpoint.swift in Sources */, 580EE22524B3243100F9D8A1 /* AsyncBlockOperation.swift in Sources */, diff --git a/ios/MullvadVPN/Logging/Logging.swift b/ios/MullvadVPN/Logging/Logging.swift index e21c5a5b24..b96d9dc08d 100644 --- a/ios/MullvadVPN/Logging/Logging.swift +++ b/ios/MullvadVPN/Logging/Logging.swift @@ -24,11 +24,6 @@ func initLoggingSystem(bundleIdentifier: String) { // Create an array of log output streams var streams: [TextOutputStream] = [] - #if DEBUG - // Add standard output logging in debug - streams.append(TextFileOutputStream.standardOutputStream()) - #endif - // Create output stream to file if let fileLogStream = TextFileOutputStream(fileURL: logFileURL, createFile: true) { streams.append(fileLogStream) @@ -36,10 +31,20 @@ func initLoggingSystem(bundleIdentifier: String) { // Configure Logging system LoggingSystem.bootstrap { (label) -> LogHandler in - if streams.isEmpty { + var logHandlers: [LogHandler] = [] + + #if DEBUG + logHandlers.append(OSLogHandler(subsystem: bundleIdentifier, category: label)) + #endif + + if !streams.isEmpty { + logHandlers.append(CustomFormatLogHandler(label: label, streams: streams)) + } + + if logHandlers.isEmpty { return SwiftLogNoOpLogHandler() } else { - return CustomFormatLogHandler(label: label, streams: streams) + return MultiplexLogHandler(logHandlers) } } diff --git a/ios/MullvadVPN/Logging/OSLogHandler.swift b/ios/MullvadVPN/Logging/OSLogHandler.swift new file mode 100644 index 0000000000..34fd449c2f --- /dev/null +++ b/ios/MullvadVPN/Logging/OSLogHandler.swift @@ -0,0 +1,88 @@ +// +// OSLogHandler.swift +// OSLogHandler +// +// Created by pronebird on 16/08/2021. +// Copyright © 2021 Mullvad VPN AB. All rights reserved. +// + +import Foundation +import Logging +import os + +struct OSLogHandler: LogHandler { + var metadata: Logging.Logger.Metadata = [:] + var logLevel: Logging.Logger.Level = .debug + + private let label: String + private let osLog: OSLog + + private struct RegistryKey: Hashable { + let subsystem: String + let category: String + } + + private static var osLogRegistry: [RegistryKey: OSLog] = [:] + private static let registryLock = NSLock() + + private static func getOSLog(subsystem: String, category: String) -> OSLog { + return registryLock.withCriticalBlock { + let key = RegistryKey(subsystem: subsystem, category: category) + if let log = osLogRegistry[key] { + return log + } else { + let newLog = OSLog(subsystem: subsystem, category: category) + osLogRegistry[key] = newLog + return newLog + } + } + } + + init(subsystem: String, category: String) { + self.label = category + self.osLog = OSLogHandler.getOSLog(subsystem: subsystem, category: category) + } + + subscript(metadataKey metadataKey: String) -> Logging.Logger.Metadata.Value? { + get { + return metadata[metadataKey] + } + set(newValue) { + metadata[metadataKey] = newValue + } + } + + func log(level: Logging.Logger.Level, + message: Logging.Logger.Message, + metadata: Logging.Logger.Metadata?, + source: String, + file: String, + function: String, + line: UInt) + { + let mergedMetadata = self.metadata.merging(metadata ?? [:]) { (lhs, rhs) -> Logging.Logger.MetadataValue in + return rhs + } + let prettyMetadata = Self.formatMetadata(mergedMetadata) + let logMessage = prettyMetadata.isEmpty ? message : "\(prettyMetadata) \(message)" + + os_log("%{public}s", log: osLog, type: level.osLogType, "\(logMessage)") + } + + private static func formatMetadata(_ metadata: Logging.Logger.Metadata) -> String { + return metadata.map { "\($0)=\($1)" }.joined(separator: " ") + } +} +extension Logging.Logger.Level { + var osLogType: OSLogType { + switch self { + case .trace, .debug: + // Console app does not output .debug logs, use .info instead. + return .info + case .info, .notice, .warning: + return .info + case .error, .critical: + return .error + } + } +} |
