mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-06 04:18:31 +00:00
Update the default logs directory and allow collection from elsewhere. (#4352)
* Replace deprecated URL methods. * Start putting log files in Library/Logs (moving existing files with a migration.) * Make Tracing.deleteLogFiles aware of the legacy logs location. * Allow the logs to be collected from a different directory.
This commit is contained in:
@@ -17,10 +17,16 @@ enum Tracing {
|
||||
if ProcessInfo.isRunningIntegrationTests {
|
||||
"/Users/Shared"
|
||||
} else {
|
||||
.appGroupContainerDirectory
|
||||
logsDirectoryOverride ?? .appGroupLogsDirectory
|
||||
}
|
||||
}
|
||||
|
||||
/// Set this to temporarily override the directory from which logs will be collected.
|
||||
/// This basically only affects ``logFiles``, and doesn't inform the SDK to write
|
||||
/// the logs to a different directory, which should be done before setting this.
|
||||
static var logsDirectoryOverride: URL?
|
||||
static var legacyLogsDirectory: URL { .appGroupContainerDirectory }
|
||||
|
||||
static let fileExtension = "log"
|
||||
|
||||
static func buildConfiguration(logLevel: LogLevel, traceLogPacks: Set<TraceLogPack>,
|
||||
@@ -52,13 +58,17 @@ enum Tracing {
|
||||
sentryDsn: sentryURL?.absoluteString)
|
||||
}
|
||||
|
||||
/// A list of all log file URLs, sorted chronologically. This is only public for testing purposes, within
|
||||
/// the app please use ``copyLogs(to:)`` so that the files are name appropriates for QuickLook.
|
||||
static var logFiles: [URL] {
|
||||
/// A list of all log file URLs, sorted chronologically.
|
||||
static var logFiles: [URL] { logFiles(in: logsDirectory) }
|
||||
|
||||
/// Collect all of the logs in the given directory, sorting them chronologically.
|
||||
private static func logFiles(in directory: URL) -> [URL] {
|
||||
var logFiles = [(url: URL, modificationDate: Date)]()
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let enumerator = fileManager.enumerator(at: logsDirectory, includingPropertiesForKeys: [.contentModificationDateKey])
|
||||
let enumerator = fileManager.enumerator(at: directory,
|
||||
includingPropertiesForKeys: [.contentModificationDateKey],
|
||||
options: .skipsSubdirectoryDescendants)
|
||||
|
||||
// Find all *.log files and their modification dates.
|
||||
while let logURL = enumerator?.nextObject() as? URL {
|
||||
@@ -78,10 +88,38 @@ enum Tracing {
|
||||
return sortedFiles
|
||||
}
|
||||
|
||||
/// Delete all log files.
|
||||
static func deleteLogFiles() {
|
||||
static func migrateLogFiles() {
|
||||
MXLog.info("Moving log files to \(logsDirectory)")
|
||||
let fileManager = FileManager.default
|
||||
for logFileURL in logFiles {
|
||||
let oldLogFiles = logFiles(in: legacyLogsDirectory)
|
||||
|
||||
for oldFileURL in oldLogFiles {
|
||||
do {
|
||||
let newFileURL = logsDirectory.appending(component: oldFileURL.lastPathComponent)
|
||||
try fileManager.moveItem(at: oldFileURL, to: newFileURL)
|
||||
MXLog.info("Moved \(newFileURL.lastPathComponent)")
|
||||
} catch {
|
||||
MXLog.error("Failed to move \(oldFileURL.lastPathComponent): \(error.localizedDescription)")
|
||||
|
||||
let nsError = error as NSError
|
||||
if nsError.domain == NSCocoaErrorDomain, nsError.code == NSFileWriteFileExistsError {
|
||||
// By now there will already be some logs in the new directory, so there is likely to be
|
||||
// one log file that cannot be removed. As this is a one-off operation lets just delete it.
|
||||
MXLog.error("Attempting to delete log file \(oldFileURL.lastPathComponent)")
|
||||
try? fileManager.removeItem(at: oldFileURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete all log files.
|
||||
static func deleteLogFiles(in directory: URL) {
|
||||
let fileManager = FileManager.default
|
||||
|
||||
// We don't simply delete logsDirectory as once upon a time the logs
|
||||
// we written to the very top-level of the app group container and
|
||||
// there's a migration in place for old users of the app.
|
||||
for logFileURL in logFiles(in: directory) {
|
||||
try? fileManager.removeItem(at: logFileURL)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user