Merge branch 'feature/migrate_to_ios_13_file_handle_methods' into develop

This commit is contained in:
Wojciech Nagrodzki 2019-11-05 16:38:08 +01:00
commit eed8485760
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
7 changed files with 138 additions and 101 deletions

View file

@ -1,5 +1,5 @@
language: swift language: swift
osx_image: xcode10.2 osx_image: xcode11.2
xcode_project: Logger.xcodeproj xcode_project: Logger.xcodeproj
script: xcodebuild test -project Logger.xcodeproj -scheme Logger -destination 'platform=iOS Simulator,name=iPhone XS,OS=12.2' script: xcodebuild test -project Logger.xcodeproj -scheme Logger -destination 'platform=iOS Simulator,name=iPhone 11,OS=13.2'
after_success: bash <(curl -s https://codecov.io/bash) after_success: bash <(curl -s https://codecov.io/bash)

View file

@ -370,7 +370,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES; MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES; ONLY_ACTIVE_ARCH = YES;
@ -425,7 +425,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO; MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES; MTL_FAST_MATH = YES;
SDKROOT = iphoneos; SDKROOT = iphoneos;

View file

@ -46,6 +46,7 @@ protocol LogrotateFactory {
} }
/// Logger that writes messages into the file at specified URL with log rotation support. /// Logger that writes messages into the file at specified URL with log rotation support.
@available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public final class DiskLogger: Logger { public final class DiskLogger: Logger {
private let fileURL: URL private let fileURL: URL
@ -97,7 +98,7 @@ public final class DiskLogger: Logger {
try self.writeBuffer() try self.writeBuffer()
} }
catch is SizeLimitedFileQuotaReached { catch is SizeLimitedFileQuotaReached {
self.closeSizeLimitedFile() try self.closeSizeLimitedFile()
try self.rotateLogFiles() try self.rotateLogFiles()
try self.openSizeLimitedFile() try self.openSizeLimitedFile()
try self.writeBuffer() try self.writeBuffer()
@ -124,8 +125,8 @@ public final class DiskLogger: Logger {
buffer.removeAll() buffer.removeAll()
} }
private func closeSizeLimitedFile() { private func closeSizeLimitedFile() throws {
self.sizeLimitedFile.synchronizeAndCloseFile() try self.sizeLimitedFile.synchronizeAndCloseFile()
self.sizeLimitedFile = nil self.sizeLimitedFile = nil
} }

View file

@ -9,15 +9,45 @@
import Foundation import Foundation
protocol OSFileHandle { protocol OSFileHandle {
func seekToEndOfFile() -> UInt64 func osSeekToEndOfFile() throws -> UInt64
func swift_write(_ data: Data) throws func osWrite(_ data: Data) throws
func synchronizeFile() func osSynchronizeFile() throws
func closeFile() func osCloseFile() throws
} }
extension FileHandle: OSFileHandle { extension FileHandle: OSFileHandle {
func swift_write(_ data: Data) throws { func osSeekToEndOfFile() throws -> UInt64 {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
var offsetInFile: UInt64 = 0
try __seek(toEndReturningOffset:&offsetInFile)
return offsetInFile
} else {
fatalError()
}
}
func osWrite(_ data: Data) throws {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try __write(data, error: ()) try __write(data, error: ())
} else {
fatalError()
}
}
func osSynchronizeFile() throws {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try synchronize()
} else {
fatalError()
}
}
func osCloseFile() throws {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
try close()
} else {
fatalError()
}
} }
} }

View file

@ -42,7 +42,7 @@ protocol SizeLimitedFile {
func write(_ data: Data) throws func write(_ data: Data) throws
/// Writes all in-memory data to permanent storage and closes the file. /// Writes all in-memory data to permanent storage and closes the file.
func synchronizeAndCloseFile() func synchronizeAndCloseFile() throws
} }
/// Allows writing to a file while respecting allowed size limit. /// Allows writing to a file while respecting allowed size limit.
@ -61,7 +61,7 @@ final class SizeLimitedFileImpl {
init(fileURL: URL, fileSizeLimit: UInt64, fileFactory: FileHandleFactory) throws { init(fileURL: URL, fileSizeLimit: UInt64, fileFactory: FileHandleFactory) throws {
file = try fileFactory.makeInstance(forWritingTo: fileURL) file = try fileFactory.makeInstance(forWritingTo: fileURL)
self.sizeLimit = fileSizeLimit self.sizeLimit = fileSizeLimit
currentSize = file.seekToEndOfFile() currentSize = try file.osSeekToEndOfFile()
} }
} }
@ -72,12 +72,12 @@ extension SizeLimitedFileImpl: SizeLimitedFile {
guard currentSize + dataSize <= sizeLimit else { guard currentSize + dataSize <= sizeLimit else {
throw SizeLimitedFileQuotaReached() throw SizeLimitedFileQuotaReached()
} }
try file.swift_write(data) try file.osWrite(data)
currentSize += dataSize currentSize += dataSize
} }
func synchronizeAndCloseFile() { func synchronizeAndCloseFile() throws {
file.synchronizeFile() try file.osSynchronizeFile()
file.closeFile() try file.osCloseFile()
} }
} }

