From 14f1a82fede5289f2fd83f937e83012dd1bf1e23 Mon Sep 17 00:00:00 2001 From: Emils Date: Fri, 22 Sep 2023 14:28:07 +0200 Subject: [PATCH] Add a header to log files --- ios/MullvadLogging/Logging.swift | 3 ++- ios/MullvadVPN.xcodeproj/project.pbxproj | 2 ++ ios/MullvadVPN/AppDelegate.swift | 4 ++- ios/MullvadVPNTests/LoggingTests.swift | 31 ++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 ios/MullvadVPNTests/LoggingTests.swift diff --git a/ios/MullvadLogging/Logging.swift b/ios/MullvadLogging/Logging.swift index a7a19ce7e18d..109f320cf7b9 100644 --- a/ios/MullvadLogging/Logging.swift +++ b/ios/MullvadLogging/Logging.swift @@ -46,11 +46,12 @@ public struct LoggerBuilder { outputs.append(.osLogOutput(subsystem)) } - public func install() { + public func install(header: String) { LoggingSystem.bootstrap { label -> LogHandler in let logHandlers: [LogHandler] = outputs.map { output in switch output { case let .fileOutput(stream): + stream.write("\(header)\n") return CustomFormatLogHandler(label: label, streams: [stream]) case let .osLogOutput(subsystem): diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index f1ee25f1a1e3..8216b8f00478 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -2915,6 +2915,7 @@ 7A83A0C52B29A750008B5CE7 /* APIAccessMethodsTests.swift */, A900E9BD2ACC654100C95F67 /* APIProxy+Stubs.swift */, A9EC20E72A5D3A8C0040D56E /* CoordinatesTests.swift */, + 01168B192BBEE7F800D5F382 /* LoggingTests.swift */, 5896AE85246D6AD8005B36CB /* CustomDateComponentsFormattingTests.swift */, 58915D622A25F8400066445B /* DeviceCheckOperationTests.swift */, A900E9BB2ACC609200C95F67 /* DevicesProxy+Stubs.swift */, @@ -5060,6 +5061,7 @@ 7A3FD1B52AD4465A0042BEA6 /* AppMessageHandlerTests.swift in Sources */, 58C7A4702A8649ED0060C66F /* PingerTests.swift in Sources */, A97D25B22B0CB02D00946B2D /* ProtocolObfuscatorTests.swift in Sources */, + 01168B1A2BBEE7F800D5F382 /* LoggingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ios/MullvadVPN/AppDelegate.swift b/ios/MullvadVPN/AppDelegate.swift index a2239e3e7cb8..3a73dba46b20 100644 --- a/ios/MullvadVPN/AppDelegate.swift +++ b/ios/MullvadVPN/AppDelegate.swift @@ -357,9 +357,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD #if DEBUG loggerBuilder.addOSLogOutput(subsystem: ApplicationTarget.mainApp.bundleIdentifier) #endif - loggerBuilder.install() + loggerBuilder.install(header: "TODO: Add version info here") logger = Logger(label: "AppDelegate") + + loggerBuilder.logLevel = .debug } private func addApplicationNotifications(application: UIApplication) { diff --git a/ios/MullvadVPNTests/LoggingTests.swift b/ios/MullvadVPNTests/LoggingTests.swift new file mode 100644 index 000000000000..3c1abefbc108 --- /dev/null +++ b/ios/MullvadVPNTests/LoggingTests.swift @@ -0,0 +1,31 @@ +// +// LoggingTests.swift +// MullvadVPNTests +// +// Created by Emils on 04/04/2024. +// Copyright © 2024 Mullvad VPN AB. All rights reserved. +// + +import Foundation +import XCTest +@testable import MullvadLogging + +class MullvadLoggingTests: XCTestCase { + func testLogHeader() { + let dummySig = "test-sgi"; + let testFileName = "test" + let expectedHeader = "Header of a log file" + + var builder = LoggerBuilder() + try! builder.addFileOutput(securityGroupIdentifier: dummySig, basename: testFileName) + + builder.install(header: expectedHeader) + + let logFileUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: dummySig)!.appendingPathComponent("Logs", isDirectory: true).appendingPathComponent("\(testFileName).log", isDirectory: false) + + let contents = String(decoding: try! Data(contentsOf: logFileUrl), as: UTF8.self) + + XCTAssert(contents.hasPrefix(expectedHeader)) + XCTAssertEqual("\(expectedHeader)\n", contents) + } +}