Last active
August 2, 2020 02:06
-
-
Save guilherme-v/c0ec437b4281c6de10296c2e62a55b80 to your computer and use it in GitHub Desktop.
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
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:ansicolor/ansicolor.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
runZoned( | |
() { | |
runApp(MyApp()); | |
}, | |
zoneSpecification: ZoneSpecification( | |
// Intercept all print calls | |
print: (self, parent, zone, line) async { | |
// Paint all logs with White color | |
final pen = AnsiPen()..white(bold: true); | |
// Include a timestamp and the name of the App - 'MyApp' in this case | |
final messageToLog = "[${DateTime.now()}] MyApp: $line"; | |
// Send the message to our local server that is running locally | |
logLocally(messageToLog); | |
// Also print the message in the "Debug Console" | |
parent.print(zone, pen(messageToLog)); | |
}, | |
), | |
); | |
} | |
Future<File> writeLine(String line) async { | |
final file = File('./customLogs.tmp'); | |
return file.writeAsString('$line'); | |
} | |
Future<void> logLocally(String line) async { | |
// When using Android Emulator the address '127.0.0.1' is a loopback interface | |
// to target a server running in the local machine use '10.0.0.2' instead | |
var url = 'http://10.0.2.2:8082/'; | |
http.post(url, body: jsonEncode(line)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment