-
-
Notifications
You must be signed in to change notification settings - Fork 278
Feat: debugPrint integration
#618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marandaneto
merged 9 commits into
getsentry:main
from
ueman:feat/flutter-debug-print-integration
Oct 18, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
81fe959
debug print integration
ueman 8f5e66f
Update CHANGELOG.md
ueman 29c7195
refactoring + close test
ueman 1c178cd
disable in debug, enable when enablePrintBreadcrumbs
ueman b3ca159
add as default integration
ueman f05d269
breadcrumb test
ueman f2ecbff
use console breadcrumb in print integration
ueman a78cc66
use debug level
ueman 7c051fe
feedback
ueman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
|
|
||
| import '../sentry_flutter_options.dart'; | ||
|
|
||
| /// Integration which intercepts Flutters [debugPrint] method. | ||
| /// If this integration is added, all calls to [debugPrint] a redirected to | ||
| /// add a [Breadcrumb]. [debugPrint] is not outputting to the console anymore! | ||
| /// This integration fixes the issue described in | ||
| /// [#492](https://github.com/getsentry/sentry-dart/issues/492). | ||
| class DebugPrintIntegration extends Integration<SentryFlutterOptions> { | ||
| DebugPrintCallback? _debugPrintBackup; | ||
| late Hub _hub; | ||
| late SentryFlutterOptions _options; | ||
|
|
||
| @override | ||
| FutureOr<void> call(Hub hub, SentryFlutterOptions options) { | ||
| _hub = hub; | ||
| _options = options; | ||
|
|
||
| final isDebug = options.platformChecker.isDebugMode(); | ||
| final enablePrintBreadcrumbs = options.enablePrintBreadcrumbs; | ||
| if (isDebug || !enablePrintBreadcrumbs) { | ||
| return Future.value(); | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| _debugPrintBackup = debugPrint; | ||
|
|
||
| // We're simply replacing debugPrint here. The default implementation is a | ||
| // a throttling system which prints using Darts print method. It's basically | ||
| // a fire and forget method which completes sometime in the future. We can't | ||
| // observe when it's done. | ||
| // | ||
| // This makes it impossible to just disable adding print() breadcrumbs | ||
| // before debugPrint is called and re-enable it after debugPrint was called. | ||
| // See the docs for more information. | ||
| // https://api.flutter.dev/flutter/foundation/debugPrint.html | ||
| debugPrint = _debugPrint; | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| options.sdk.addIntegration('debugPrintIntegration'); | ||
| } | ||
|
|
||
| @override | ||
| void close() { | ||
| if (_debugPrintBackup != null) { | ||
| debugPrint = _debugPrintBackup!; | ||
| } | ||
| } | ||
|
|
||
| void _debugPrint(String? message, {int? wrapWidth}) { | ||
| if (message == null) { | ||
| _options.logger( | ||
| SentryLevel.debug, | ||
| 'debugPrint Integration received "null" as message. ' | ||
| 'The message is dropped.', | ||
| ); | ||
| return; | ||
| } | ||
| _hub.addBreadcrumb(Breadcrumb.console( | ||
| message: message, | ||
| level: SentryLevel.debug, | ||
| )); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
flutter/test/integrations/debug_print_integration_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:sentry_flutter/sentry_flutter.dart'; | ||
| import 'package:sentry_flutter/src/integrations/debug_print_integration.dart'; | ||
|
|
||
| import '../mocks.dart'; | ||
| import '../mocks.mocks.dart'; | ||
|
|
||
| void main() { | ||
| late Fixture fixture; | ||
|
|
||
| setUp(() { | ||
| fixture = Fixture(); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| debugPrint = debugPrintSynchronously; | ||
| }); | ||
|
|
||
| test('$DebugPrintIntegration: debugPrint adds a breadcrumb', () { | ||
| final integration = fixture.getSut(); | ||
| integration.call(fixture.hub, fixture.getOptions()); | ||
|
|
||
| debugPrint('Foo Bar'); | ||
|
|
||
| final breadcrumb = verify( | ||
| fixture.hub.addBreadcrumb(captureAny), | ||
| ).captured.first as Breadcrumb; | ||
|
|
||
| expect(breadcrumb.message, 'Foo Bar'); | ||
| }); | ||
|
|
||
| test( | ||
| '$DebugPrintIntegration: debugPrint does not add a breadcrumb after close', | ||
| () { | ||
| final integration = fixture.getSut(); | ||
| integration.call(fixture.hub, fixture.getOptions()); | ||
| integration.close(); | ||
|
|
||
| debugPrint('Foo Bar'); | ||
|
|
||
| verifyNever(fixture.hub.addBreadcrumb(captureAny)); | ||
| }); | ||
|
|
||
| test( | ||
| '$DebugPrintIntegration: close changes debugPrint back to default implementation', | ||
| () { | ||
| final original = debugPrint; | ||
|
|
||
| final integration = fixture.getSut(); | ||
| integration.call(fixture.hub, fixture.getOptions()); | ||
| integration.close(); | ||
|
|
||
| expect(debugPrint, original); | ||
| }); | ||
|
|
||
| test('$DebugPrintIntegration: disabled in debug builds', () { | ||
| final integration = fixture.getSut(); | ||
| integration.call(fixture.hub, fixture.getOptions(debug: true)); | ||
|
|
||
| debugPrint('Foo Bar'); | ||
|
|
||
| verifyNever(fixture.hub.addBreadcrumb(captureAny)); | ||
| }); | ||
|
|
||
| test('$DebugPrintIntegration: disabled if enablePrintBreadcrumbs = false', | ||
| () { | ||
| final integration = fixture.getSut(); | ||
| integration.call( | ||
| fixture.hub, | ||
| fixture.getOptions(enablePrintBreadcrumbs: false), | ||
| ); | ||
|
|
||
| debugPrint('Foo Bar'); | ||
|
|
||
| verifyNever(fixture.hub.addBreadcrumb(captureAny)); | ||
| }); | ||
|
|
||
| test( | ||
| '$DebugPrintIntegration: close works if debugPrintIntegration.call was not called', | ||
| () { | ||
| final integration = fixture.getSut(); | ||
|
|
||
| // test is successful if no exception is thrown | ||
| expect(() => integration.close(), returnsNormally); | ||
| }); | ||
| } | ||
|
|
||
| class Fixture { | ||
| final hub = MockHub(); | ||
|
|
||
| SentryFlutterOptions getOptions({ | ||
| bool debug = false, | ||
| bool enablePrintBreadcrumbs = true, | ||
| }) { | ||
| return SentryFlutterOptions( | ||
| dsn: fakeDsn, | ||
| checker: MockPlatformChecker(isDebug: debug), | ||
| )..enablePrintBreadcrumbs = enablePrintBreadcrumbs; | ||
| } | ||
|
|
||
| DebugPrintIntegration getSut() { | ||
| return DebugPrintIntegration(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.