mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-06 12:31:51 +02:00
Rename FileWriter to SizeLimitedFileImpl. Move SizeLimitedFile to SizeLimitedFile.swift
This commit is contained in:
parent
4f608bbb60
commit
bdbd615ea4
4 changed files with 34 additions and 34 deletions
|
@ -15,13 +15,13 @@
|
|||
2EBF4B4B2122AF53008E4117 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B482122AF53008E4117 /* ConsoleLogger.swift */; };
|
||||
2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B492122AF53008E4117 /* NullLogger.swift */; };
|
||||
2EBF4B572122B598008E4117 /* DiskLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B542122B598008E4117 /* DiskLogger.swift */; };
|
||||
2EBF4B582122B598008E4117 /* FileWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B552122B598008E4117 /* FileWriter.swift */; };
|
||||
2EBF4B582122B598008E4117 /* SizeLimitedFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B552122B598008E4117 /* SizeLimitedFile.swift */; };
|
||||
2EBF4B592122B598008E4117 /* FileRotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B562122B598008E4117 /* FileRotate.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 */; };
|
||||
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E02135C61100EB3683 /* FileRotateTests.swift */; };
|
||||
2ED103E32135D3FB00EB3683 /* FileWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */; };
|
||||
2ED103E32135D3FB00EB3683 /* SizeLimitedFileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */; };
|
||||
2ED103E52138553B00EB3683 /* DiskLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
|
@ -58,13 +58,13 @@
|
|||
2EBF4B482122AF53008E4117 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
|
||||
2EBF4B492122AF53008E4117 /* NullLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NullLogger.swift; sourceTree = "<group>"; };
|
||||
2EBF4B542122B598008E4117 /* DiskLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskLogger.swift; sourceTree = "<group>"; };
|
||||
2EBF4B552122B598008E4117 /* FileWriter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileWriter.swift; sourceTree = "<group>"; };
|
||||
2EBF4B552122B598008E4117 /* SizeLimitedFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SizeLimitedFile.swift; sourceTree = "<group>"; };
|
||||
2EBF4B562122B598008E4117 /* FileRotate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileRotate.swift; sourceTree = "<group>"; };
|
||||
2ED077D621329CA30058EEFC /* LoggetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggetTests.swift; sourceTree = "<group>"; };
|
||||
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgregateLoggerTests.swift; sourceTree = "<group>"; };
|
||||
2ED077DA2132B0320058EEFC /* FileSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystem.swift; sourceTree = "<group>"; };
|
||||
2ED103E02135C61100EB3683 /* FileRotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileRotateTests.swift; sourceTree = "<group>"; };
|
||||
2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileWriterTests.swift; sourceTree = "<group>"; };
|
||||
2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizeLimitedFileTests.swift; sourceTree = "<group>"; };
|
||||
2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskLoggerTests.swift; sourceTree = "<group>"; };
|
||||
2EDA8AE8213ACCFF00FE5840 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
@ -95,7 +95,7 @@
|
|||
2ED077D621329CA30058EEFC /* LoggetTests.swift */,
|
||||
2ED077D82132A4820058EEFC /* AgregateLoggerTests.swift */,
|
||||
2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */,
|
||||
2ED103E22135D3FB00EB3683 /* FileWriterTests.swift */,
|
||||
2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */,
|
||||
2ED103E02135C61100EB3683 /* FileRotateTests.swift */,
|
||||
2E58D35E21316C3500BEF81A /* Info.plist */,
|
||||
);
|
||||
|
@ -146,7 +146,7 @@
|
|||
isa = PBXGroup;
|
||||
children = (
|
||||
2EBF4B542122B598008E4117 /* DiskLogger.swift */,
|
||||
2EBF4B552122B598008E4117 /* FileWriter.swift */,
|
||||
2EBF4B552122B598008E4117 /* SizeLimitedFile.swift */,
|
||||
2EBF4B562122B598008E4117 /* FileRotate.swift */,
|
||||
2ED077DA2132B0320058EEFC /* FileSystem.swift */,
|
||||
);
|
||||
|
@ -246,7 +246,7 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
2E58D35D21316C3500BEF81A /* LogStringConvertibleTests.swift in Sources */,
|
||||
2ED103E32135D3FB00EB3683 /* FileWriterTests.swift in Sources */,
|
||||
2ED103E32135D3FB00EB3683 /* SizeLimitedFileTests.swift in Sources */,
|
||||
2ED103E52138553B00EB3683 /* DiskLoggerTests.swift in Sources */,
|
||||
2ED077D92132A4820058EEFC /* AgregateLoggerTests.swift in Sources */,
|
||||
2ED103E12135C61100EB3683 /* FileRotateTests.swift in Sources */,
|
||||
|
@ -258,7 +258,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
2EBF4B582122B598008E4117 /* FileWriter.swift in Sources */,
|
||||
2EBF4B582122B598008E4117 /* SizeLimitedFile.swift in Sources */,
|
||||
2EBF4B572122B598008E4117 /* DiskLogger.swift in Sources */,
|
||||
2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */,
|
||||
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */,
|
||||
|
|
|
@ -24,23 +24,6 @@
|
|||
|
||||
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.
|
||||
|
@ -174,7 +157,7 @@ private class FileRotateFactory: LogrotateFactory {
|
|||
|
||||
private class FileWriterFactory: SizeLimitedFileFactory {
|
||||
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile {
|
||||
return try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit, fileFactory: FileHandleFactory())
|
||||
return try SizeLimitedFileImpl(fileURL: fileURL, fileSizeLimit: fileSizeLimit, fileFactory: FileHandleFactory())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,14 +35,31 @@ protocol FileFactory {
|
|||
func makeInstance(forWritingTo: URL) throws -> File
|
||||
}
|
||||
|
||||
/// Write failed as allowed size limit would be exceeded.
|
||||
public struct SizeLimitedFileQuotaReached: Error {}
|
||||
|
||||
/// Allows writing to a file while respecting allowed size limit.
|
||||
final class FileWriter {
|
||||
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()
|
||||
}
|
||||
|
||||
/// Allows writing to a file while respecting allowed size limit.
|
||||
final class SizeLimitedFileImpl {
|
||||
|
||||
private let file: File
|
||||
private let sizeLimit: UInt64
|
||||
private var currentSize: UInt64
|
||||
|
||||
/// Initializes new FileWriter instance.
|
||||
/// Initializes new SizeLimitedFileImpl instance.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - fileURL: URL of the file.
|
||||
|
@ -55,7 +72,7 @@ final class FileWriter {
|
|||
}
|
||||
}
|
||||
|
||||
extension FileWriter: SizeLimitedFile {
|
||||
extension SizeLimitedFileImpl: SizeLimitedFile {
|
||||
|
||||
func write(_ data: Data) throws {
|
||||
let dataSize = UInt64(data.count)
|
|
@ -25,20 +25,20 @@
|
|||
import XCTest
|
||||
@testable import Logger
|
||||
|
||||
class FileWriterTests: XCTestCase {
|
||||
class SizeLimitedFileTests: XCTestCase {
|
||||
|
||||
let logURL = URL(fileURLWithPath: "/var/log/application.log")
|
||||
|
||||
func testFileOpeningFailure() {
|
||||
let factory = UnopenableFileFactory()
|
||||
XCTAssertThrowsError(try FileWriter(fileURL: logURL, fileSizeLimit: 0, fileFactory: factory), "file open failure") { (error) in
|
||||
XCTAssertThrowsError(try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 0, fileFactory: factory), "file open failure") { (error) in
|
||||
XCTAssertTrue(error is UnopenableFileFactory.OpenFileError)
|
||||
}
|
||||
}
|
||||
|
||||
func testKeepingFileSizeLimit() throws {
|
||||
let factory = FileMockFactory()
|
||||
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
let data = Data([0])
|
||||
try writer.write(data)
|
||||
|
||||
|
@ -47,7 +47,7 @@ class FileWriterTests: XCTestCase {
|
|||
|
||||
func testExceedingFileSizeLimit() throws {
|
||||
let factory = FileMockFactory()
|
||||
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
let data = Data([0, 0])
|
||||
|
||||
XCTAssertThrowsError(try writer.write(data), "file size limit exceeded") { (error) in
|
||||
|
@ -59,7 +59,7 @@ class FileWriterTests: XCTestCase {
|
|||
|
||||
func testSynchronizingAndClosingFile() throws {
|
||||
let factory = FileMockFactory()
|
||||
let writer = try FileWriter(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
|
||||
writer.synchronizeAndCloseFile()
|
||||
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
|
||||
}
|
Loading…
Add table
Reference in a new issue