Skip to content

Commit 0583f2c

Browse files
authored
Breadcrumb overflow (#119)
* Breadcrumb overflow * Version update * Fix a problem with invalid breadcrumb file size * Add support for remove subrange * Updated changelog
1 parent 92a658a commit 0583f2c

File tree

7 files changed

+53
-19
lines changed

7 files changed

+53
-19
lines changed

Backtrace.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Pod::Spec.new do |s|
1010

1111
s.name = "Backtrace"
12-
s.version = "2.0.1"
12+
s.version = "2.0.2"
1313
s.swift_version = '5'
1414
s.summary = "Backtrace's integration with iOS, macOS and tvOS"
1515
s.description = "Reliable crash and hang reporting for iOS, macOS and tvOS."

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Backtrace Cocoa Release Notes
22

3+
## Version 2.0.2
4+
- Fixed infinity loop generated by the breadcrumb overflow
5+
- Adjusted device.model attribute - now the attribute shows device model, rather than the model id
6+
37
## Version 2.0.1
48
- Added application.session and application.version attribute as defaults - no matter if the metrics integration is enabled or not.
59
- Added application.build attribute that represents an app build version.

Sources/Features/Attributes/DefaultAttributes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct Device: AttributesSource {
101101
var immutable: [String: Any?] {
102102
return [
103103
"device.machine": try? System.machine(),
104-
"device.model": try? System.model(),
104+
"device.model": try? System.machine(),
105105
"uname.sysname": getSysname()
106106
]
107107
}
@@ -207,7 +207,7 @@ struct LibInfo: AttributesSource {
207207
private static let applicationGuidKey = "backtrace.unique.user.identifier"
208208
private static let applicationLangName = "backtrace-cocoa"
209209

210-
var backtraceVersion = "2.0.1"
210+
var backtraceVersion = "2.0.2"
211211

212212
var immutable: [String: Any?] {
213213
return ["guid": LibInfo.guid(store: UserDefaultsStore.self).uuidString,

Sources/Features/Breadcrumb/BacktraceBreadcrumbFile.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ enum BacktraceBreadcrumbFileError: Error {
1717
self.breadcrumbLogURL = try breadcrumbSettings.getBreadcrumbLogPath()
1818
self.queue = Queue<BreadcrumbRecord>()
1919
self.maximumIndividualBreadcrumbSize = breadcrumbSettings.maxIndividualBreadcrumbSizeBytes
20-
if breadcrumbSettings.maxQueueFileSizeBytes < BacktraceBreadcrumbFile.minimumQueueFileSizeBytes {
21-
BacktraceLogger.warning("\(breadcrumbSettings.maxQueueFileSizeBytes) is smaller than the minimum of " +
22-
"\(BacktraceBreadcrumbFile.minimumQueueFileSizeBytes)" +
23-
", ignoring value and overriding with minimum.")
24-
self.maxQueueFileSizeBytes = BacktraceBreadcrumbFile.minimumQueueFileSizeBytes
25-
} else {
26-
self.maxQueueFileSizeBytes = breadcrumbSettings.maxQueueFileSizeBytes
27-
}
20+
self.maxQueueFileSizeBytes = breadcrumbSettings.maxQueueFileSizeBytes
2821

2922
super.init()
3023
}
@@ -60,9 +53,7 @@ enum BacktraceBreadcrumbFileError: Error {
6053
let breadcrumbSize = queueBreadcrumb.size
6154
// Pop last element if size is greater than maxQueueFileSizeBytes
6255
if size + breadcrumbSize > maxQueueFileSizeBytes && !queue.isEmpty {
63-
while (index != 0) {
64-
_ = queue.pop(at: index)
65-
}
56+
queue.removeSubrange(range: (0...index))
6657
break
6758
}
6859
let breadcrumbJsonData = queueBreadcrumb.json

Sources/Features/Breadcrumb/BacktraceBreadcrumbs.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
case user = 6
1111
case configuration = 7
1212

13-
var description: String {
13+
public var description: String {
1414
switch self {
1515
case .manual:
1616
return "manual"
@@ -42,7 +42,7 @@ import Foundation
4242
case error = 4
4343
case fatal = 5
4444

45-
var description: String {
45+
public var description: String {
4646
switch self {
4747
case .debug:
4848
return "debug"
@@ -101,7 +101,7 @@ import Foundation
101101
BreadcrumbsInfo.currentBreadcrumbsId = nil
102102
}
103103

104-
func addBreadcrumb(_ message: String,
104+
public func addBreadcrumb(_ message: String,
105105
attributes: [String: String]? = nil,
106106
type: BacktraceBreadcrumbType = BacktraceBreadcrumbType.manual,
107107
level: BacktraceBreadcrumbLevel = BacktraceBreadcrumbLevel.info) -> Bool {
@@ -123,7 +123,7 @@ import Foundation
123123
return breadcrumbsLogManager?.clear() ?? false
124124
}
125125

126-
var getCurrentBreadcrumbId: Int? {
126+
public var getCurrentBreadcrumbId: Int? {
127127
return breadcrumbsLogManager?.getCurrentBreadcrumbId
128128
}
129129
}

Sources/Features/Breadcrumb/Queue.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ public class Queue<T>: NSObject {
2020
return elements.first
2121
}
2222

23+
func removeSubrange(range: ClosedRange<Int>) {
24+
elements.removeSubrange(range);
25+
}
26+
2327
func remove(at index: Int) -> T? {
2428
guard index < elements.count else {
2529
return nil
2630
}
31+
32+
if (index < 0) {
33+
return nil
34+
}
2735
return elements.remove(at: index)
2836
}
2937

Tests/BacktraceBreadcrumbTests.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class BacktraceBreadcrumbTests: QuickSpec {
4545
}
4646
}
4747
context("when constructed") {
48-
it("Clear clears the file") {
48+
it("Clears the file") {
4949
expect { manager?.addBreadcrumb("this is a Breadcrumb",
5050
attributes: nil,
5151
type: BacktraceBreadcrumbType.system,
@@ -151,6 +151,37 @@ final class BacktraceBreadcrumbTests: QuickSpec {
151151
}
152152
}
153153
context("rollover and async tests") {
154+
it("should remove old breadcrumb and add a new one") {
155+
let settings = BacktraceBreadcrumbSettings()
156+
let maximumNumberOfBreadcrumbs = 4
157+
let breadcrumbMessage = "this is test"
158+
let breadcrumbLevel = BacktraceBreadcrumbLevel.debug
159+
let breadcrumbType = BacktraceBreadcrumbType.log
160+
let breadcrumb: [String: Any] = ["timestamp": Date().millisecondsSince1970,
161+
"id": 1,
162+
"level": breadcrumbLevel.description,
163+
"type": breadcrumbType.description,
164+
"message": breadcrumbMessage]
165+
166+
let breadcrumbJsonData = try JSONSerialization.data(withJSONObject: breadcrumb)
167+
let breadcrumbJsonString = String(data: breadcrumbJsonData, encoding: .utf8)
168+
let breadcrumbSize = breadcrumbJsonString!.count
169+
170+
171+
settings.maxQueueFileSizeBytes = breadcrumbSize * maximumNumberOfBreadcrumbs + maximumNumberOfBreadcrumbs
172+
breadcrumbs.enableBreadcrumbs(settings)
173+
174+
for index in (0...maximumNumberOfBreadcrumbs) {
175+
_ = breadcrumbs.addBreadcrumb("\(breadcrumbMessage)\(index)", type: breadcrumbType, level: breadcrumbLevel)
176+
}
177+
178+
// expect to clean up the file
179+
_ = breadcrumbs.addBreadcrumb("\(breadcrumbMessage)cleanup", type: breadcrumbType, level: breadcrumbLevel)
180+
let breadcrumbText = self.readBreadcrumbText()
181+
expect { breadcrumbText }.notTo(contain("\(breadcrumbMessage)0"))
182+
expect { breadcrumbText }.to(contain("\(breadcrumbMessage)cleanup"))
183+
}
184+
154185
it("rolls over after enough breadcrumbs are added to get to the maximum file size") {
155186
let settings = BacktraceBreadcrumbSettings()
156187
settings.maxQueueFileSizeBytes = 32 * 1024

0 commit comments

Comments
 (0)