mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Use new FileHandle methods that throw errors instead of exceptions
This commit is contained in:
parent
3bc845ee28
commit
04ab08f294
4 changed files with 48 additions and 17 deletions
|
@ -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;
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
try __write(data, error: ())
|
if #available(iOS 13.0, *) {
|
||||||
|
var offsetInFile: UInt64 = 0
|
||||||
|
try __seek(toEndReturningOffset:&offsetInFile)
|
||||||
|
return offsetInFile
|
||||||
|
} else {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func osWrite(_ data: Data) throws {
|
||||||
|
if #available(iOS 13.0, *) {
|
||||||
|
try __write(data, error: ())
|
||||||
|
} else {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func osSynchronizeFile() throws {
|
||||||
|
if #available(iOS 13.0, *) {
|
||||||
|
try synchronize()
|
||||||
|
} else {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func osCloseFile() throws {
|
||||||
|
if #available(iOS 13.0, *) {
|
||||||
|
try close()
|
||||||
|
} else {
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue