mirror of
https://github.com/cranci1/Sora.git
synced 2026-03-11 17:45:37 +00:00
logger improvements
This commit is contained in:
parent
0926b5b9d2
commit
cb76d39715
1 changed files with 29 additions and 4 deletions
|
|
@ -21,6 +21,8 @@ class Logger {
|
|||
private let logFileURL: URL
|
||||
private let logFilterViewModel = LogFilterViewModel.shared
|
||||
|
||||
private let maxFileSize = 1024 * 1024
|
||||
|
||||
private init() {
|
||||
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
||||
logFileURL = documentDirectory.appendingPathComponent("logs.txt")
|
||||
|
|
@ -64,10 +66,33 @@ class Logger {
|
|||
|
||||
if let data = logString.data(using: .utf8) {
|
||||
if FileManager.default.fileExists(atPath: logFileURL.path) {
|
||||
if let handle = try? FileHandle(forWritingTo: logFileURL) {
|
||||
handle.seekToEndOfFile()
|
||||
handle.write(data)
|
||||
handle.closeFile()
|
||||
do {
|
||||
let attributes = try FileManager.default.attributesOfItem(atPath: logFileURL.path)
|
||||
let fileSize = attributes[.size] as? UInt64 ?? 0
|
||||
|
||||
if fileSize + UInt64(data.count) > maxFileSize {
|
||||
guard var content = try? String(contentsOf: logFileURL, encoding: .utf8) else { return }
|
||||
|
||||
while (content.data(using: .utf8)?.count ?? 0) + data.count > maxFileSize {
|
||||
if let rangeOfFirstLine = content.range(of: "\n---\n") {
|
||||
content.removeSubrange(content.startIndex...rangeOfFirstLine.upperBound)
|
||||
} else {
|
||||
content = ""
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
content += logString
|
||||
try? content.data(using: .utf8)?.write(to: logFileURL)
|
||||
} else {
|
||||
if let handle = try? FileHandle(forWritingTo: logFileURL) {
|
||||
handle.seekToEndOfFile()
|
||||
handle.write(data)
|
||||
handle.closeFile()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
print("Error managing log file: \(error)")
|
||||
}
|
||||
} else {
|
||||
try? data.write(to: logFileURL)
|
||||
|
|
|
|||
Loading…
Reference in a new issue