diff options
| author | Andrew Bulhak <andrew.bulhak@mullvad.net> | 2024-04-11 12:55:52 +0200 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-04-17 12:40:40 +0200 |
| commit | 6cd934154cda7e4e33b6a62dbdec982a96a219c8 (patch) | |
| tree | 38602dbd862aa10ae0aad5f852e23bdcb82963ef /ios/MullvadLogging/LogFileOutputStream.swift | |
| parent | 4654acb952e4b4024329f99bb0b4f780154bdd12 (diff) | |
| download | mullvadvpn-6cd934154cda7e4e33b6a62dbdec982a96a219c8.tar.xz mullvadvpn-6cd934154cda7e4e33b6a62dbdec982a96a219c8.zip | |
Move log file header writing to LogFileOutputStream
Diffstat (limited to 'ios/MullvadLogging/LogFileOutputStream.swift')
| -rw-r--r-- | ios/MullvadLogging/LogFileOutputStream.swift | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/ios/MullvadLogging/LogFileOutputStream.swift b/ios/MullvadLogging/LogFileOutputStream.swift index a3b7ca4caa..26b059638e 100644 --- a/ios/MullvadLogging/LogFileOutputStream.swift +++ b/ios/MullvadLogging/LogFileOutputStream.swift @@ -19,6 +19,7 @@ class LogFileOutputStream: TextOutputStream { private let fileURL: URL private let encoding: String.Encoding private let maxBufferCapacity: Int + private let fileHeader: String private var state: State = .closed { didSet { @@ -44,10 +45,11 @@ class LogFileOutputStream: TextOutputStream { case waitingToReopen } - init(fileURL: URL, encoding: String.Encoding = .utf8, maxBufferCapacity: Int = 16 * 1024) { + init(fileURL: URL, header: String, encoding: String.Encoding = .utf8, maxBufferCapacity: Int = 16 * 1024) { self.fileURL = fileURL self.encoding = encoding self.maxBufferCapacity = maxBufferCapacity + self.fileHeader = header } deinit { @@ -66,7 +68,7 @@ class LogFileOutputStream: TextOutputStream { switch state { case .closed: do { - let fileHandle = try openFile() + let fileHandle = try openFileWithHeader(fileHeader) state = .opened(fileHandle) try write(fileHandle: fileHandle, data: data) } catch { @@ -137,15 +139,22 @@ class LogFileOutputStream: TextOutputStream { timer = nil } + private func openFileWithHeader(_ header: String) throws -> FileHandle { + let fileHandle = try openFile() + + let messageData = + "\(header)\n" + .data(using: encoding, allowLossyConversion: true)! + try write(fileHandle: fileHandle, data: messageData) + + return fileHandle + } + private func reopenFile() { do { - let fileHandle = try openFile() - // Write a message indicating that the file was reopened. - let messageData = - "<Log file re-opened after failure. Buffered \(buffer.count) bytes of messages>\n" - .data(using: encoding, allowLossyConversion: true)! - try write(fileHandle: fileHandle, data: messageData) + let fileHandle = + try openFileWithHeader("<Log file re-opened after failure. Buffered \(buffer.count) bytes of messages>") // Write all buffered messages. if !buffer.isEmpty { |
