2021-01-01 10:13:41 +00:00
|
|
|
//
|
2022-06-23 21:31:01 +00:00
|
|
|
// DebugLog+Extensions.swift
|
2021-01-01 10:13:41 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
2022-04-12 13:09:14 +00:00
|
|
|
// Created by Davide De Rosa on 3/14/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2022-04-19 12:18:26 +00:00
|
|
|
import Foundation
|
2023-12-16 19:58:54 +00:00
|
|
|
#if os(iOS) || os(tvOS)
|
2021-01-01 10:13:41 +00:00
|
|
|
import UIKit
|
2022-05-17 09:32:19 +00:00
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
#endif
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
extension DebugLog {
|
2024-01-07 11:11:16 +00:00
|
|
|
public static func decoratedMetadataString(_ appName: String, _ appVersion: String) -> String {
|
2022-05-17 09:32:19 +00:00
|
|
|
let osVersion: String
|
|
|
|
let deviceType: String?
|
|
|
|
|
2024-01-07 11:11:16 +00:00
|
|
|
#if os(iOS) || os(tvOS)
|
2022-04-19 12:18:26 +00:00
|
|
|
let device: UIDevice = .current
|
2022-05-17 09:32:19 +00:00
|
|
|
osVersion = "\(device.systemName) \(device.systemVersion)"
|
2024-01-07 11:11:16 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2023-04-01 14:24:27 +00:00
|
|
|
deviceType = "\(device.model) (Catalyst)"
|
2024-01-07 11:11:16 +00:00
|
|
|
#else
|
2022-10-04 05:26:50 +00:00
|
|
|
deviceType = "\(device.model) (\(device.userInterfaceIdiom.debugDescription))"
|
2024-01-07 11:11:16 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2022-05-17 09:32:19 +00:00
|
|
|
let os = ProcessInfo().operatingSystemVersion
|
|
|
|
osVersion = "macOS \(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"
|
|
|
|
deviceType = nil
|
2024-01-07 11:11:16 +00:00
|
|
|
#endif
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2022-05-17 09:32:19 +00:00
|
|
|
var metadata = [
|
2022-04-19 12:18:26 +00:00
|
|
|
"App: \(appName) \(appVersion)",
|
2022-05-17 09:32:19 +00:00
|
|
|
"OS: \(osVersion)"
|
2022-04-19 12:18:26 +00:00
|
|
|
]
|
2022-05-17 09:32:19 +00:00
|
|
|
if let deviceType = deviceType {
|
|
|
|
metadata.append("Device: \(deviceType)")
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2024-01-07 11:11:16 +00:00
|
|
|
return metadata.joined(separator: "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
public func decoratedString(_ appName: String, _ appVersion: String) -> String {
|
|
|
|
[Self.decoratedMetadataString(appName, appVersion), content]
|
|
|
|
.joined(separator: "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
public static func decoratedMetadataData(_ appName: String, _ appVersion: String) -> Data {
|
|
|
|
guard let data = decoratedMetadataString(appName, appVersion).data(using: .utf8) else {
|
|
|
|
assertionFailure("Could not encode log metadata to UTF8?")
|
|
|
|
return Data()
|
|
|
|
}
|
|
|
|
return data
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public func decoratedData(_ appName: String, _ appVersion: String) -> Data {
|
|
|
|
guard let data = decoratedString(appName, appVersion).data(using: .utf8) else {
|
|
|
|
assertionFailure("Could not encode log metadata to UTF8?")
|
|
|
|
return Data()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 05:26:50 +00:00
|
|
|
|
2022-10-08 07:20:04 +00:00
|
|
|
#if canImport(UIKit)
|
2022-10-04 05:26:50 +00:00
|
|
|
private extension UIUserInterfaceIdiom {
|
|
|
|
var debugDescription: String {
|
|
|
|
switch self {
|
|
|
|
case .phone:
|
|
|
|
return "Phone"
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-04 05:26:50 +00:00
|
|
|
case .pad:
|
|
|
|
return "Pad"
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-04 05:26:50 +00:00
|
|
|
case .mac:
|
|
|
|
return "Mac"
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-04 05:26:50 +00:00
|
|
|
default:
|
|
|
|
return "Other"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 07:20:04 +00:00
|
|
|
#endif
|