diff --git a/Logger.xcodeproj/project.pbxproj b/Logger.xcodeproj/project.pbxproj index 69657b4..11a519e 100644 --- a/Logger.xcodeproj/project.pbxproj +++ b/Logger.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 2EBF4B592122B598008E4117 /* Logrotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B562122B598008E4117 /* Logrotate.swift */; }; 2ED077D721329CA30058EEFC /* LoggetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D621329CA30058EEFC /* LoggetTests.swift */; }; 2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */; }; + 2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED077DA2132B0320058EEFC /* FileSystem.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -62,6 +63,7 @@ 2EBF4B562122B598008E4117 /* Logrotate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logrotate.swift; sourceTree = ""; }; 2ED077D621329CA30058EEFC /* LoggetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggetTests.swift; sourceTree = ""; }; 2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgregateLoggerTests.swift; sourceTree = ""; }; + 2ED077DA2132B0320058EEFC /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -142,6 +144,7 @@ 2EBF4B562122B598008E4117 /* Logrotate.swift */, 2EBF4B4F2122B06E008E4117 /* NSFileHandle+Swift.h */, 2EBF4B502122B06E008E4117 /* NSFileHandle+Swift.m */, + 2ED077DA2132B0320058EEFC /* FileSystem.swift */, ); path = DiskLogger; sourceTree = ""; @@ -252,6 +255,7 @@ 2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */, 2EBF4B512122B06E008E4117 /* NSFileHandle+Swift.m in Sources */, 2EBF4B4B2122AF53008E4117 /* ConsoleLogger.swift in Sources */, + 2ED077DB2132B0320058EEFC /* FileSystem.swift in Sources */, 2EBF4B4A2122AF53008E4117 /* AgregateLogger.swift in Sources */, 2EBF4B592122B598008E4117 /* Logrotate.swift in Sources */, 2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */, diff --git a/Logger/Loggers/DiskLogger/DiskLogger.swift b/Logger/Loggers/DiskLogger/DiskLogger.swift index d4cb80f..20e864e 100644 --- a/Logger/Loggers/DiskLogger/DiskLogger.swift +++ b/Logger/Loggers/DiskLogger/DiskLogger.swift @@ -97,7 +97,7 @@ public final class DiskLogger: Logger { } private func rotateLogFiles() throws { - let logrotate = Logrotate(fileURL: fileURL, rotations: rotations) + let logrotate = Logrotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default) try logrotate.rotate() } } diff --git a/Logger/Loggers/DiskLogger/FileSystem.swift b/Logger/Loggers/DiskLogger/FileSystem.swift new file mode 100644 index 0000000..d78c5f4 --- /dev/null +++ b/Logger/Loggers/DiskLogger/FileSystem.swift @@ -0,0 +1,38 @@ +// +// MIT License +// +// Copyright (c) 2018 Wojciech Nagrodzki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import Foundation + +protocol FileSystem { + func itemExists(at URL: URL) -> Bool + func removeItem(at URL: URL) throws + func moveItem(at srcURL: URL, to dstURL: URL) throws +} + +extension FileManager: FileSystem { + + func itemExists(at URL: URL) -> Bool { + return fileExists(atPath: URL.path) + } +} diff --git a/Logger/Loggers/DiskLogger/Logrotate.swift b/Logger/Loggers/DiskLogger/Logrotate.swift index c377d2e..d2a97e4 100644 --- a/Logger/Loggers/DiskLogger/Logrotate.swift +++ b/Logger/Loggers/DiskLogger/Logrotate.swift @@ -29,16 +29,18 @@ final class Logrotate { private let fileURL: URL private let rotations: Int + private let fileSystem: FileSystem /// Initializes new Logrotate instance. /// /// - Parameters: /// - fileURL: URL of the log file. /// - rotations: Number of times log files are rotated before being removed. - init(fileURL: URL, rotations: Int) { + init(fileURL: URL, rotations: Int, fileSystem: FileSystem) { precondition(rotations > 0) self.fileURL = fileURL self.rotations = rotations + self.fileSystem = fileSystem } /// Rotates log files `rotations` number of times. @@ -58,12 +60,12 @@ final class Logrotate { let toDelete = rotatedURLs.last! let toMove = zip(allURLs, rotatedURLs).reversed() - if FileManager.default.fileExists(atPath: toDelete.path) { - try FileManager.default.removeItem(at: toDelete) + if fileSystem.itemExists(at: toDelete) { + try fileSystem.removeItem(at: toDelete) } for (oldURL, newURL) in toMove { - guard FileManager.default.fileExists(atPath: oldURL.path) else { continue } - try FileManager.default.moveItem(at: oldURL, to: newURL) + guard fileSystem.itemExists(at: oldURL) else { continue } + try fileSystem.moveItem(at: oldURL, to: newURL) } } }