From 3872e8d9261ec70663725aca24591ff9636a3861 Mon Sep 17 00:00:00 2001 From: Ewerton Ramos Date: Thu, 3 Sep 2020 18:13:57 -0300 Subject: [PATCH] Add support to logger 0.9.1 or greater --- CHANGELOG.md | 3 ++ README.md | 79 ++++++++++++++++++++++++++++++- example/lib/main.dart | 40 +++++++++++++--- lib/src/log_console.dart | 56 ++++++++-------------- lib/src/log_console_on_shake.dart | 15 +----- pubspec.yaml | 6 +-- 6 files changed, 138 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 100e731..d9ab084 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.9.0 +- Add support to logger 0.9.1 or greater + ## 0.7.1 - Fixed error during build diff --git a/README.md b/README.md index 3efeefc..8a127f5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,77 @@ -Flutter extension for [logger](https://github.com/leisim/logger).
-Please go to there for documentation. +# Flutter extension for Logger + +**Logger** package is [here](https://pub.dev/packages/logger). + +**Show some ❤️ and star the repo to support the project** + +### Resources: +- [Pub Package](https://pub.dev/packages/logger_flutter) +- [GitHub Repository](https://github.com/leisim/logger_flutter) + +## Getting Started + +### Create a `LogOutput` to send output events to `LogConsole` + +`LogOutput` sends the log lines to the desired destination.
+The default implementation (`ConsoleOutput`) send every line to the system console.
+You can extend from `LogOuput` or another implementation of that, and call `LogConsole.add(event)`: + +```dart +var logger = Logger( + output: ExampleLogOutput(), +); + +class ExampleLogOutput extends ConsoleOutput { + @override + void output(OutputEvent event) { + super.output(event); + LogConsole.add(event); + } +} +``` + +### Show `LogConsole` + +You have to options: + +- Just shake the phone to show the console + +```dart +LogConsoleOnShake( + child: Container() // Your widgets +), +``` + +- Show the console from anywhere + +```dart +LogConsole.open(context); +``` + +## Output + +| ![](https://raw.githubusercontent.com/leisim/logger/master/art/log_console_light.png) | ![](https://raw.githubusercontent.com/leisim/logger/master/art/log_console_dark.png) | +|---|---| + +## MIT License +``` +Copyright (c) 2019 Simon Leier + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` diff --git a/example/lib/main.dart b/example/lib/main.dart index 14c48d6..0eba532 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,6 @@ -import 'package:flutter/material.dart'; import 'dart:async'; +import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:logger_flutter/logger_flutter.dart'; @@ -11,12 +11,22 @@ void main() { var logger = Logger( printer: PrettyPrinter(), + output: ExampleLogOutput(), ); var loggerNoStack = Logger( printer: PrettyPrinter(methodCount: 0), + output: ExampleLogOutput(), ); +class ExampleLogOutput extends ConsoleOutput { + @override + void output(OutputEvent event) { + super.output(event); + LogConsole.add(event); + } +} + void log() { logger.d("Log message with 2 methods"); @@ -36,13 +46,29 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, - home: Scaffold( - body: LogConsoleOnShake( - dark: true, - child: Center( - child: Text("Shake Phone to open Console."), + routes: { + "home": (context) => HomeWidget(), + }, + initialRoute: "home", + ); + } +} + +class HomeWidget extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + LogConsoleOnShake( + dark: true, + child: Center( + child: Text("Shake Phone to open Console."), + ), ), - ), + FlatButton(onPressed: () => LogConsole.open(context), child: Text("or click here to open Console")), + ], ), ); } diff --git a/lib/src/log_console.dart b/lib/src/log_console.dart index fdf5b77..e7042bd 100644 --- a/lib/src/log_console.dart +++ b/lib/src/log_console.dart @@ -1,27 +1,33 @@ part of logger_flutter; ListQueue _outputEventBuffer = ListQueue(); -int _bufferSize = 20; -bool _initialized = false; class LogConsole extends StatefulWidget { final bool dark; final bool showCloseButton; - LogConsole({this.dark = false, this.showCloseButton = false}) - : assert(_initialized, "Please call LogConsole.init() first."); + LogConsole({this.dark = false, this.showCloseButton = false}); - static void init({int bufferSize = 20}) { - if (_initialized) return; + static Future open(BuildContext context, {bool dark}) async { + var logConsole = LogConsole( + showCloseButton: true, + dark: dark ?? Theme.of(context).brightness == Brightness.dark, + ); + PageRoute route; + if (Platform.isIOS) { + route = CupertinoPageRoute(builder: (_) => logConsole); + } else { + route = MaterialPageRoute(builder: (_) => logConsole); + } - _bufferSize = bufferSize; - _initialized = true; - Logger.addOutputListener((e) { - if (_outputEventBuffer.length == bufferSize) { - _outputEventBuffer.removeFirst(); - } - _outputEventBuffer.add(e); - }); + await Navigator.push(context, route); + } + + static void add(OutputEvent outputEvent, {int bufferSize = 20}) { + while (_outputEventBuffer.length >= (bufferSize ?? 1)) { + _outputEventBuffer.removeFirst(); + } + _outputEventBuffer.add(outputEvent); } @override @@ -38,8 +44,6 @@ class RenderedEvent { } class _LogConsoleState extends State { - OutputCallback _callback; - ListQueue _renderedBuffer = ListQueue(); List _filteredBuffer = []; @@ -57,21 +61,9 @@ class _LogConsoleState extends State { void initState() { super.initState(); - _callback = (e) { - if (_renderedBuffer.length == _bufferSize) { - _renderedBuffer.removeFirst(); - } - - _renderedBuffer.add(_renderEvent(e)); - _refreshFilter(); - }; - - Logger.addOutputListener(_callback); - _scrollController.addListener(() { if (!_scrollListenerEnabled) return; - var scrolledToBottom = _scrollController.offset >= - _scrollController.position.maxScrollExtent; + var scrolledToBottom = _scrollController.offset >= _scrollController.position.maxScrollExtent; setState(() { _followBottom = scrolledToBottom; }); @@ -307,12 +299,6 @@ class _LogConsoleState extends State { text.toLowerCase(), ); } - - @override - void dispose() { - Logger.removeOutputListener(_callback); - super.dispose(); - } } class LogBar extends StatelessWidget { diff --git a/lib/src/log_console_on_shake.dart b/lib/src/log_console_on_shake.dart index 29477c0..3469244 100644 --- a/lib/src/log_console_on_shake.dart +++ b/lib/src/log_console_on_shake.dart @@ -39,7 +39,6 @@ class _LogConsoleOnShakeState extends State { } _init() { - LogConsole.init(); _detector = ShakeDetector(onPhoneShake: _openLogConsole); _detector.startListening(); } @@ -48,19 +47,7 @@ class _LogConsoleOnShakeState extends State { if (_open) return; _open = true; - - var logConsole = LogConsole( - showCloseButton: true, - dark: widget.dark ?? Theme.of(context).brightness == Brightness.dark, - ); - PageRoute route; - if (Platform.isIOS) { - route = CupertinoPageRoute(builder: (_) => logConsole); - } else { - route = MaterialPageRoute(builder: (_) => logConsole); - } - - await Navigator.push(context, route); + await LogConsole.open(context, dark: widget.dark); _open = false; } diff --git a/pubspec.yaml b/pubspec.yaml index f0026e5..e951e0b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: logger_flutter description: Flutter extension for logger. Please go there for documentation. -version: 0.7.1 +version: 0.9.0 author: Simon Leier homepage: https://github.com/leisim/logger_flutter @@ -11,5 +11,5 @@ dependencies: flutter: sdk: flutter - logger: ">=0.7.0" - sensors: ^0.4.0+1 \ No newline at end of file + logger: ">=0.7.0 <1.0.0" + sensors: ^0.4.2+4 \ No newline at end of file