Skip to content

Commit 5a88669

Browse files
authored
Attributes
1 parent f367fe0 commit 5a88669

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1667
-212
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 = "1.2.0"
12+
s.version = "1.3.0"
1313
s.summary = "Backtrace's integration with iOS and macOS"
1414
s.description = "Backtrace's integration with iOS and macOS for handling crashes"
1515
s.homepage = "https://backtrace.io/"

Backtrace.xcodeproj/project.pbxproj

Lines changed: 136 additions & 86 deletions
Large diffs are not rendered by default.

Example-iOS-ObjC/AppDelegate.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ @implementation AppDelegate
1010
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
1111
BacktraceCredentials *credentials = [[BacktraceCredentials alloc]
1212
initWithEndpoint: [NSURL URLWithString: @"https://backtrace.io"]
13-
token: @""];
14-
[BacktraceClient.shared registerWithCredentials: credentials];
15-
13+
token: @"token"];
14+
BacktraceClientConfiguration *configuration = [[BacktraceClientConfiguration alloc] initWithCredentials: credentials
15+
dbSettings: [BacktraceDatabaseSettings new]
16+
reportsPerMin: 3];
17+
BacktraceClient.shared = [[BacktraceClient alloc] initWithConfiguration: configuration error: nil];
1618
BacktraceClient.shared.delegate = self;
1719

1820
// sending NSException
@@ -37,7 +39,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3739
}
3840

3941
#pragma mark - BacktraceClientDelegate
40-
- (BacktraceCrashReport *)willSend:(BacktraceCrashReport *)report {
42+
- (BacktraceReport *)willSend:(BacktraceReport *)report {
4143
return report;
4244
}
4345

Example-iOS/AppDelegate.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717
func application(_ application: UIApplication,
1818
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1919
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: "https://backtrace.io")!,
20-
token: "")
21-
BacktraceClient.shared.register(credentials: backtraceCredentials)
22-
BacktraceClient.shared.delegate = self
20+
token: "token")
21+
let configuration = BacktraceClientConfiguration(credentials: backtraceCredentials)
22+
23+
BacktraceClient.shared = try? BacktraceClient(configuration: configuration)
24+
BacktraceClient.shared?.delegate = self
25+
BacktraceClient.shared?.userAttributes = ["foo": "bar", "testing": true]
2326

2427
do {
2528
try throwingFunc()
2629
} catch {
27-
BacktraceClient.shared.send { (result) in
30+
BacktraceClient.shared?.send { (result) in
2831
print(result)
2932
}
3033
}
@@ -34,7 +37,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3437
}
3538

