summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadLogging/TextFileOutputStream.swift
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2023-02-01 16:24:39 +0100
committerAndrej Mihajlov <and@mullvad.net>2023-02-03 11:03:18 +0100
commit04194a342379c29e17fa07895a57240c8f8877dc (patch)
tree14f9571b163d05dd84d8823d85350c3b31d60f7b /ios/MullvadLogging/TextFileOutputStream.swift
parent4c9565661c7b1273b7019bb8a895a511b6e03bd0 (diff)
downloadmullvadvpn-04194a342379c29e17fa07895a57240c8f8877dc.tar.xz
mullvadvpn-04194a342379c29e17fa07895a57240c8f8877dc.zip
Buffer messages and attempt to reopen log file descriptor on failure
This should cover the case where log file cannot be open on boot when file system is still locked (until the first device unlock)
Diffstat (limited to 'ios/MullvadLogging/TextFileOutputStream.swift')
-rw-r--r--ios/MullvadLogging/TextFileOutputStream.swift87
1 files changed, 0 insertions, 87 deletions
diff --git a/ios/MullvadLogging/TextFileOutputStream.swift b/ios/MullvadLogging/TextFileOutputStream.swift
deleted file mode 100644
index 95387b53ec..0000000000
--- a/ios/MullvadLogging/TextFileOutputStream.swift
+++ /dev/null
@@ -1,87 +0,0 @@
-//
-// TextFileOutputStream.swift
-// MullvadVPN
-//
-// Created by pronebird on 02/08/2020.
-// Copyright © 2020 Mullvad VPN AB. All rights reserved.
-//
-
-import Foundation
-
-class TextFileOutputStream: TextOutputStream {
- private let writer: DispatchIO
- private let encoding: String.Encoding
- private let queue = DispatchQueue.global(qos: .utility)
-
- class func standardOutputStream(encoding: String.Encoding = .utf8) -> TextFileOutputStream {
- return TextFileOutputStream(
- fileDescriptor: FileHandle.standardOutput.fileDescriptor,
- encoding: encoding
- )
- }
-
- init(fileDescriptor: Int32, encoding: String.Encoding = .utf8) {
- self.encoding = encoding
- writer = DispatchIO(type: .stream, fileDescriptor: fileDescriptor, queue: queue) { errno in
- if errno != 0 {
- print("TextFileOutputStream: closed channel with error: \(errno)")
- }
- }
- }
-
- init?(
- fileURL: URL,
- createFile: Bool,
- filePermissions: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
- encoding: String.Encoding = .utf8
- ) {
- var oflag: Int32 = O_WRONLY
- var mode: mode_t = .zero
- if createFile {
- oflag |= O_CREAT
- mode = filePermissions
- }
-
- let queue = queue
- let writer = fileURL.path.withCString { filePathPointer -> DispatchIO? in
- return DispatchIO(
- type: .stream,
- path: filePathPointer,
- oflag: oflag,
- mode: mode,
- queue: queue,
- cleanupHandler: { errno in
- if errno != 0 {
- print("TextFileOutputStream: closed channel with error: \(errno)")
- }
- }
- )
- }
-
- if let writer = writer {
- self.writer = writer
- self.encoding = encoding
- } else {
- return nil
- }
- }
-
- deinit {
- writer.close()
- }
-
- func write(_ string: String) {
- string.data(using: encoding)?.withUnsafeBytes { bytes in
- writer
- .write(
- offset: .zero,
- data: DispatchData(bytes: bytes),
- queue: queue
- ) { done, data, errno in
- if errno != 0 {
- print("TextFileOutputStream: write error: \(errno)")
- }
- }
- }
- }
-}