Skip to content

Commit

Permalink
Limiting initialization of the FileWriter on iOS to just the main app…
Browse files Browse the repository at this point in the history
…lication exluding extensions to allow us to add protected data monitoring.
  • Loading branch information
tonystone committed Sep 15, 2019
1 parent 0b44c2f commit 76e258b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Sources/TraceLog/Writers/FileStrategy+Fixed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@
///
import Foundation

@available(iOSApplicationExtension, unavailable)
internal class FileStrategyFixed: FileStrategyManager {

let url: URL

private var stream: FileOutputStream
private var available: Bool

init(directory: URL, fileName: String) throws {

self.url = directory.appendingPathComponent(fileName)
self.available = isLogAvailable()
self.stream = try FileOutputStream(url: url, options: [.create])

#if os(iOS)
#if os(iOS) && !targetEnvironment(simulator)
/// Note: You can create empty files with file protection in any state. You just cant write or read from them.
/// We can safely create the file and set it's prtection level even if not available.
/// We can safely create the file and set it's protection level even if not available.
///
try FileManager.default.setAttributes([.protectionKey : FileProtectionType.completeUntilFirstUserAuthentication], ofItemAtPath: url.path)

Expand All @@ -49,19 +53,18 @@ internal class FileStrategyFixed: FileStrategyManager {
}

func write(_ bytes: [UInt8]) -> Result<Int, FailureReason> {

guard self.available
else { return .failure(.unavailable) }

return stream.write(bytes).mapError({ self.failureReason($0) })
}

private var stream: FileOutputStream
private var available: Bool
}

#if os(iOS)
#if os(iOS) && !targetEnvironment(simulator)
import UIKit

@available(iOSApplicationExtension, unavailable)
func isLogAvailable() -> Bool {
if !Thread.isMainThread {
return DispatchQueue.main.sync {
Expand Down
1 change: 1 addition & 0 deletions Sources/TraceLog/Writers/FileWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import Foundation
///
/// - SeeAlso: `FileWriter.Strategy` for complete details of all strategies that can be used.
///
@available(iOSApplicationExtension, unavailable, message: "FielWriter can not be initialized in an Extension. Please initialize it in the main App.")
public class FileWriter: OutputStreamWriter {

// MARK: Initialization
Expand Down

0 comments on commit 76e258b

Please sign in to comment.