From 7c58a507787a58cc3541f31ceea4b2d6819902a8 Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Sun, 26 Aug 2018 12:08:07 +0200 Subject: [PATCH] Uncoupled DiskLogger from FileManager --- Logger/Loggers/DiskLogger/DiskLogger.swift | 10 ++++++---- Logger/Loggers/DiskLogger/FileSystem.swift | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Logger/Loggers/DiskLogger/DiskLogger.swift b/Logger/Loggers/DiskLogger/DiskLogger.swift index 20e864e..dfc6859 100644 --- a/Logger/Loggers/DiskLogger/DiskLogger.swift +++ b/Logger/Loggers/DiskLogger/DiskLogger.swift @@ -30,6 +30,7 @@ public final class DiskLogger: Logger { private let fileURL: URL private let fileSizeLimit: UInt64 private let rotations: Int + private let fileSystem: FileSystem private let formatter: DateFormatter private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil) private var buffer = Data() @@ -41,10 +42,11 @@ public final class DiskLogger: Logger { /// - fileURL: URL of the log file. /// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation. /// - rotations: Number of times log files are rotated before being removed. - public init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) { + public init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem) { self.fileURL = fileURL self.fileSizeLimit = fileSizeLimit self.rotations = rotations + self.fileSystem = fileSystem formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" formatter.timeZone = TimeZone(secondsFromGMT: 0) @@ -80,8 +82,8 @@ public final class DiskLogger: Logger { private func openFileWriter() throws { guard fileWriter == nil else { return } - if FileManager.default.fileExists(atPath: fileURL.path) == false { - FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil) + if fileSystem.itemExists(at: fileURL) == false { + _ = fileSystem.createFile(at: fileURL) } fileWriter = try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit) } @@ -97,7 +99,7 @@ public final class DiskLogger: Logger { } private func rotateLogFiles() throws { - let logrotate = Logrotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default) + let logrotate = Logrotate(fileURL: fileURL, rotations: rotations, fileSystem: fileSystem) try logrotate.rotate() } } diff --git a/Logger/Loggers/DiskLogger/FileSystem.swift b/Logger/Loggers/DiskLogger/FileSystem.swift index d78c5f4..b47b316 100644 --- a/Logger/Loggers/DiskLogger/FileSystem.swift +++ b/Logger/Loggers/DiskLogger/FileSystem.swift @@ -24,15 +24,20 @@ import Foundation -protocol FileSystem { +public protocol FileSystem { func itemExists(at URL: URL) -> Bool func removeItem(at URL: URL) throws func moveItem(at srcURL: URL, to dstURL: URL) throws + func createFile(at URL: URL) -> Bool } extension FileManager: FileSystem { - func itemExists(at URL: URL) -> Bool { + public func itemExists(at URL: URL) -> Bool { return fileExists(atPath: URL.path) } + + public func createFile(at URL: URL) -> Bool { + return createFile(atPath: URL.path, contents: nil, attributes: nil) + } }