passepartout-apple/Passepartout/Library/Sources/CommonLibrary/CommonLibrary.swift

101 lines
3.3 KiB
Swift
Raw Normal View History

2024-09-23 13:02:26 +00:00
//
// CommonLibrary.swift
// Passepartout
//
// Created by Davide De Rosa on 8/31/24.
// Copyright (c) 2024 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
import PassepartoutKit
2024-10-01 13:45:25 +00:00
// TODO: #656, make non-static
2024-09-23 13:02:26 +00:00
public struct CommonLibrary {
private init() {
}
public static func configureLogging(to url: URL, parameters: Constants.Log) {
pp_log(.common, .debug, "Log to: \(url)")
2024-09-23 13:02:26 +00:00
PassepartoutConfiguration.shared.setLocalLogger(options: .init(
url: url,
maxNumberOfLines: parameters.maxNumberOfLines,
maxLevel: parameters.maxLevel,
mapper: parameters.formatter.formattedLine
))
if UserDefaults.group.bool(forKey: AppPreference.logsPrivateData.key) {
PassepartoutConfiguration.shared.logsAddresses = true
PassepartoutConfiguration.shared.logsModules = true
}
if let maxAge = parameters.maxAge {
purgeLogs(at: url, beyond: maxAge)
}
}
public static func currentLog(parameters: Constants.Log) -> [String] {
PassepartoutConfiguration.shared.currentLogLines(
sinceLast: parameters.sinceLast,
maxLevel: parameters.maxLevel
)
.map(parameters.formatter.formattedLine)
}
public static func availableLogs(at url: URL) -> [Date: URL] {
let parent = url.deletingLastPathComponent()
let prefix = url.lastPathComponent
do {
let contents = try FileManager.default.contentsOfDirectory(at: parent, includingPropertiesForKeys: nil)
return contents.reduce(into: [:]) { found, item in
let filename = item.lastPathComponent
guard filename.hasPrefix(prefix) else {
return
}
guard let timestampString = filename.split(separator: ".").last,
let timestamp = TimeInterval(timestampString) else {
return
}
let date = Date(timeIntervalSince1970: timestamp)
found[date] = item
}
} catch {
return [:]
}
}
public static func flushLog() {
try? PassepartoutConfiguration.shared.saveLog()
}
}
private extension CommonLibrary {
static func purgeLogs(at url: URL, beyond maxAge: TimeInterval) {
let logs = availableLogs(at: url)
let minDate = Date().addingTimeInterval(-maxAge)
logs.forEach { date, url in
guard date >= minDate else {
try? FileManager.default.removeItem(at: url)
return
}
}
}
}