forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRustErrors.swift
74 lines (62 loc) · 2.37 KB
/
RustErrors.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Common
import Foundation
import MozillaAppServices
public func initializeRustErrors(logger: Logger) {
setApplicationErrorReporter(errorReporter: FirefoxIOSErrorReporter(logger: logger))
}
/// The `AppServicesErrorReport` class (with its inheritance from `CustomCrashReport`) exists
/// to distinguish native Sentry reports from reports originating in A-S
private class AppServicesErrorReport: Error, CustomCrashReport {
var typeName: String
var message: String
init(typeName: String, message: String) {
self.typeName = typeName
self.message = message
}
}
/// The `FirefoxIOSErrorReporter` class contains the callbacks A-S uses to report Sentry errors and
/// breadcrumbs. These functions are not intended to be explicitly called in this repo.
private class FirefoxIOSErrorReporter: ApplicationErrorReporter {
var logger: Logger
init(logger: Logger) {
self.logger = logger
}
func reportError(typeName: String, message: String) {
logger.logCustomError(error: AppServicesErrorReport(typeName: typeName, message: message))
}
func reportBreadcrumb(message: String, module: String, line: UInt32, column: UInt32) {
logger.log("\(module)[\(line)]: \(message)",
level: .info,
category: .sync)
}
}
/// The `ForwardOnLog` class exists to support the rust-log-forwarder `setLogger` function.
internal class ForwardOnLog: AppServicesLogger {
var logger: Logger
init(logger: Logger) {
self.logger = logger
}
func log(record: Record) {
self.logger.log(record.message,
level: rustLevelToLoggerLevel(level: record.level),
category: LoggerCategory.sync,
extra: ["target": record.target])
}
private func rustLevelToLoggerLevel(level: Level) -> LoggerLevel {
switch level {
case .trace:
return LoggerLevel.debug
case .debug:
return LoggerLevel.debug
case .info:
return LoggerLevel.info
case .warn:
return LoggerLevel.warning
case .error:
return LoggerLevel.fatal
}
}
}