View file

@ -30,6 +30,7 @@ class DiskLoggerTests: XCTestCase {
let logURL = URL(fileURLWithPath: "/var/log/application.log") let logURL = URL(fileURLWithPath: "/var/log/application.log")
func testLoggingMessageToFile() { func testLoggingMessageToFile() {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile") let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 1 expectation.expectedFulfillmentCount = 1
@ -52,12 +53,14 @@ class DiskLoggerTests: XCTestCase {
// "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n" // "2018-08-30 17:23:11.514 <crit> DiskLoggerTests:46 testWritingMessageToFile() a message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self) let loggedMessage = String(decoding: sizeLimitedFileFactory.files[0].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:46 testLoggingMessageToFile() message\n" let expectedSuffix = " <crit> DiskLoggerTests:47 testLoggingMessageToFile() message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix)) XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
} }
}
func testExceedingFileSizeLimit() { func testExceedingFileSizeLimit() {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile") let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2 expectation.expectedFulfillmentCount = 2
@ -81,12 +84,14 @@ class DiskLoggerTests: XCTestCase {
// "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n" // "2018-08-31 18:29:34.748 <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n"
let loggedMessage = String(decoding: sizeLimitedFileFactory.files[1].data, as: UTF8.self) let loggedMessage = String(decoding: sizeLimitedFileFactory.files[1].data, as: UTF8.self)
let expectedSuffix = " <crit> DiskLoggerTests:75 testExceedingFileSizeLimit() 2st message\n" let expectedSuffix = " <crit> DiskLoggerTests:78 testExceedingFileSizeLimit() 2st message\n"
XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix)) XCTAssertTrue(loggedMessage.hasSuffix(expectedSuffix))
} }
}
func testErrorDuringLogProcess() { func testErrorDuringLogProcess() {
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile") let expectation = XCTestExpectation(description: "write(_:) was called on SizeLimitedFile")
expectation.expectedFulfillmentCount = 2 expectation.expectedFulfillmentCount = 2
@ -115,6 +120,7 @@ class DiskLoggerTests: XCTestCase {
XCTAssertTrue(loggedMessage.contains(expectedOccurence)) XCTAssertTrue(loggedMessage.contains(expectedOccurence))
} }
} }
}
private class FileManagerStub: OSFileManager { private class FileManagerStub: OSFileManager {
func itemExists(at URL: URL) -> Bool { func itemExists(at URL: URL) -> Bool {

View file

@ -60,7 +60,7 @@ class SizeLimitedFileTests: XCTestCase {
func testSynchronizingAndClosingFile() throws { func testSynchronizingAndClosingFile() throws {
let factory = FileMockFactory() let factory = FileMockFactory()
let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory) let writer = try SizeLimitedFileImpl(fileURL: logURL, fileSizeLimit: 1, fileFactory: factory)
writer.synchronizeAndCloseFile() try writer.synchronizeAndCloseFile()
XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1) XCTAssertTrue(factory.mock.synchronizeFileCallCount == 1 && factory.mock.closeFileCallCount == 1)
} }
} }
@ -89,19 +89,19 @@ private class FileMock: OSFileHandle {
private(set) var synchronizeFileCallCount = 0 private(set) var synchronizeFileCallCount = 0
private(set) var closeFileCallCount = 0 private(set) var closeFileCallCount = 0
func seekToEndOfFile() -> UInt64 { func osSeekToEndOfFile() throws -> UInt64 {
return 0 return 0
} }
func swift_write(_ data: Data) throws { func osWrite(_ data: Data) throws {
self.writtenData.append(data) self.writtenData.append(data)
} }
func synchronizeFile() { func osSynchronizeFile() throws {
synchronizeFileCallCount += 1 synchronizeFileCallCount += 1
} }
func closeFile() { func osCloseFile() throws {
closeFileCallCount += 1 closeFileCallCount += 1
} }
} }