3639
extension AppDelegate: BacktraceClientDelegate {
37-
func willSend(_ report: BacktraceCrashReport) -> (BacktraceCrashReport) {
40+
func willSend(_ report: BacktraceReport) -> (BacktraceReport) {
3841
return report
3942
}
4043

Example-iOS/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ViewController: UIViewController {
1010
}
1111

1212
@IBAction func liveReportAction(_ sender: Any) {
13-
BacktraceClient.shared.send { (result) in
13+
BacktraceClient.shared?.send { (result) in
1414
print(result)
1515
}
1616
}

Example-macOS-ObjC/AppDelegate.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ @interface AppDelegate ()
88
@implementation AppDelegate
99

1010
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
11-
// Insert code here to initialize your application
1211
BacktraceCredentials *credentials = [[BacktraceCredentials alloc]
1312
initWithEndpoint: [NSURL URLWithString: @"https://backtrace.io"]
14-
token: @""];
15-
[BacktraceClient.shared registerWithCredentials: credentials];
13+
token: @"token"];
14+
BacktraceClientConfiguration *configuration = [[BacktraceClientConfiguration alloc] initWithCredentials: credentials
15+
dbSettings: [BacktraceDatabaseSettings new]
16+
reportsPerMin: 3];
17+
BacktraceClient.shared = [[BacktraceClient alloc] initWithConfiguration: configuration error: nil];
18+
[BacktraceClient.shared setUserAttributes: @{@"foo": @"bar"}];
1619

1720
@try {
1821
NSArray *array = @[];
@@ -32,5 +35,4 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification {
3235
// Insert code here to tear down your application
3336
}
3437

35-
3638
@end

Example-macOS-ObjC/ViewController.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ - (void)viewDidLoad {
1515
// Do any additional setup after loading the view.
1616
}
1717
- (IBAction)crashAction:(id)sender {
18-
18+
NSArray *array = @[];
19+
NSObject *o = array[1];
1920
}
2021

2122
- (IBAction)liveReportAction:(id)sender {
2223

2324
}
2425

2526
- (IBAction)liveReportButtonAction:(id)sender {
26-
27+
NSArray *array = @[];
28+
NSObject *o = array[1];
2729
}
2830

2931
- (void)setRepresentedObject:(id)representedObject {

README.md

Lines changed: 90 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,55 @@
1313

1414
## Minimal usage
1515

16-
### Register the `BacktraceClient` using `register(endpoint:,token:)` method and then send error/exception just by calling method `send`:
16+
### Create the `BacktraceClient` using `init(credentials:)` initializer and then send error/exception just by calling method `send`:
1717

1818
- Swift
1919
```swift
20-
import UIKit
21-
import Backtrace
22-
23-
@UIApplicationMain
24-
class AppDelegate: UIResponder, UIApplicationDelegate {
25-
26-
var window: UIWindow?
27-
28-
func application(_ application: UIApplication,
29-
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
30-
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: "")!,
31-
token: "")
32-
BacktraceClient.shared.register(credentials: backtraceCredentials)
33-
34-
do {
35-
try throwingFunc()
36-
} catch {
37-
BacktraceClient.shared.send { (result) in
38-
print(result)
39-
}
40-
}
41-
42-
return true
43-
}
44-
}
20+
import UIKit
21+
import Backtrace
22+
23+
@UIApplicationMain
24+
class AppDelegate: UIResponder, UIApplicationDelegate {
25+
26+
var window: UIWindow?
27+
28+
func application(_ application: UIApplication,
29+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
30+
31+
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: "https://backtrace.io")!,
32+
token: "token")
33+
BacktraceClient.shared = try? BacktraceClient(credentials: backtraceCredentials)
34+
35+
do {
36+
try throwingFunc()
37+
} catch {
38+
BacktraceClient.shared?.send { (result) in
39+
print(result)
40+
}
41+
}
42+
43+
return true
44+
}
45+
}
4546
```
4647

4748
- Objective-C
4849
```objective-c
49-
#import "AppDelegate.h"
50-
@import Backtrace;
50+
#import "AppDelegate.h"
51+
@import Backtrace;
5152

52-
@interface AppDelegate ()
53+
@interface AppDelegate ()
5354

54-
@end
55+
@end
5556

56-
@implementation AppDelegate
57+
@implementation AppDelegate
5758

58-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
59+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
60+
5961
BacktraceCredentials *credentials = [[BacktraceCredentials alloc]
60-
initWithEndpoint: [NSURL URLWithString: @""]
61-
token: @""];
62-
[BacktraceClient.shared registerWithCredentials: credentials];
62+
initWithEndpoint: [NSURL URLWithString: @"https://backtrace.io"]
63+
token: @"token"];
64+
BacktraceClient.shared = [[BacktraceClient alloc] initWithCredentials: credentials error: nil];
6365

6466
// sending NSException
6567
@try {
@@ -80,9 +82,9 @@
8082
}];
8183

8284
return YES;
83-
}
85+
}
8486

85-
@end
87+
@end
8688
```
8789
8890
# Features Summary <a name="features-summary"></a>
@@ -116,11 +118,60 @@ Register to Backtrace services using provided submission url (see: <a href="http
116118
117119
- Swift
118120
```swift
119-
BacktraceClient.shared.register(credentials: BacktraceCredentials)
121+
BacktraceClient.shared = try? BacktraceClient(credentials: BacktraceCredentials)
120122
```
121123
- Objective-C
122124
```objective-c
123-
[[BacktraceClient shared] registerWithCredentials: BacktraceCredentials];
125+
BacktraceClient.shared = [[BacktraceClient alloc] initWithCredentials: BacktraceCredentials error: error];
126+
```
127+
128+
## Backtrace client configuration
129+
For more advanced usage of BacktraceClient, you can supply BacktraceClientConfiguration as a parameter. See the following example:
130+
```swift
131+
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: "https://backtrace.io")!,
132+
token: "token")
133+
let configuration = BacktraceClientConfiguration(credentials: backtraceCredentials,
134+
dbSettings: BacktraceDatabaseSettings(),
135+
reportsPerMin: 10)
136+
BacktraceClient.shared = try? BacktraceClient(configuration: configuration)
137+
```
138+
139+
### Database settings
140+
BacktraceClient allows you to customize the initialization of BacktraceDatabase for local storage of error reports by supplying a BacktraceDatabaseSettings parameter, as follows:
141+
```swift
142+
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: "https://backtrace.io")!,
143+
token: "token")
144+
let backtraceDatabaseSettings = BacktraceDatabaseSettings()
145+
backtraceDatabaseSettings.maxRecordCount = 1000
146+
backtraceDatabaseSettings.maxDatabaseSize = 10
147+
backtraceDatabaseSettings.retryInterval = 5
148+
backtraceDatabaseSettings.retryLimit = 3
149+
backtraceDatabaseSettings.retryBehaviour = RetryBehaviour.interval
150+
backtraceDatabaseSettings.retryOrder = RetryOder.queue
151+
let backtraceConfiguration = BacktraceClientConfiguration(credentials: backtraceCredentials,
152+
dbSettings: backtraceDatabaseSettings,
153+
reportsPerMin: 10)
154+
BacktraceClient.shared = try? BacktraceClient(configuration: backtraceConfiguration)
155+
```
156+
157+
### Events handling
158+
BacktraceClient allows you to subscribe for events produced before and after sending error report:
159+
- Swift
160+
```swift
161+
BacktraceClient.shared?.delegate = self
162+
163+
func willSend(_ report: BacktraceCrashReport) -> (BacktraceCrashReport)
164+
func willSendRequest(_ request: URLRequest) -> URLRequest
165+
func serverDidFail(_ error: Error)
166+
func serverDidResponse(_ result: BacktraceResult)
167+
func didReachLimit(_ result: BacktraceResult)
168+
```
169+
170+
### User attributes
171+
You can add custom user attributes that should be send alongside crash and erros/exceptions:
172+
- Swift
173+
```swift
174+
BacktraceClient.shared?.userAttributes = ["foo": "bar", "testing": true]
124175
```
125176

126177
## Backtrace client configuration
@@ -186,24 +237,6 @@ Registered `BacktraceClient` will be able to send an crash reports. Error report
186237
- (void) sendWithException: NSException completion: (void (^)(BacktraceResult * _Nonnull)) completion;
187238
```
188239

189-
# Architecture <a name="architecture"></a>
190-
191-
The library is written in pure Swift and Objective-C is fully supported as Swift codebase can be automatically exposed just by adding annotations:
192-
193-
```swift
194-
// pure swift class - cannot be exposed to Objective-C
195-
class PureSwiftClass {
196-
197-
}
198-
199-
// swift class with Objective-C support
200-
@objc class SwiftClass: NSObject {
201-
202-
}
203-
```
204-
205-
As Swift is meant to be open-source and platform independent the library relies on `protocols` which allows to provide default implementation but simultaneously gives you a lot of place for customisation.
206-
207240
# FAQ
208241
## Missing dSYM files
209242
Make sure your project is configured to generate the debug symbols:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
3+
struct Annotations {
4+
let environmentVariables: [String: String]
5+
let dependencies: [String: String]
6+
7+
init() {
8+
environmentVariables = ProcessInfo.processInfo.environment
9+
10+
let items = Bundle.allFrameworks.compactMap(FrameworkInfo.init)
11+
.map({($0.name, $0.version)})
12+
dependencies = Dictionary(items, uniquingKeysWith: { _, new in new })
13+
}
14+
}
15+
16+
struct FrameworkInfo {
17+
let name: String
18+
let version: String
19+
let identifier: String
20+
21+
init?(_ bundle: Bundle) {
22+
guard let name = bundle.infoDictionary?[kCFBundleNameKey as String] as? String,
23+
let version = bundle.infoDictionary?[kCFBundleVersionKey as String] as? String,
24+
let identifier = bundle.infoDictionary?[kCFBundleIdentifierKey as String] as? String else {
25+
return nil
26+
}
27+
self.name = name
28+
self.version = version
29+
self.identifier = identifier
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Foundation
2+
3+
final class AttributesProvider {
4+
// attributes can be modified on runtime
5+
var userAttributes: Attributes = [:]
6+
var defaultAttributes: Attributes {
7+
return DefaultAttributes.current()
8+
}
9+
}
10+
11+
extension AttributesProvider: SignalContext {
12+
var attributes: Attributes {
13+
return userAttributes + defaultAttributes
14+
}
15+
}
16+
17+
extension AttributesProvider: CustomStringConvertible {
18+
var description: String {
19+
return attributes.compactMap { "\($0.key): \($0.value)"}.joined(separator: "\n")
20+
}
21+
}

0 commit comments

Comments
 (0)