-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbugsee_manager.dart
More file actions
409 lines (357 loc) · 12.9 KB
/
bugsee_manager.dart
File metadata and controls
409 lines (357 loc) · 12.9 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import 'dart:async';
import 'dart:io';
import 'package:app/access/bugsee/bugsee_configuration_data.dart';
import 'package:app/access/bugsee/bugsee_repository.dart';
import 'package:app/access/persistence_exception.dart';
import 'package:app/business/bugsee/bugsee_config_state.dart';
import 'package:app/business/logger/logger_manager.dart';
import 'package:bugsee_flutter/bugsee_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:logger/logger.dart';
import 'package:logger/web.dart';
const String bugseeTokenFormat =
r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$';
const String bugseeFilterRegex = r'.';
/// Service related to initializing Bugsee service
abstract interface class BugseeManager {
factory BugseeManager({
required BugseeRepository bugseeRepository,
required Logger logger,
required LoggerManager loggerManager,
}) = _BugseeManager;
/// Current BugseeManager state
BugseeConfigState get bugseeConfigState;
/// Initialize bugsee with given token
/// bugsee is not available in debug mode
/// * [bugseeToken]: nullable bugsee token, if null bugsee won't be initialized make sure you provide
/// [BUGSEE_TOKEN] in the env using `--dart-define` or `launch.json` on vscode
Future<void> initialize({
String? bugseeToken,
});
/// Manually log a provided exception with a stack trace
/// (medium severity exception in Bugsee dashboard)
///
///* exception: the exception instance that will be reported
///* stackTrace: the strack trace of the exception, by default it's null
///* traces: the traces that led to the exception.
///* events: the events where the exception has been caught.
Future<void> logException({
required Exception exception,
StackTrace? stackTrace,
Map<String, dynamic> traces,
});
/// Manually update the current BugseeEnabled flag in shared prefs and in current manager singleton.
Future<void> setIsBugseeEnabled(bool value);
/// Manually update isDataObscured flag in shared prefs and in current state [bugseeConfigState].
Future<void> setIsDataObscured(bool value);
/// Manually update the current enableVideoCapture flag in shared prefs and in current manager singleton.
Future<void> setIsVideoCaptureEnabled(bool value);
/// Manually shows the built-in capture log report screen of Bugsee.
Future<void> showCaptureLogReport();
/// Manually update whether logs will be collected or not flag in shared prefs.
Future<void> setIsLogsCollectionEnabled(bool value);
/// Manually update isLogFilterEnabled flag in shared prefs.
Future<void> setIsLogFilterEnabeld(bool value);
/// Manually update whether Bugsee attach the log file with the reported
/// exception or not.
///
/// By default the log file is attached
Future<void> setAttachLogFileEnabled(bool value);
/// Intecept all unhandled exception thrown by the dart framework
Future<void> inteceptExceptions(Object error, StackTrace stackTrace);
/// Intercept all unhandled rending exception thrown by the Flutter framework
Future<void> inteceptRenderingExceptions(FlutterErrorDetails error);
/// Manually add a map of attributes
/// - the map entry key is the attribute name
/// - the map entry value is the attribute value (string, int, boolean)
///
/// Attributes will be attached to all reported exception unless app is uninstalled
/// or attributes are removed manually
Future<void> addAttributes(Map<String, dynamic> attributes);
/// Manually add an email attached to all reported.
///
/// The email will be attached to all reported exception unless app is uninstalled
/// or the email is removed manually.
Future<void> addEmailAttribute(String email);
/// Manually remove the email attribute attached using [addEmailAttribute]
Future<void> clearEmailAttribute();
/// Manually remove an attribute by the given key attached using [addAttributes]
Future<void> clearAttribute(String attribute);
/// Manually log Bugsee events that will be attached to the reported issues
void logEvents(Map<String, Map<String, dynamic>> events);
}
final class _BugseeManager implements BugseeManager {
late Logger logger;
late LoggerManager loggerManager;
late BugseeRepository bugseeRepository;
_BugseeManager({
required this.bugseeRepository,
required this.logger,
required this.loggerManager,
});
BugseeConfigState _currentState = const BugseeConfigState();
@override
BugseeConfigState get bugseeConfigState => _currentState;
late bool _isBugSeeInitialized;
late BugseeConfigurationData configurationData;
BugseeLaunchOptions? launchOptions;
@override
Future<void> initialize({
String? bugseeToken,
}) async {
if (!Platform.isIOS && !Platform.isAndroid) {
_currentState = _currentState.copyWith(
isConfigurationValid: false,
configErrorEnum: ConfigErrorEnum.invalidPlatform,
);
logger.i("BUGSEE: ${_currentState.configErrorEnum?.error}");
return;
}
configurationData = await bugseeRepository.getBugseeConfiguration();
configurationData = configurationData.copyWith(
isLogCollectionEnabled: configurationData.isLogCollectionEnabled ??
bool.parse(
dotenv.env['BUGSEE_DISABLE_LOG_COLLECTION'] ?? 'true',
),
isLogsFilterEnabled: configurationData.isLogsFilterEnabled ??
bool.parse(
dotenv.env['BUGSEE_FILTER_LOG_COLLECTION'] ?? 'true',
),
isDataObscured: configurationData.isDataObscured ??
bool.parse(dotenv.env['BUGSEE_IS_DATA_OBSCURE'] ?? 'true'),
attachLogFileEnabled: configurationData.attachLogFileEnabled ??
bool.parse(dotenv.env['BUGSEE_ATTACH_LOG_FILE'] ?? 'true'),
);
launchOptions = _initializeLaunchOptions();
_isBugSeeInitialized = false;
if (kDebugMode) {
_currentState = _currentState.copyWith(
isConfigurationValid: false,
configErrorEnum: ConfigErrorEnum.invalidReleaseMode,
);
logger.i("BUGSEE: ${_currentState.configErrorEnum?.error}");
return;
}
if (bugseeToken == null ||
!RegExp(bugseeTokenFormat).hasMatch(bugseeToken)) {
_currentState = _currentState.copyWith(
isConfigurationValid: false,
configErrorEnum: ConfigErrorEnum.invalidToken,
);
logger.i(
"BUGSEE: ${_currentState.configErrorEnum?.error}",
);
return;
}
_initializeBugsee(bugseeToken);
}
void _initializeBugsee(String bugseeToken) async {
if (configurationData.isBugseeEnabled ?? true) {
_isBugSeeInitialized = await _launchBugseeLogger(bugseeToken);
}
_currentState = _currentState.copyWith(
isConfigurationValid: _isBugSeeInitialized,
isBugseeEnabled: _isBugSeeInitialized,
isVideoCaptureEnabled: _isBugSeeInitialized &&
(configurationData.isVideoCaptureEnabled ?? true),
isDataObscured: configurationData.isDataObscured,
isLogFilterEnabled: configurationData.isLogsFilterEnabled,
isLogCollectionEnabled: configurationData.isLogCollectionEnabled,
attachLogFile: configurationData.attachLogFileEnabled,
);
}
Future<bool> _launchBugseeLogger(String bugseeToken) async {
bool isInitialized = false;
HttpOverrides.global = Bugsee.defaultHttpOverrides;
await Bugsee.launch(
bugseeToken,
appRunCallback: (isBugseeLaunched) {
if (!isBugseeLaunched) {
logger.e(
"BUGSEE: not initialized, verify bugsee token configuration",
);
}
isInitialized = isBugseeLaunched;
},
launchOptions: launchOptions,
);
if (configurationData.isLogsFilterEnabled ?? false) {
Bugsee.setLogFilter(_filterBugseeLogs);
}
if (configurationData.attachLogFileEnabled ?? false) {
Bugsee.setAttachmentsCallback(_attachLogFile);
}
return isInitialized;
}
BugseeLaunchOptions? _initializeLaunchOptions() {
if (Platform.isAndroid) {
return AndroidLaunchOptions()
..captureLogs = (configurationData.isLogCollectionEnabled ?? true);
} else if (Platform.isIOS) {
return IOSLaunchOptions()
..captureLogs = (configurationData.isLogCollectionEnabled ?? true);
}
return null;
}
Future<BugseeLogEvent> _filterBugseeLogs(BugseeLogEvent logEvent) async {
logEvent.text = logEvent.text.replaceAll(RegExp(bugseeFilterRegex), '');
return logEvent;
}
Future<List<BugseeAttachment>> _attachLogFile(BugseeReport report) async {
var attachments = <BugseeAttachment>[];
if (loggerManager.logFile != null) {
attachments.add(
BugseeAttachment(
"logFile",
loggerManager.logFile!.path,
loggerManager.logFile!.readAsBytesSync(),
),
);
}
return attachments;
}
@override
Future<void> logException({
required Exception exception,
StackTrace? stackTrace,
Map<String, dynamic> traces = const {},
}) async {
if (_currentState.isBugseeEnabled) {
for (var trace in traces.entries) {
await Bugsee.trace(trace.key, trace.value);
}
await Bugsee.logException(exception, stackTrace);
}
}
@override
Future<void> setIsBugseeEnabled(bool value) async {
if (_currentState.isConfigurationValid) {
await bugseeRepository.setIsBugseeEnabled(value);
_currentState = _currentState.copyWith(
isBugseeEnabled: value,
isRestartRequired: value,
isVideoCaptureEnabled: value,
isDataObscured: value,
);
if (!_currentState.isRestartRequired) {
await Bugsee.stop();
}
}
}
@override
Future<void> setIsVideoCaptureEnabled(bool value) async {
if (_currentState.isBugseeEnabled) {
_currentState = _currentState.copyWith(
isVideoCaptureEnabled: value,
);
await bugseeRepository.setIsVideoCaptureEnabled(
_currentState.isVideoCaptureEnabled,
);
if (!_currentState.isVideoCaptureEnabled) {
await Bugsee.pause();
} else {
await Bugsee.resume();
}
}
}
@override
Future<void> showCaptureLogReport() async {
if (_currentState.isBugseeEnabled) {
await Bugsee.showReportDialog();
}
}
@override
Future<void> setIsDataObscured(bool value) async {
if (_currentState.isBugseeEnabled) {
await bugseeRepository.setIsDataObscure(value);
_currentState = _currentState.copyWith(
isRestartRequired: value != configurationData.isDataObscured,
isDataObscured: value,
);
}
}
@override
Future<void> setIsLogsCollectionEnabled(bool value) async {
if (_currentState.isBugseeEnabled) {
await bugseeRepository.setIsLogCollectionEnabled(value);
_currentState = _currentState.copyWith(
isRestartRequired: value != configurationData.isLogCollectionEnabled,
isLogCollectionEnabled: value,
);
if (!value) {
_currentState = _currentState.copyWith(
isLogFilterEnabled: false,
);
}
}
}
@override
Future<void> setIsLogFilterEnabeld(bool value) async {
if (_currentState.isBugseeEnabled && _currentState.isLogCollectionEnabled) {
await bugseeRepository.setIsLogFilterEnabled(value);
_currentState = _currentState.copyWith(
isRestartRequired: value != configurationData.isLogsFilterEnabled,
isLogFilterEnabled: value,
);
}
}
@override
Future<void> setAttachLogFileEnabled(bool value) async {
if (_currentState.isBugseeEnabled) {
await bugseeRepository.setAttachLogFileEnabled(value);
_currentState = _currentState.copyWith(
isRestartRequired: value != configurationData.attachLogFileEnabled,
attachLogFile: value,
);
}
}
@override
Future<void> inteceptExceptions(
Object error,
StackTrace stackTrace,
) async {
String? message = switch (error.runtimeType) {
const (PersistenceException) => (error as PersistenceException).message,
_ => null,
};
await logException(
exception: Exception(error),
stackTrace: stackTrace,
traces: {
'message': message,
},
);
}
@override
Future<void> addAttributes(Map<String, dynamic> attributes) async {
for (var attribute in attributes.entries) {
await Bugsee.setAttribute(attribute.key, attribute.value);
}
}
@override
Future<void> clearAttribute(String attribute) async {
await Bugsee.clearAttribute(attribute);
}
@override
Future<void> addEmailAttribute(String email) async {
await Bugsee.setEmail(email);
}
@override
Future<void> clearEmailAttribute() async {
await Bugsee.clearEmail();
}
@override
Future<void> inteceptRenderingExceptions(FlutterErrorDetails error) async {
await logException(
exception: Exception(error.exception),
stackTrace: error.stack,
);
}
@override
void logEvents(Map<String, Map<String, dynamic>> events) async {
for (var event in events.entries) {
Bugsee.event(event.key, event.value);
}
}
}