summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Logging/Logging.swift
blob: e21c5a5b24f94c4abf199ade88c6766ce44920e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
//  Logging.swift
//  MullvadVPN
//
//  Created by pronebird on 02/08/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import Logging

func initLoggingSystem(bundleIdentifier: String) {
    let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: ApplicationConfiguration.securityGroupIdentifier)!
    let logsDirectoryURL = containerURL.appendingPathComponent("Logs", isDirectory: true)
    let logFileName = "\(bundleIdentifier).log"
    let logFileURL = logsDirectoryURL.appendingPathComponent(logFileName)

    // Create Logs folder within container if it doesn't exist
    try? FileManager.default.createDirectory(at: logsDirectoryURL, withIntermediateDirectories: false, attributes: nil)

    // Rotate log
    let logRotationResult = LogRotation.rotateLog(logsDirectory: logsDirectoryURL, logFileName: logFileName)

    // 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)
    }

    // Configure Logging system
    LoggingSystem.bootstrap { (label) -> LogHandler in
        if streams.isEmpty {
            return SwiftLogNoOpLogHandler()
        } else {
            return CustomFormatLogHandler(label: label, streams: streams)
        }
    }

    if case .failure(let logRotationError) = logRotationResult {
        Logger(label: "LogRotation")
            .error(chainedError: logRotationError, message: "Failed to rotate log")
    }
}