mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +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 {
|
private class FileWriterFactory: SizeLimitedFileFactory {
|
||||||
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile {
|
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
|
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.
|
/// Allows writing to a file while respecting allowed size limit.
|
||||||
final class FileWriter {
|
final class FileWriter {
|
||||||
|
|
||||||
private let handle: FileHandle
|
private let file: File
|
||||||
private let sizeLimit: UInt64
|
private let sizeLimit: UInt64
|
||||||
private var currentSize: UInt64
|
private var currentSize: UInt64
|
||||||
|
|
||||||
|
@ -37,10 +48,10 @@ final class FileWriter {
|
||||||
/// - fileURL: URL of the file.
|
/// - fileURL: URL of the file.
|
||||||
/// - fileSizeLimit: Maximum size the file can reach in bytes.
|
/// - fileSizeLimit: Maximum size the file can reach in bytes.
|
||||||
/// - Throws: An error that may occur while the file is being opened for writing.
|
/// - Throws: An error that may occur while the file is being opened for writing.
|
||||||
init(fileURL: URL, fileSizeLimit: UInt64) throws {
|
init(fileURL: URL, fileSizeLimit: UInt64, fileFactory: FileFactory) throws {
|
||||||
handle = try FileHandle(forWritingTo: fileURL)
|
file = try fileFactory.makeInstance(forWritingTo: fileURL)
|
||||||
self.sizeLimit = fileSizeLimit
|
self.sizeLimit = fileSizeLimit
|
||||||
currentSize = handle.seekToEndOfFile()
|
currentSize = file.seekToEndOfFile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,12 +62,12 @@ extension FileWriter: SizeLimitedFile {
|
||||||
guard currentSize + dataSize <= sizeLimit else {
|
guard currentSize + dataSize <= sizeLimit else {
|
||||||
throw SizeLimitedFileQuotaReached()
|
throw SizeLimitedFileQuotaReached()
|
||||||
}
|
}
|
||||||
try handle.swift_write(data)
|
try file.swift_write(data)
|
||||||
currentSize += dataSize
|
currentSize += dataSize
|
||||||
}
|
}
|
||||||
|
|
||||||
func synchronizeAndCloseFile() {
|
func synchronizeAndCloseFile() {
|
||||||
handle.synchronizeFile()
|
file.synchronizeFile()
|
||||||
handle.closeFile()
|
file.closeFile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue