Skip to content
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

Add support to logger 0.9.1 or greater #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.9.0
- Add support to logger 0.9.1 or greater

## 0.7.1
- Fixed error during build

Expand Down
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
Flutter extension for [logger](https://github.com/leisim/logger).<br>
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.<br>
The default implementation (`ConsoleOutput`) send every line to the system console.<br>
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.
```
40 changes: 33 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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");

Expand All @@ -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: <String, WidgetBuilder>{
"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")),
],
),
);
}
Expand Down
56 changes: 21 additions & 35 deletions lib/src/log_console.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
part of logger_flutter;

ListQueue<OutputEvent> _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<void> 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
Expand All @@ -38,8 +44,6 @@ class RenderedEvent {
}

class _LogConsoleState extends State<LogConsole> {
OutputCallback _callback;

ListQueue<RenderedEvent> _renderedBuffer = ListQueue();
List<RenderedEvent> _filteredBuffer = [];

Expand All @@ -57,21 +61,9 @@ class _LogConsoleState extends State<LogConsole> {
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;
});
Expand Down Expand Up @@ -307,12 +299,6 @@ class _LogConsoleState extends State<LogConsole> {
text.toLowerCase(),
);
}

@override
void dispose() {
Logger.removeOutputListener(_callback);
super.dispose();
}
}

class LogBar extends StatelessWidget {
Expand Down
15 changes: 1 addition & 14 deletions lib/src/log_console_on_shake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class _LogConsoleOnShakeState extends State<LogConsoleOnShake> {
}

_init() {
LogConsole.init();
_detector = ShakeDetector(onPhoneShake: _openLogConsole);
_detector.startListening();
}
Expand All @@ -48,19 +47,7 @@ class _LogConsoleOnShakeState extends State<LogConsoleOnShake> {
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;
}

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/leisim/logger_flutter

Expand All @@ -11,5 +11,5 @@ dependencies:
flutter:
sdk: flutter

logger: ">=0.7.0"
sensors: ^0.4.0+1
logger: ">=0.7.0 <1.0.0"
sensors: ^0.4.2+4