Skip to content

Commit

Permalink
Add a header to log files
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Sep 22, 2023
1 parent e4b2ada commit 14f1a82
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ios/MullvadLogging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
};
Expand Down
4 changes: 3 additions & 1 deletion ios/MullvadVPN/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions ios/MullvadVPNTests/LoggingTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 14f1a82

Please sign in to comment.