passepartout-apple/PassepartoutLibrary/Sources/PassepartoutVPN/Extensions/DebugLog+Extensions.swift

104 lines
3.0 KiB
Swift
Raw Normal View History

//
2022-06-23 21:31:01 +00:00
// DebugLog+Extensions.swift
// 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.
//
// 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/>.
//
import Foundation
2023-12-16 19:58:54 +00:00
#if os(iOS) || os(tvOS)
import UIKit
#else
import AppKit
#endif
2022-04-12 13:09:14 +00:00
extension DebugLog {
public static func decoratedMetadataString(_ appName: String, _ appVersion: String) -> String {
let osVersion: String
let deviceType: String?
#if os(iOS) || os(tvOS)
let device: UIDevice = .current
osVersion = "\(device.systemName) \(device.systemVersion)"
#if targetEnvironment(macCatalyst)
deviceType = "\(device.model) (Catalyst)"
#else
2022-10-04 05:26:50 +00:00
deviceType = "\(device.model) (\(device.userInterfaceIdiom.debugDescription))"
#endif
#else
let os = ProcessInfo().operatingSystemVersion
osVersion = "macOS \(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"
deviceType = nil
#endif
var metadata = [
"App: \(appName) \(appVersion)",
"OS: \(osVersion)"
]
if let deviceType = deviceType {
metadata.append("Device: \(deviceType)")
}
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
}
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()
}
return data
}
}
2022-10-04 05:26:50 +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"
}
}
}
#endif