From 88b20f5b2cf9da3572c0644e4c04b670a177b6ad Mon Sep 17 00:00:00 2001 From: Koichi Yokota Date: Fri, 20 Nov 2020 00:06:09 +0900 Subject: [PATCH] Support new API about FileHandle (#7) --- .github/workflows/ci.yml | 10 ++--- CHANGELOG.md | 6 ++- Makefile | 2 +- Puppy.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/xcschemes/Puppy.xcscheme | 2 +- .../xcshareddata/xcschemes/SwiftLint.xcscheme | 2 +- Sources/Puppy/FileLogger.swift | 40 +++++++++++++++---- Sources/Puppy/FileRotationLogger.swift | 30 ++++++++------ 8 files changed, 63 insertions(+), 31 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b2b69d..c9e6e71 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: make-tatget: [xcode-build, carthage-build-workaround, pod-lib-lint] - xcode: [12.1] + xcode: [12.2] fail-fast: false env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app @@ -30,7 +30,7 @@ jobs: runs-on: macos-latest strategy: matrix: - xcode: [12.1] + xcode: [12.2] fail-fast: false env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app @@ -46,7 +46,7 @@ jobs: runs-on: macos-latest strategy: matrix: - xcode: [12.1] + xcode: [12.2] fail-fast: false env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app @@ -59,7 +59,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - swift-version: [5.3] + swift-version: [5.3.1] fail-fast: false steps: - uses: actions/checkout@v2 @@ -70,7 +70,7 @@ jobs: runs-on: macos-latest strategy: matrix: - xcode: [12.1] + xcode: [12.2] fail-fast: false env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app diff --git a/CHANGELOG.md b/CHANGELOG.md index d1c4f26..309a22e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ +## [x.y.z](https://github.com/sushichop/Puppy/releases/tag/x.y.z) (yyyy-mm-dd) + +- Support new API about FileHandle. [#7](https://github.com/sushichop/Puppy/pull/7) + ## [0.1.1](https://github.com/sushichop/Puppy/releases/tag/0.1.1) (2020-10-20) -- Fix access level issue for use as a library. #4 +- Fix access level issue for use as a library. [#4](https://github.com/sushichop/Puppy/pull/4) ## [0.1.0](https://github.com/sushichop/Puppy/releases/tag/0.1.0) (2020-10-18) diff --git a/Makefile b/Makefile index 0570de3..5c8e316 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .DEFAULT_GOAL := help HELP_INDENT := 28 -SWIFT_VERSION := 5.3 +SWIFT_VERSION := 5.3.1 .PHONY: help help: diff --git a/Puppy.xcodeproj/project.pbxproj b/Puppy.xcodeproj/project.pbxproj index d44b5b4..6eb24f7 100644 --- a/Puppy.xcodeproj/project.pbxproj +++ b/Puppy.xcodeproj/project.pbxproj @@ -283,7 +283,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1200; - LastUpgradeCheck = 1210; + LastUpgradeCheck = 1220; TargetAttributes = { C78B9219252DCFBD0026B9B1 = { CreatedOnToolsVersion = 12.0.1; diff --git a/Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme b/Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme index 628fd07..da6ac7c 100644 --- a/Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme +++ b/Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme @@ -1,6 +1,6 @@ UInt64 { + if #available(macOS 10.15.4, iOS 13.4, tvOS 13.4, watchOS 6.2, *) { + return try seekToEnd() + } else { + return seekToEndOfFile() + } + } + + func writeCompatible(contentsOf data: Data) throws { + if #available(macOS 10.15.4, iOS 13.4, tvOS 13.4, watchOS 6.2, *) { + try write(contentsOf: data) + } else { + write(data) + } + } +} diff --git a/Sources/Puppy/FileRotationLogger.swift b/Sources/Puppy/FileRotationLogger.swift index 40fff9a..8ffd55f 100644 --- a/Sources/Puppy/FileRotationLogger.swift +++ b/Sources/Puppy/FileRotationLogger.swift @@ -19,7 +19,7 @@ public class FileRotationLogger: BaseLogger { public init(_ label: String, fileURL: URL) throws { self.fileURL = fileURL - debug("fileURL is \(fileURL)") + debug("fileURL is \(fileURL).") super.init(label) try validateFileURL(fileURL) try openFile() @@ -32,10 +32,15 @@ public class FileRotationLogger: BaseLogger { public override func log(_ level: LogLevel, string: String) { rotateFiles() - fileHandle?.seekToEndOfFile() - if let data = (string + "\r\n").data(using: .utf8) { - fileHandle?.write(data) - fileHandle?.synchronizeFile() + do { + _ = try fileHandle?.seekToEndCompatible() + if let data = (string + "\r\n").data(using: .utf8) { + // swiftlint:disable force_try + try! fileHandle?.writeCompatible(contentsOf: data) + // swiftlint:enable force_try + } + } catch { + print("seekToEnd error. error is \(error.localizedDescription).") } rotateFiles() @@ -62,7 +67,7 @@ public class FileRotationLogger: BaseLogger { let directoryURL = fileURL.deletingLastPathComponent() do { try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) - debug("created directoryURL is \(directoryURL)") + debug("created directoryURL is \(directoryURL).") } catch { throw FileError.creatingDirectoryFailed(at: directoryURL) } @@ -70,7 +75,7 @@ public class FileRotationLogger: BaseLogger { if !FileManager.default.fileExists(atPath: fileURL.path) { let successful = FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil) if successful { - debug("succeeded in creating filePath") + debug("succeeded in creating filePath.") } else { throw FileError.creatingFileFailed(at: fileURL) } @@ -96,8 +101,7 @@ public class FileRotationLogger: BaseLogger { } private func rotateFiles() { - let size = fileHandle.seekToEndOfFile() - if size > maxFileSize { + if let size = try? fileHandle.seekToEndCompatible(), size > maxFileSize { closeFile() do { let archivedFileURL = fileURL.deletingPathExtension() @@ -121,7 +125,7 @@ public class FileRotationLogger: BaseLogger { let creationDate1 = try FileManager.default.attributesOfItem(atPath: $1.path)[.modificationDate] as? Date return creationDate0!.timeIntervalSince1970 < creationDate1!.timeIntervalSince1970 } - debug("sortedArchivedFileURLs is \(sortedArchivedFileURLs)") + debug("sortedArchivedFileURLs is \(sortedArchivedFileURLs).") for index in 0 ..< archivedFileURLs.count - Int(maxArchivedFilesCount) { debug("\(sortedArchivedFileURLs[index]) will be removed...") try FileManager.default.removeItem(at: sortedArchivedFileURLs[index]) @@ -130,14 +134,14 @@ public class FileRotationLogger: BaseLogger { } } } catch { - print("archivedFileURLs error. error is \(error.localizedDescription)") + print("archivedFileURLs error. error is \(error.localizedDescription).") } do { - debug("will openFile in rotateFiles") + debug("will openFile in rotateFiles.") try openFile() } catch { - print("openFile error in rotating. error is \(error.localizedDescription)") + print("openFile error in rotating. error is \(error.localizedDescription).") } }