Skip to content

Commit 7b80b56

Browse files
authored
Introduce storageGroup on Output (#9)
* Add storageGroup to Output * Fix tests for storageGroup * Encode filename to base16 * Add IDEWorkspaceChecks * Add ignore rules for testing * Add comment * Modify comment * Remove fileName prefix
1 parent 3304113 commit 7b80b56

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Sources/Puree/BufferedOutput.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ open class BufferedOutput: Output {
7676
public func emit(log: LogEntry) {
7777
buffer.insert(log)
7878

79-
logStore.add(log, for: tagPattern.pattern, completion: nil)
79+
logStore.add(log, for: storageGroup, completion: nil)
8080

8181
if buffer.count >= logLimit {
8282
flush()
@@ -87,6 +87,11 @@ open class BufferedOutput: Output {
8787
completion(false)
8888
}
8989

90+
open var storageGroup: String {
91+
let typeName = String(describing: type(of: self))
92+
return "\(tagPattern.pattern)_\(typeName)"
93+
}
94+
9095
private func setUpTimer() {
9196
self.timer?.invalidate()
9297

@@ -112,7 +117,7 @@ open class BufferedOutput: Output {
112117
private func reloadLogStore() {
113118
buffer.removeAll()
114119

115-
logStore.retrieveLogs(of: tagPattern.pattern) { logs in
120+
logStore.retrieveLogs(of: storageGroup) { logs in
116121
buffer = buffer.union(logs)
117122
}
118123
}
@@ -139,7 +144,7 @@ open class BufferedOutput: Output {
139144
private func callWriteChunk(_ chunk: Chunk) {
140145
write(chunk) { success in
141146
if success {
142-
self.logStore.remove(chunk.logs, from: self.tagPattern.pattern, completion: nil)
147+
self.logStore.remove(chunk.logs, from: self.storageGroup, completion: nil)
143148
return
144149
}
145150

Sources/Puree/LogStore/FileLogStore.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ struct SystemFileManager: FileManagerProtocol {
4949
}
5050

5151
public class FileLogStore: LogStore {
52-
private static let baseFileName = "com.cookpad.Puree.FileLogStore"
5352
private static let directoryName = "PureeLogs"
5453
private var bundle: Bundle = Bundle.main
5554
private var baseDirectoryURL: URL!
5655

5756
public static let `default` = FileLogStore()
5857

59-
func fileURL(for outputTagPattern: String) -> URL {
60-
let fileName = "\(FileLogStore.baseFileName)_\(outputTagPattern)"
61-
return baseDirectoryURL.appendingPathComponent(fileName)
58+
private func fileURL(for group: String) -> URL {
59+
// Tag patterns usually contain '*'. However we don't want to use special characters in filenames
60+
// so encode file names to Base16
61+
return baseDirectoryURL.appendingPathComponent(encodeToBase16(group))
6262
}
6363
private var fileManager: FileManagerProtocol = SystemFileManager()
6464

@@ -110,4 +110,8 @@ public class FileLogStore: LogStore {
110110
try? fileManager.removeDirectory(at: baseDirectoryURL)
111111
try? createCachesDirectory()
112112
}
113+
114+
private func encodeToBase16(_ string: String) -> String {
115+
return string.data(using: .utf8)!.map { String(format: "%02hhx", $0) }.joined()
116+
}
113117
}

Sources/Puree/TagPattern.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ private let separator: Character = "."
44
private let allWildcard = "**"
55
private let wildcard = "*"
66

7-
public struct TagPattern {
7+
public struct TagPattern: CustomStringConvertible {
88
struct Match {
99
let captured: String?
1010
}
1111

1212
public let pattern: String
1313

14+
public var description: String {
15+
return pattern
16+
}
17+
1418
public init?(string patternString: String) {
1519
if TagPattern.isValidPattern(patternString) {
1620
pattern = patternString

Tests/.swiftlint.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
disabled_rules:
22
- force_try
33
- force_cast
4+
- nesting
5+
- identifier_name

Tests/PureeTests/BufferedOutputTests.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BufferedOutputTests: XCTestCase {
3535

3636
func testBufferedOutput() {
3737
output.configuration.logEntryCountLimit = 1
38-
XCTAssertEqual(logStore.logs(for: "pv").count, 0)
38+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 0)
3939
XCTAssertEqual(output.calledWriteCount, 0)
4040
output.emit(log: makeLog())
4141
XCTAssertEqual(output.calledWriteCount, 1)
@@ -46,21 +46,21 @@ class BufferedOutputTests: XCTestCase {
4646
output.configuration.flushInterval = 1
4747

4848
let storedLogs: Set<LogEntry> = Set((0..<10).map { _ in LogEntry(tag: "pv", date: Date()) })
49-
logStore.add(storedLogs, for: "pv", completion: nil)
49+
logStore.add(storedLogs, for: "pv_TestingBufferedOutput", completion: nil)
5050

51-
XCTAssertEqual(logStore.logs(for: "pv").count, 10)
51+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 10)
5252
XCTAssertEqual(output.calledWriteCount, 0)
5353

5454
output.resume()
5555

56-
XCTAssertEqual(logStore.logs(for: "pv").count, 0)
56+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 0)
5757
XCTAssertEqual(output.calledWriteCount, 1)
5858
}
5959

6060
func testBufferedOutputFlushedByInterval() {
6161
output.configuration.logEntryCountLimit = 10
6262
output.configuration.flushInterval = 1
63-
XCTAssertEqual(logStore.logs(for: "pv").count, 0)
63+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 0)
6464
XCTAssertEqual(output.calledWriteCount, 0)
6565
output.emit(log: makeLog())
6666
XCTAssertEqual(output.calledWriteCount, 0)
@@ -75,7 +75,7 @@ class BufferedOutputTests: XCTestCase {
7575
func testBufferedOutputNotFlushed() {
7676
output.configuration.logEntryCountLimit = 10
7777
output.configuration.flushInterval = 10
78-
XCTAssertEqual(logStore.logs(for: "pv").count, 0)
78+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 0)
7979
XCTAssertEqual(output.calledWriteCount, 0)
8080
output.emit(log: makeLog())
8181
XCTAssertEqual(output.calledWriteCount, 0)
@@ -88,11 +88,11 @@ class BufferedOutputTests: XCTestCase {
8888

8989
func testHittingLogLimit() {
9090
output.configuration.logEntryCountLimit = 10
91-
XCTAssertEqual(logStore.logs(for: "pv").count, 0)
91+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 0)
9292
XCTAssertEqual(output.calledWriteCount, 0)
9393
for i in 1..<10 {
9494
output.emit(log: makeLog())
95-
XCTAssertEqual(logStore.logs(for: "pv").count, i)
95+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, i)
9696
}
9797
XCTAssertEqual(output.calledWriteCount, 0)
9898

@@ -112,23 +112,23 @@ class BufferedOutputTests: XCTestCase {
112112

113113
var expectation = self.expectation(description: "retry writeChunk")
114114
XCTAssertEqual(output.calledWriteCount, 1)
115-
XCTAssertEqual(logStore.logs(for: "pv").count, 10)
115+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 10)
116116
output.writeCallback = {
117117
expectation.fulfill()
118118
}
119119
wait(for: [expectation], timeout: 1.0)
120120

121121
expectation = self.expectation(description: "retry writeChunk")
122122
XCTAssertEqual(output.calledWriteCount, 2)
123-
XCTAssertEqual(logStore.logs(for: "pv").count, 10)
123+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 10)
124124
output.writeCallback = {
125125
expectation.fulfill()
126126
}
127127
wait(for: [expectation], timeout: 1.0)
128128

129129
expectation = self.expectation(description: "retry writeChunk")
130130
XCTAssertEqual(output.calledWriteCount, 3)
131-
XCTAssertEqual(logStore.logs(for: "pv").count, 10)
131+
XCTAssertEqual(logStore.logs(for: "pv_TestingBufferedOutput").count, 10)
132132
output.writeCallback = {
133133
expectation.fulfill()
134134
}

0 commit comments

Comments
 (0)