Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
fix: logs are incomplete (#3)
Browse files Browse the repository at this point in the history
- flush before zip logs
  • Loading branch information
iHugo-Tang authored Jul 30, 2022
1 parent 5069c16 commit a5ef751
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
Binary file added README.assets/image-20220731005733085.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added README.assets/image-20220731005753835.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ let url = try fileLogHandler.zipLogs()

![zip logs](docs/images/image-20220730230255924.png)

For iOS side, you can share logs through `UIActivityViewController`, e.g.

```swift
guard let url = try? hadesLogger.zipLogs() else {
return
}
var filesToShare = [Any]()
filesToShare.append(url)
let activityViewController = UIActivityViewController(
activityItems: filesToShare,
applicationActivities: nil
)
```

<img src="docs/images/image-20220731005753835.png" alt="share logs at iOS device" style="zoom: 25%;" />

## Class Diagram

![class diagram](./docs/01.class_diagram.png)
2 changes: 1 addition & 1 deletion Sources/HadesLogger/HadesLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct HadesLogger {
private let oslogHandler: OSLogHandler
private let fileLogHandler: FileLogHandler

init(label: String) {
public init(label: String) {
self.label = label
oslogHandler = OSLogHandler(label: label)
fileLogHandler = FileLogHandler()
Expand Down
10 changes: 10 additions & 0 deletions Sources/HadesLogger/Handler/FileLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ class FileLogHandler: LogHandler {
/// zip all logs into a zip file
/// - Returns: a url of log file
func zipLogs() throws -> URL? {
try loggerQueue?.sync {
try? self._flush(false)
return try self._zipLogs()
}
}

private func _zipLogs() throws -> URL? {
let archiveURL = logFileManager.logDirectory.appendingPathComponent("logs.zip")
if logFileManager.isExistsDirectory(at: archiveURL) {
try logFileManager.remove(at: archiveURL)
}
guard let archive = Archive(url: archiveURL, accessMode: .create) else {
return nil
}
Expand Down
60 changes: 27 additions & 33 deletions Tests/HadesLoggerTests/FileLogHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,41 @@ class FileLogHandlerTests: XCTestCase {
for i in 0..<40 {
log.debug("test \(i)")
}
try fileLogHandler.flush()
try queue.sync {
let baseURL = try! FileManager.default.url(
for: .cachesDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
let logDirectory = baseURL.appendingPathComponent("hades-log", isDirectory: true)
let logFileManager = LogFileManager(logDirectory: logDirectory)
let logs = try logFileManager.queryLogFiles()
if let logFileURL = logs.first?.url {
let content = try String(contentsOf: logFileURL)
XCTAssert(content.hasSuffix("test 39"), "This file should end with 'test 39' according to the code at line 50.")
}
for log in logs {
XCTAssert(log.size <= fileLogHandler.maximumFileSize)
}
if logs.count >= 2 {
XCTAssert((logs[0].size + logs[1].size) > fileLogHandler.maximumFileSize)
}
let baseURL = try! FileManager.default.url(
for: .cachesDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
let logDirectory = baseURL.appendingPathComponent("hades-log", isDirectory: true)
let logFileManager = LogFileManager(logDirectory: logDirectory)
let logs = try logFileManager.queryLogFiles()
if let logFileURL = logs.first?.url {
let content = try String(contentsOf: logFileURL)
XCTAssert(content.hasSuffix("test 39"), "This file should end with 'test 39' according to the code at line 50.")
}
for log in logs {
XCTAssert(log.size <= fileLogHandler.maximumFileSize)
}
if logs.count >= 2 {
XCTAssert((logs[0].size + logs[1].size) > fileLogHandler.maximumFileSize)
}
}

func testZip() throws {
for i in 0..<40 {
log.debug("test \(i)")
}
try fileLogHandler.flush()

try queue.sync {
guard let url = try fileLogHandler.zipLogs() else {
XCTAssert(false)
return
}
let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
guard let fileSize = attributes[.size] as? Int else {
XCTAssert(false)
return
}
XCTAssert(fileSize > 2_000 )
guard let url = try fileLogHandler.zipLogs() else {
XCTAssert(false)
return
}
let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
guard let fileSize = attributes[.size] as? Int else {
XCTAssert(false)
return
}
XCTAssert(fileSize > 2_000 )
}
}
Binary file added docs/images/image-20220731005753835.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a5ef751

Please sign in to comment.