-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
sentry_native_channel.dart
161 lines (136 loc) · 4.21 KB
/
sentry_native_channel.dart
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import '../sentry_flutter.dart';
/// Provide typed methods to access native layer.
@internal
class SentryNativeChannel {
SentryNativeChannel(this._channel, this._options);
final MethodChannel _channel;
final SentryFlutterOptions _options;
// TODO Move other native calls here.
Future<NativeAppStart?> fetchNativeAppStart() async {
try {
final json = await _channel
.invokeMapMethod<String, dynamic>('fetchNativeAppStart');
return (json != null) ? NativeAppStart.fromJson(json) : null;
} catch (error, stackTrace) {
_logError('fetchNativeAppStart', error, stackTrace);
return null;
}
}
Future<void> beginNativeFrames() async {
try {
await _channel.invokeMethod('beginNativeFrames');
} catch (error, stackTrace) {
_logError('beginNativeFrames', error, stackTrace);
}
}
Future<NativeFrames?> endNativeFrames(SentryId id) async {
try {
final json = await _channel.invokeMapMethod<String, dynamic>(
'endNativeFrames', {'id': id.toString()});
return (json != null) ? NativeFrames.fromJson(json) : null;
} catch (error, stackTrace) {
_logError('endNativeFrames', error, stackTrace);
return null;
}
}
Future<void> setUser(SentryUser? user) async {
try {
await _channel.invokeMethod('setUser', {'user': user?.toJson()});
} catch (error, stackTrace) {
_logError('setUser', error, stackTrace);
}
}
Future<void> addBreadcrumb(Breadcrumb breadcrumb) async {
try {
await _channel
.invokeMethod('addBreadcrumb', {'breadcrumb': breadcrumb.toJson()});
} catch (error, stackTrace) {
_logError('addBreadcrumb', error, stackTrace);
}
}
Future<void> clearBreadcrumbs() async {
try {
await _channel.invokeMethod('clearBreadcrumbs');
} catch (error, stackTrace) {
_logError('clearBreadcrumbs', error, stackTrace);
}
}
Future<void> setContexts(String key, dynamic value) async {
try {
await _channel.invokeMethod('setContexts', {'key': key, 'value': value});
} catch (error, stackTrace) {
_logError('setContexts', error, stackTrace);
}
}
Future<void> removeContexts(String key) async {
try {
await _channel.invokeMethod('removeContexts', {'key': key});
} catch (error, stackTrace) {
_logError('removeContexts', error, stackTrace);
}
}
Future<void> setExtra(String key, dynamic value) async {
try {
await _channel.invokeMethod('setExtra', {'key': key, 'value': value});
} catch (error, stackTrace) {
_logError('setExtra', error, stackTrace);
}
}
Future<void> removeExtra(String key) async {
try {
await _channel.invokeMethod('removeExtra', {'key': key});
} catch (error, stackTrace) {
_logError('removeExtra', error, stackTrace);
}
}
Future<void> setTag(String key, dynamic value) async {
try {
await _channel.invokeMethod('setTag', {'key': key, 'value': value});
} catch (error, stackTrace) {
_logError('setTag', error, stackTrace);
}
}
Future<void> removeTag(String key) async {
try {
await _channel.invokeMethod('removeTag', {'key': key});
} catch (error, stackTrace) {
_logError('removeTag', error, stackTrace);
}
}
// Helper
void _logError(String nativeMethodName, Object error, StackTrace stackTrace) {
_options.logger(
SentryLevel.error,
'Native call `$nativeMethodName` failed',
exception: error,
stackTrace: stackTrace,
);
}
}
class NativeAppStart {
NativeAppStart(this.appStartTime, this.isColdStart);
double appStartTime;
bool isColdStart;
factory NativeAppStart.fromJson(Map<String, dynamic> json) {
return NativeAppStart(
json['appStartTime'] as double,
json['isColdStart'] as bool,
);
}
}
class NativeFrames {
NativeFrames(this.totalFrames, this.slowFrames, this.frozenFrames);
int totalFrames;
int slowFrames;
int frozenFrames;
factory NativeFrames.fromJson(Map<String, dynamic> json) {
return NativeFrames(
json['totalFrames'] as int,
json['slowFrames'] as int,
json['frozenFrames'] as int,
);
}
}