-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathSentryExtraContextProvider.swift
More file actions
78 lines (66 loc) · 3.14 KB
/
SentryExtraContextProvider.swift
File metadata and controls
78 lines (66 loc) · 3.14 KB
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
// swiftlint:disable missing_docs
@_implementationOnly import _SentryPrivate
@_spi(Private) @objc public final class SentryExtraContextProvider: NSObject {
private static let kSentryProcessInfoThermalStateNominal = "nominal"
private static let kSentryProcessInfoThermalStateFair = "fair"
private static let kSentryProcessInfoThermalStateSerious = "serious"
private static let kSentryProcessInfoThermalStateCritical = "critical"
private let crashWrapper: SentryCrashWrapper
private let processInfoWrapper: SentryProcessInfoSource
#if (os(iOS)) && !SENTRY_NO_UI_FRAMEWORK
private let deviceWrapper: SentryUIDeviceWrapper
init(crashWrapper: SentryCrashWrapper, processInfoWrapper: SentryProcessInfoSource, deviceWrapper: SentryUIDeviceWrapper) {
self.crashWrapper = crashWrapper
self.processInfoWrapper = processInfoWrapper
self.deviceWrapper = deviceWrapper
}
#else
init(crashWrapper: SentryCrashWrapper, processInfoWrapper: SentryProcessInfoSource) {
self.crashWrapper = crashWrapper
self.processInfoWrapper = processInfoWrapper
}
#endif
@objc public func getExtraContext() -> [String: Any] {
[
"device": getExtraDeviceContext(),
"app": getExtraAppContext()
]
}
private func getExtraDeviceContext() -> [String: Any] {
var extraDeviceContext: [String: Any] = [
SentryDeviceContextFreeMemoryKey: NSNumber(value: crashWrapper.freeMemorySize),
"processor_count": NSNumber(value: processInfoWrapper.processorCount)
]
let thermalState = processInfoWrapper.thermalState
switch thermalState {
case .nominal:
extraDeviceContext["thermal_state"] = Self.kSentryProcessInfoThermalStateNominal
case .fair:
extraDeviceContext["thermal_state"] = Self.kSentryProcessInfoThermalStateFair
case .serious:
extraDeviceContext["thermal_state"] = Self.kSentryProcessInfoThermalStateSerious
case .critical:
extraDeviceContext["thermal_state"] = Self.kSentryProcessInfoThermalStateCritical
default:
SentrySDKLog.warning("Unexpected thermal state enum value: \(thermalState)")
}
if #available(macOS 12.0, *) {
extraDeviceContext["low_power_mode"] = NSNumber(value: processInfoWrapper.isLowPowerModeEnabled)
}
#if (os(iOS)) && !SENTRY_NO_UI_FRAMEWORK
if deviceWrapper.orientation != .unknown {
extraDeviceContext["orientation"]
= deviceWrapper.orientation.isPortrait ? "portrait" : "landscape"
}
if deviceWrapper.isBatteryMonitoringEnabled {
extraDeviceContext["charging"] = NSNumber(value: deviceWrapper.batteryState == .charging)
extraDeviceContext["battery_level"] = NSNumber(value: Int(deviceWrapper.batteryLevel * 100))
}
#endif
return extraDeviceContext
}
private func getExtraAppContext() -> [String: Any] {
[ SentryDeviceContextAppMemoryKey: NSNumber(value: self.crashWrapper.appMemorySize) ]
}
}
// swiftlint:enable missing_docs