mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-06 12:31:51 +02:00
Uncoupled FileWriter from FileHandle
This commit is contained in:
parent
74fe9ba0d2
commit
ab7ec33418
2 changed files with 29 additions and 8 deletions
|
@ -172,6 +172,16 @@ private class FileRotateFactory: LogrotateFactory {
|
|||
|
||||
private class FileWriterFactory: SizeLimitedFileFactory {
|
||||
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile {
|
||||
return try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
||||
return try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit, fileFactory: FileHandleFactory())
|
||||
}
|
||||
}
|
||||
|
||||
private class FileHandleFactory: FileFactory {
|
||||
func makeInstance(forWritingTo: URL) throws -> File {
|
||||
return try FileHandle(forWritingTo: forWritingTo)
|
||||
}
|
||||
}
|
||||
|
||||
extension FileHandle: File {
|
||||
|
||||
}
|
||||
|
|
|
@ -24,10 +24,21 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
protocol File {
|
||||
func seekToEndOfFile() -> UInt64
|
||||
func swift_write(_ data: Data) throws
|
||||
func synchronizeFile()
|
||||
func closeFile()
|
||||
}
|
||||
|
||||
protocol FileFactory {
|
||||
func makeInstance(forWritingTo: URL) throws -> File
|
||||
}
|
||||
|
||||
/// Allows writing to a file while respecting allowed size limit.
|
||||
final class FileWriter {
|
||||
|
||||
private let handle: FileHandle
|
||||
private let file: File
|
||||
private let sizeLimit: UInt64
|
||||
private var currentSize: UInt64
|
||||
|
||||
|
@ -37,10 +48,10 @@ final class FileWriter {
|
|||
/// - fileURL: URL of the file.
|
||||
/// - fileSizeLimit: Maximum size the file can reach in bytes.
|
||||
/// - Throws: An error that may occur while the file is being opened for writing.
|
||||
init(fileURL: URL, fileSizeLimit: UInt64) throws {
|
||||
handle = try FileHandle(forWritingTo: fileURL)
|
||||
init(fileURL: URL, fileSizeLimit: UInt64, fileFactory: FileFactory) throws {
|
||||
file = try fileFactory.makeInstance(forWritingTo: fileURL)
|
||||
self.sizeLimit = fileSizeLimit
|
||||
currentSize = handle.seekToEndOfFile()
|
||||
currentSize = file.seekToEndOfFile()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,12 +62,12 @@ extension FileWriter: SizeLimitedFile {
|
|||
guard currentSize + dataSize <= sizeLimit else {
|
||||
throw SizeLimitedFileQuotaReached()
|
||||
}
|
||||
try handle.swift_write(data)
|
||||
try file.swift_write(data)
|
||||
currentSize += dataSize
|
||||
}
|
||||
|
||||
func synchronizeAndCloseFile() {
|
||||
handle.synchronizeFile()
|
||||
handle.closeFile()
|
||||
file.synchronizeFile()
|
||||
file.closeFile()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue