-
Notifications
You must be signed in to change notification settings - Fork 18
/
AppDelegate.swift
97 lines (80 loc) · 3.7 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import UIKit
import Backtrace
enum CustomError: Error {
case runtimeError
}
func throwingFunc() throws {
throw CustomError.runtimeError
}
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
let fileUrl = createAndWriteFile("sample.txt")
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let backtraceCredentials = BacktraceCredentials(endpoint: URL(string: Keys.backtraceUrl as String)!,
token: Keys.backtraceToken as String)
let backtraceDatabaseSettings = BacktraceDatabaseSettings()
backtraceDatabaseSettings.maxRecordCount = 10
let backtraceConfiguration = BacktraceClientConfiguration(credentials: backtraceCredentials,
dbSettings: backtraceDatabaseSettings,
reportsPerMin: 10,
allowsAttachingDebugger: true,
detectOOM: true)
BacktraceClient.shared = try? BacktraceClient(configuration: backtraceConfiguration)
BacktraceClient.shared?.attributes = ["foo": "bar", "testing": true]
BacktraceClient.shared?.attachments.append(fileUrl)
do {
try throwingFunc()
} catch {
BacktraceClient.shared?.send(attachmentPaths: []) { (result) in
print("AppDelegate:Result:\(result)")
}
}
BacktraceClient.shared?.delegate = self
BacktraceClient.shared?.loggingDestinations = [BacktraceBaseDestination(level: .debug)]
BacktraceClient.shared?.enableBreadcrumbs()
let attributes = ["My Attribute":"My Attribute Value"]
_ = BacktraceClient.shared?.addBreadcrumb("My Breadcrumb",
attributes: attributes,
type: .user,
level: .error)
return true
}
static func createAndWriteFile(_ fileName: String) -> URL {
let dirName = "directory"
let libraryDirectoryUrl = try! FileManager.default.url(
for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let directoryUrl = libraryDirectoryUrl.appendingPathComponent(dirName)
try? FileManager().createDirectory(
at: directoryUrl,
withIntermediateDirectories: false,
attributes: nil
)
let fileUrl = directoryUrl.appendingPathComponent(fileName)
let formatter = DateFormatter()
formatter.timeStyle = .medium
let myData = formatter.string(from: Date())
try! myData.write(to: fileUrl, atomically: true, encoding: .utf8)
return fileUrl
}
}
extension AppDelegate: BacktraceClientDelegate {
func willSend(_ report: BacktraceReport) -> BacktraceReport {
print("AppDelegate: willSend")
return report
}
func willSendRequest(_ request: URLRequest) -> URLRequest {
print("AppDelegate: willSendRequest")
return request
}
func serverDidRespond(_ result: BacktraceResult) {
print("AppDelegate:serverDidRespond: \(result)")
}
func connectionDidFail(_ error: Error) {
print("AppDelegate: connectionDidFail: \(error)")
}
func didReachLimit(_ result: BacktraceResult) {
print("AppDelegate: didReachLimit: \(result)")
}
}