Skip to content

Commit

Permalink
Support new API about FileHandle (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichop authored Nov 19, 2020
1 parent 345ec4e commit 88b20f5
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 31 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DEFAULT_GOAL := help
HELP_INDENT := 28

SWIFT_VERSION := 5.3
SWIFT_VERSION := 5.3.1

.PHONY: help
help:
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1200;
LastUpgradeCheck = 1210;
LastUpgradeCheck = 1220;
TargetAttributes = {
C78B9219252DCFBD0026B9B1 = {
CreatedOnToolsVersion = 12.0.1;
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1210"
LastUpgradeVersion = "1220"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/xcshareddata/xcschemes/SwiftLint.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1210"
LastUpgradeVersion = "1220"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
40 changes: 32 additions & 8 deletions Sources/Puppy/FileLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FileLogger: 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()
Expand All @@ -26,12 +26,18 @@ public class FileLogger: BaseLogger {
}

public override func log(_ level: LogLevel, string: String) {
fileHandle?.seekToEndOfFile()
if let data = (string + "\r\n").data(using: .utf8) {
fileHandle?.write(data)
if flushmode == .always {
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
if flushmode == .always {
fileHandle?.synchronizeFile()
}
}
} catch {
print("seekToEnd error. error is \(error.localizedDescription).")
}
}

Expand Down Expand Up @@ -62,15 +68,15 @@ public class FileLogger: 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)
}

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)
}
Expand Down Expand Up @@ -100,3 +106,21 @@ public enum FlushMode {
case always
case manual
}

extension FileHandle {
func seekToEndCompatible() throws -> 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)
}
}
}
30 changes: 17 additions & 13 deletions Sources/Puppy/FileRotationLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -62,15 +67,15 @@ 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)
}

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)
}
Expand All @@ -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()
Expand All @@ -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])
Expand All @@ -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).")
}

}
Expand Down

0 comments on commit 88b20f5

Please sign in to comment.