2022-10-10 18:21:12 +00:00
|
|
|
//
|
|
|
|
// Logging.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/10/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import OSLog
|
|
|
|
|
|
|
|
struct Logging {
|
|
|
|
private init() {}
|
|
|
|
|
|
|
|
static let general = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "General")
|
2022-12-04 17:08:22 +00:00
|
|
|
static let generalSignposter = OSSignposter(logger: general)
|
2022-10-10 18:21:12 +00:00
|
|
|
|
|
|
|
static func getLogData() -> Data? {
|
|
|
|
do {
|
|
|
|
let store = try OSLogStore(scope: .currentProcessIdentifier)
|
2022-10-29 04:23:18 +00:00
|
|
|
// do the filtering ourself, passing position/predicate into getEntries is far slower (priority inversion, I think)
|
|
|
|
let entries = try store.getEntries()
|
2022-10-10 18:21:12 +00:00
|
|
|
var data = Data()
|
2022-10-29 04:23:18 +00:00
|
|
|
let subsystem = Bundle.main.bundleIdentifier!
|
2022-10-10 18:21:12 +00:00
|
|
|
for entry in entries {
|
2022-10-29 04:23:18 +00:00
|
|
|
guard let entry = entry as? OSLogEntryLog,
|
|
|
|
entry.subsystem == subsystem else {
|
2022-10-10 18:21:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
data.append(contentsOf: entry.date.formatted(.iso8601).utf8)
|
|
|
|
data.append(32) // ' '
|
|
|
|
data.append(91) // '['
|
|
|
|
data.append(contentsOf: entry.category.utf8)
|
|
|
|
data.append(93) // ']'
|
|
|
|
data.append(32) // ' '
|
|
|
|
data.append(contentsOf: entry.composedMessage.utf8)
|
|
|
|
data.append(10) // '\n'
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 04:23:18 +00:00
|
|
|
|
|
|
|
static func writeDataForCrash() {
|
|
|
|
guard let data = getLogData(),
|
|
|
|
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let timestamp = ISO8601DateFormatter().string(from: Date())
|
|
|
|
let url = cacheDir.appendingPathComponent("Tusker-\(timestamp).log", isDirectory: false)
|
|
|
|
do {
|
|
|
|
try data.write(to: url)
|
|
|
|
UserDefaults.standard.set(url, forKey: "lastCrashLog")
|
|
|
|
} catch {
|
|
|
|
// if we can't write the data, oh well, we just won't have logs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func logURLForLastCrash() -> URL? {
|
|
|
|
return UserDefaults.standard.url(forKey: "lastCrashLog")
|
|
|
|
}
|
2022-10-10 18:21:12 +00:00
|
|
|
}
|