mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Uncoupled DiskLogger from FileWriter
This commit is contained in:
parent
1144d6a90f
commit
74fe9ba0d2
2 changed files with 53 additions and 23 deletions
|
@ -24,6 +24,34 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
/// Write failed as allowed size limit would be exceeded.
|
||||||
|
public struct SizeLimitedFileQuotaReached: Error {}
|
||||||
|
|
||||||
|
/// Allows writing to a file while respecting allowed size limit.
|
||||||
|
protocol SizeLimitedFile {
|
||||||
|
|
||||||
|
/// Synchronously writes `data` at the end of the file.
|
||||||
|
///
|
||||||
|
/// - Parameter data: The data to be written.
|
||||||
|
/// - Throws: Throws an error if no free space is left on the file system, or if any other writing error occurs.
|
||||||
|
/// Throws `SizeLimitedFileQuotaReached` if allowed size limit would be exceeded.
|
||||||
|
func write(_ data: Data) throws
|
||||||
|
|
||||||
|
/// Writes all in-memory data to permanent storage and closes the file.
|
||||||
|
func synchronizeAndCloseFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol SizeLimitedFileFactory {
|
||||||
|
|
||||||
|
/// Returns newly initialized SizeLimitedFile instance.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - 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.
|
||||||
|
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile
|
||||||
|
}
|
||||||
|
|
||||||
/// Allows log files rotation.
|
/// Allows log files rotation.
|
||||||
protocol Logrotate {
|
protocol Logrotate {
|
||||||
|
|
||||||
|
@ -55,11 +83,12 @@ public final class DiskLogger: Logger {
|
||||||
private let fileSizeLimit: UInt64
|
private let fileSizeLimit: UInt64
|
||||||
private let rotations: Int
|
private let rotations: Int
|
||||||
private let fileSystem: FileSystem
|
private let fileSystem: FileSystem
|
||||||
|
private let sizeLimitedFileFactory: SizeLimitedFileFactory
|
||||||
private let logrotateFactory: LogrotateFactory
|
private let logrotateFactory: LogrotateFactory
|
||||||
private let formatter: DateFormatter
|
private let formatter: DateFormatter
|
||||||
private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
||||||
private var buffer = Data()
|
private var buffer = Data()
|
||||||
private var fileWriter: FileWriter!
|
private var sizeLimitedFile: SizeLimitedFile!
|
||||||
|
|
||||||
/// Initializes new DiskLogger instance.
|
/// Initializes new DiskLogger instance.
|
||||||
///
|
///
|
||||||
|
@ -68,14 +97,15 @@ public final class DiskLogger: Logger {
|
||||||
/// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation.
|
/// - 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.
|
/// - rotations: Number of times log files are rotated before being removed.
|
||||||
public convenience init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) {
|
public convenience init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) {
|
||||||
self.init(fileURL: fileURL, fileSizeLimit: fileSizeLimit, rotations: rotations, fileSystem: FileManager.default, logrotateFactory: FileRotateFactory())
|
self.init(fileURL: fileURL, fileSizeLimit: fileSizeLimit, rotations: rotations, fileSystem: FileManager.default, sizeLimitedFileFactory: FileWriterFactory(), logrotateFactory: FileRotateFactory())
|
||||||
}
|
}
|
||||||
|
|
||||||
init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem, logrotateFactory: LogrotateFactory) {
|
init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem, sizeLimitedFileFactory: SizeLimitedFileFactory, logrotateFactory: LogrotateFactory) {
|
||||||
self.fileURL = fileURL
|
self.fileURL = fileURL
|
||||||
self.fileSizeLimit = fileSizeLimit
|
self.fileSizeLimit = fileSizeLimit
|
||||||
self.rotations = rotations
|
self.rotations = rotations
|
||||||
self.fileSystem = fileSystem
|
self.fileSystem = fileSystem
|
||||||
|
self.sizeLimitedFileFactory = sizeLimitedFileFactory
|
||||||
self.logrotateFactory = logrotateFactory
|
self.logrotateFactory = logrotateFactory
|
||||||
formatter = DateFormatter()
|
formatter = DateFormatter()
|
||||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||||
|
@ -93,12 +123,12 @@ public final class DiskLogger: Logger {
|
||||||
self.buffer.append(data)
|
self.buffer.append(data)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try self.openFileWriter()
|
try self.openSizeLimitedFile()
|
||||||
do {
|
do {
|
||||||
try self.writeBuffer()
|
try self.writeBuffer()
|
||||||
}
|
}
|
||||||
catch is FileWriter.FileSizeLimitReached {
|
catch is SizeLimitedFileQuotaReached {
|
||||||
self.closeFileWriter()
|
self.closeSizeLimitedFile()
|
||||||
try self.rotateLogFiles()
|
try self.rotateLogFiles()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,22 +140,22 @@ public final class DiskLogger: Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func openFileWriter() throws {
|
private func openSizeLimitedFile() throws {
|
||||||
guard fileWriter == nil else { return }
|
guard sizeLimitedFile == nil else { return }
|
||||||
if fileSystem.itemExists(at: fileURL) == false {
|
if fileSystem.itemExists(at: fileURL) == false {
|
||||||
_ = fileSystem.createFile(at: fileURL)
|
_ = fileSystem.createFile(at: fileURL)
|
||||||
}
|
}
|
||||||
fileWriter = try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
sizeLimitedFile = try sizeLimitedFileFactory.makeInstance(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func writeBuffer() throws {
|
private func writeBuffer() throws {
|
||||||
try fileWriter.write(buffer)
|
try sizeLimitedFile.write(buffer)
|
||||||
buffer.removeAll()
|
buffer.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func closeFileWriter() {
|
private func closeSizeLimitedFile() {
|
||||||
self.fileWriter.synchronizeAndCloseFile()
|
self.sizeLimitedFile.synchronizeAndCloseFile()
|
||||||
self.fileWriter = nil
|
self.sizeLimitedFile = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
private func rotateLogFiles() throws {
|
private func rotateLogFiles() throws {
|
||||||
|
@ -139,3 +169,9 @@ private class FileRotateFactory: LogrotateFactory {
|
||||||
return FileRotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default)
|
return FileRotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class FileWriterFactory: SizeLimitedFileFactory {
|
||||||
|
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile {
|
||||||
|
return try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -27,9 +27,6 @@ import Foundation
|
||||||
/// 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 {
|
||||||
|
|
||||||
/// Write failed as allowed size limit would be exceeded for the file.
|
|
||||||
struct FileSizeLimitReached: Error {}
|
|
||||||
|
|
||||||
private let handle: FileHandle
|
private let handle: FileHandle
|
||||||
private let sizeLimit: UInt64
|
private let sizeLimit: UInt64
|
||||||
private var currentSize: UInt64
|
private var currentSize: UInt64
|
||||||
|
@ -45,22 +42,19 @@ final class FileWriter {
|
||||||
self.sizeLimit = fileSizeLimit
|
self.sizeLimit = fileSizeLimit
|
||||||
currentSize = handle.seekToEndOfFile()
|
currentSize = handle.seekToEndOfFile()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension FileWriter: SizeLimitedFile {
|
||||||
|
|
||||||
/// Synchronously writes `data` at the end of the file.
|
|
||||||
///
|
|
||||||
/// - Parameter data: The data to be written.
|
|
||||||
/// - Throws: Throws an error if no free space is left on the file system, or if any other writing error occurs.
|
|
||||||
/// Throws `FileSizeLimitReached` if allowed size limit would be exceeded for the file.
|
|
||||||
func write(_ data: Data) throws {
|
func write(_ data: Data) throws {
|
||||||
let dataSize = UInt64(data.count)
|
let dataSize = UInt64(data.count)
|
||||||
guard currentSize + dataSize <= sizeLimit else {
|
guard currentSize + dataSize <= sizeLimit else {
|
||||||
throw FileSizeLimitReached()
|
throw SizeLimitedFileQuotaReached()
|
||||||
}
|
}
|
||||||
try handle.swift_write(data)
|
try handle.swift_write(data)
|
||||||
currentSize += dataSize
|
currentSize += dataSize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes all in-memory data to permanent storage and closes the file.
|
|
||||||
func synchronizeAndCloseFile() {
|
func synchronizeAndCloseFile() {
|
||||||
handle.synchronizeFile()
|
handle.synchronizeFile()
|
||||||
handle.closeFile()
|
handle.closeFile()
|
||||||
|
|
Loading…
Add table
Reference in a new issue