Jonny Borges
Committed by GitHub

Merge pull request #481 from stefandevo/master

Abstraction of logging
@@ -10,6 +10,8 @@ void main() { @@ -10,6 +10,8 @@ void main() {
10 GetMaterialApp( 10 GetMaterialApp(
11 debugShowCheckedModeBanner: false, 11 debugShowCheckedModeBanner: false,
12 initialRoute: '/', 12 initialRoute: '/',
  13 + enableLog: true,
  14 + // logWriterCallback: localLogWriter,
13 getPages: [ 15 getPages: [
14 GetPage(name: '/', page: () => HomePage(), binding: HomeBinding()), 16 GetPage(name: '/', page: () => HomePage(), binding: HomeBinding()),
15 GetPage(name: '/country', page: () => CountryPage()), 17 GetPage(name: '/country', page: () => CountryPage()),
@@ -18,3 +20,8 @@ void main() { @@ -18,3 +20,8 @@ void main() {
18 ), 20 ),
19 ); 21 );
20 } 22 }
  23 +
  24 +// Sample of abstract logging function
  25 +void localLogWriter(String text, {bool isError = false}) {
  26 + print('** ' + text + ' [' + isError.toString() + ']');
  27 +}
@@ -3,7 +3,7 @@ description: A new Flutter project. @@ -3,7 +3,7 @@ description: A new Flutter project.
3 3
4 # The following line prevents the package from being accidentally published to 4 # The following line prevents the package from being accidentally published to
5 # pub.dev using `pub publish`. This is preferred for private packages. 5 # pub.dev using `pub publish`. This is preferred for private packages.
6 -publish_to: 'none' # Remove this line if you wish to publish to pub.dev 6 +publish_to: "none" # Remove this line if you wish to publish to pub.dev
7 7
8 # The following defines the version and build number for your application. 8 # The following defines the version and build number for your application.
9 # A version number is three numbers separated by dots, like 1.2.43 9 # A version number is three numbers separated by dots, like 1.2.43
@@ -24,10 +24,10 @@ dependencies: @@ -24,10 +24,10 @@ dependencies:
24 flutter: 24 flutter:
25 sdk: flutter 25 sdk: flutter
26 26
27 -  
28 # The following adds the Cupertino Icons font to your application. 27 # The following adds the Cupertino Icons font to your application.
29 # Use with the CupertinoIcons class for iOS style icons. 28 # Use with the CupertinoIcons class for iOS style icons.
30 - get: ^3.2.2 29 + get:
  30 + path: ../
31 dio: ^3.0.9 31 dio: ^3.0.9
32 32
33 dev_dependencies: 33 dev_dependencies:
@@ -39,23 +39,18 @@ dev_dependencies: @@ -39,23 +39,18 @@ dev_dependencies:
39 39
40 # The following section is specific to Flutter. 40 # The following section is specific to Flutter.
41 flutter: 41 flutter:
42 -  
43 # The following line ensures that the Material Icons font is 42 # The following line ensures that the Material Icons font is
44 # included with your application, so that you can use the icons in 43 # included with your application, so that you can use the icons in
45 # the material Icons class. 44 # the material Icons class.
46 uses-material-design: true 45 uses-material-design: true
47 -  
48 # To add assets to your application, add an assets section, like this: 46 # To add assets to your application, add an assets section, like this:
49 # assets: 47 # assets:
50 # - images/a_dot_burr.jpeg 48 # - images/a_dot_burr.jpeg
51 # - images/a_dot_ham.jpeg 49 # - images/a_dot_ham.jpeg
52 -  
53 # An image asset can refer to one or more resolution-specific "variants", see 50 # An image asset can refer to one or more resolution-specific "variants", see
54 # https://flutter.dev/assets-and-images/#resolution-aware. 51 # https://flutter.dev/assets-and-images/#resolution-aware.
55 -  
56 # For details regarding adding assets from package dependencies, see 52 # For details regarding adding assets from package dependencies, see
57 # https://flutter.dev/assets-and-images/#from-packages 53 # https://flutter.dev/assets-and-images/#from-packages
58 -  
59 # To add custom fonts to your application, add a fonts section here, 54 # To add custom fonts to your application, add a fonts section here,
60 # in this "flutter" section. Each entry in this list should have a 55 # in this "flutter" section. Each entry in this list should have a
61 # "family" key with the font family name, and a "fonts" key with a 56 # "family" key with the font family name, and a "fonts" key with a
  1 +import 'package:get/instance_manager.dart';
  2 +
  3 +typedef LogWriterCallback = void Function(String text, {bool isError});
  4 +
  5 +void defaultLogWriterCallback(String value, {bool isError = false}) {
  6 + if (isError || GetConfig.isLogEnable) print(value);
  7 +}
  1 +import 'package:get/src/core/log.dart';
1 import 'package:get/src/navigation/root/smart_management.dart'; 2 import 'package:get/src/navigation/root/smart_management.dart';
2 import 'package:get/src/state_manager/rx/rx_interface.dart'; 3 import 'package:get/src/state_manager/rx/rx_interface.dart';
3 4
4 class GetConfig { 5 class GetConfig {
5 static SmartManagement smartManagement = SmartManagement.full; 6 static SmartManagement smartManagement = SmartManagement.full;
6 static bool isLogEnable = true; 7 static bool isLogEnable = true;
  8 + static LogWriterCallback log = defaultLogWriterCallback;
7 static String currentRoute; 9 static String currentRoute;
8 } 10 }
9 11
@@ -124,7 +126,7 @@ class GetInstance { @@ -124,7 +126,7 @@ class GetInstance {
124 126
125 if (i is DisposableInterface) { 127 if (i is DisposableInterface) {
126 i.onStart(); 128 i.onStart();
127 - if (GetConfig.isLogEnable) print('[GETX] $key has been initialized'); 129 + GetConfig.log('[GETX] $key has been initialized');
128 } 130 }
129 } 131 }
130 132
@@ -169,8 +171,7 @@ class GetInstance { @@ -169,8 +171,7 @@ class GetInstance {
169 if (!_factory.containsKey(key)) 171 if (!_factory.containsKey(key))
170 throw " $S not found. You need call put<$S>($S()) before"; 172 throw " $S not found. You need call put<$S>($S()) before";
171 173
172 - if (GetConfig.isLogEnable)  
173 - print('[GETX] $S instance was created at that time'); 174 + GetConfig.log('[GETX] $S instance was created at that time');
174 S _value = put<S>(_factory[key].builder() as S); 175 S _value = put<S>(_factory[key].builder() as S);
175 176
176 initDependencies<S>(name: tag); 177 initDependencies<S>(name: tag);
@@ -205,14 +206,15 @@ class GetInstance { @@ -205,14 +206,15 @@ class GetInstance {
205 } 206 }
206 207
207 if (!_singl.containsKey(newKey)) { 208 if (!_singl.containsKey(newKey)) {
208 - print('Instance $newKey not found'); 209 + GetConfig.log('Instance $newKey not found', isError: true);
209 return false; 210 return false;
210 } 211 }
211 212
212 FcBuilder builder = _singl[newKey] as FcBuilder; 213 FcBuilder builder = _singl[newKey] as FcBuilder;
213 if (builder.permanent && !force) { 214 if (builder.permanent && !force) {
214 - print(  
215 - '[GETX] [$newKey] has been marked as permanent, SmartManagement is not authorized to delete it.'); 215 + GetConfig.log(
  216 + '[GETX] [$newKey] has been marked as permanent, SmartManagement is not authorized to delete it.',
  217 + isError: true);
216 return false; 218 return false;
217 } 219 }
218 final i = builder.dependency; 220 final i = builder.dependency;
@@ -222,14 +224,14 @@ class GetInstance { @@ -222,14 +224,14 @@ class GetInstance {
222 } 224 }
223 if (i is DisposableInterface) { 225 if (i is DisposableInterface) {
224 await i.onClose(); 226 await i.onClose();
225 - if (GetConfig.isLogEnable) print('[GETX] onClose of $newKey called'); 227 + GetConfig.log('[GETX] onClose of $newKey called');
226 } 228 }
227 229
228 _singl.removeWhere((oldKey, value) => (oldKey == newKey)); 230 _singl.removeWhere((oldKey, value) => (oldKey == newKey));
229 if (_singl.containsKey(newKey)) { 231 if (_singl.containsKey(newKey)) {
230 - print('[GETX] error on remove object $newKey'); 232 + GetConfig.log('[GETX] error on remove object $newKey', isError: true);
231 } else { 233 } else {
232 - if (GetConfig.isLogEnable) print('[GETX] $newKey deleted from memory'); 234 + GetConfig.log('[GETX] $newKey deleted from memory');
233 } 235 }
234 // _routesKey?.remove(key); 236 // _routesKey?.remove(key);
235 return true; 237 return true;
@@ -3,6 +3,7 @@ import 'package:flutter/scheduler.dart'; @@ -3,6 +3,7 @@ import 'package:flutter/scheduler.dart';
3 import 'package:get/src/core/get_interface.dart'; 3 import 'package:get/src/core/get_interface.dart';
4 import 'package:get/instance_manager.dart'; 4 import 'package:get/instance_manager.dart';
5 import 'package:get/route_manager.dart'; 5 import 'package:get/route_manager.dart';
  6 +import 'package:get/src/core/log.dart';
6 import 'root/parse_route.dart'; 7 import 'root/parse_route.dart';
7 import 'routes/bindings_interface.dart'; 8 import 'routes/bindings_interface.dart';
8 9
@@ -763,6 +764,7 @@ extension GetNavigation on GetInterface { @@ -763,6 +764,7 @@ extension GetNavigation on GetInterface {
763 /// change default config of Get 764 /// change default config of Get
764 void config( 765 void config(
765 {bool enableLog, 766 {bool enableLog,
  767 + LogWriterCallback logWriterCallback,
766 bool defaultPopGesture, 768 bool defaultPopGesture,
767 bool defaultOpaqueRoute, 769 bool defaultOpaqueRoute,
768 Duration defaultDurationTransition, 770 Duration defaultDurationTransition,
@@ -771,6 +773,9 @@ extension GetNavigation on GetInterface { @@ -771,6 +773,9 @@ extension GetNavigation on GetInterface {
771 if (enableLog != null) { 773 if (enableLog != null) {
772 GetConfig.isLogEnable = enableLog; 774 GetConfig.isLogEnable = enableLog;
773 } 775 }
  776 + if (logWriterCallback != null) {
  777 + GetConfig.log = logWriterCallback;
  778 + }
774 if (defaultPopGesture != null) { 779 if (defaultPopGesture != null) {
775 this.defaultPopGesture = defaultPopGesture; 780 this.defaultPopGesture = defaultPopGesture;
776 } 781 }
1 import 'package:flutter/foundation.dart'; 1 import 'package:flutter/foundation.dart';
2 import 'package:flutter/material.dart'; 2 import 'package:flutter/material.dart';
3 import 'package:get/get.dart'; 3 import 'package:get/get.dart';
  4 +import 'package:get/src/core/log.dart';
4 import 'package:get/src/instance/get_instance.dart'; 5 import 'package:get/src/instance/get_instance.dart';
5 import 'package:get/src/navigation/routes/get_route.dart'; 6 import 'package:get/src/navigation/routes/get_route.dart';
6 import 'root_controller.dart'; 7 import 'root_controller.dart';
@@ -51,6 +52,7 @@ class GetMaterialApp extends StatelessWidget { @@ -51,6 +52,7 @@ class GetMaterialApp extends StatelessWidget {
51 this.getPages, 52 this.getPages,
52 this.opaqueRoute, 53 this.opaqueRoute,
53 this.enableLog, 54 this.enableLog,
  55 + this.logWriterCallback,
54 this.popGesture, 56 this.popGesture,
55 this.transitionDuration, 57 this.transitionDuration,
56 this.defaultGlobalState, 58 this.defaultGlobalState,
@@ -103,6 +105,7 @@ class GetMaterialApp extends StatelessWidget { @@ -103,6 +105,7 @@ class GetMaterialApp extends StatelessWidget {
103 final VoidCallback onInit; 105 final VoidCallback onInit;
104 final VoidCallback onDispose; 106 final VoidCallback onDispose;
105 final bool enableLog; 107 final bool enableLog;
  108 + final LogWriterCallback logWriterCallback;
106 final bool popGesture; 109 final bool popGesture;
107 final SmartManagement smartManagement; 110 final SmartManagement smartManagement;
108 final Bindings initialBinding; 111 final Bindings initialBinding;
@@ -201,6 +204,7 @@ class GetMaterialApp extends StatelessWidget { @@ -201,6 +204,7 @@ class GetMaterialApp extends StatelessWidget {
201 204
202 Get.config( 205 Get.config(
203 enableLog: enableLog ?? GetConfig.isLogEnable, 206 enableLog: enableLog ?? GetConfig.isLogEnable,
  207 + logWriterCallback: logWriterCallback,
204 defaultTransition: defaultTransition ?? Get.defaultTransition, 208 defaultTransition: defaultTransition ?? Get.defaultTransition,
205 defaultOpaqueRoute: opaqueRoute ?? Get.isOpaqueRouteDefault, 209 defaultOpaqueRoute: opaqueRoute ?? Get.isOpaqueRouteDefault,
206 defaultPopGesture: popGesture ?? Get.isPopGestureEnable, 210 defaultPopGesture: popGesture ?? Get.isPopGestureEnable,
@@ -49,17 +49,13 @@ class GetObserver extends NavigatorObserver { @@ -49,17 +49,13 @@ class GetObserver extends NavigatorObserver {
49 @override 49 @override
50 void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { 50 void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
51 if ('${route?.settings?.name}' == 'snackbar') { 51 if ('${route?.settings?.name}' == 'snackbar') {
52 - if (GetConfig.isLogEnable)  
53 - print("[GETX] OPEN SNACKBAR ${route?.settings?.name}"); 52 + GetConfig.log("[GETX] OPEN SNACKBAR ${route?.settings?.name}");
54 } else if ('${route?.settings?.name}' == 'bottomsheet') { 53 } else if ('${route?.settings?.name}' == 'bottomsheet') {
55 - if (GetConfig.isLogEnable)  
56 - print("[GETX] OPEN BOTTOMSHEET ${route?.settings?.name}"); 54 + GetConfig.log("[GETX] OPEN BOTTOMSHEET ${route?.settings?.name}");
57 } else if ('${route?.settings?.name}' == 'dialog') { 55 } else if ('${route?.settings?.name}' == 'dialog') {
58 - if (GetConfig.isLogEnable)  
59 - print("[GETX] OPEN DIALOG ${route?.settings?.name}"); 56 + GetConfig.log("[GETX] OPEN DIALOG ${route?.settings?.name}");
60 } else { 57 } else {
61 - if (GetConfig.isLogEnable)  
62 - print("[GETX] GOING TO ROUTE ${route?.settings?.name}"); 58 + GetConfig.log("[GETX] GOING TO ROUTE ${route?.settings?.name}");
63 } 59 }
64 60
65 _routeSend.update((value) { 61 _routeSend.update((value) {
@@ -81,17 +77,13 @@ class GetObserver extends NavigatorObserver { @@ -81,17 +77,13 @@ class GetObserver extends NavigatorObserver {
81 super.didPop(route, previousRoute); 77 super.didPop(route, previousRoute);
82 78
83 if ('${route?.settings?.name}' == 'snackbar') { 79 if ('${route?.settings?.name}' == 'snackbar') {
84 - if (GetConfig.isLogEnable)  
85 - print("[GETX] CLOSE SNACKBAR ${route?.settings?.name}"); 80 + GetConfig.log("[GETX] CLOSE SNACKBAR ${route?.settings?.name}");
86 } else if ('${route?.settings?.name}' == 'bottomsheet') { 81 } else if ('${route?.settings?.name}' == 'bottomsheet') {
87 - if (GetConfig.isLogEnable)  
88 - print("[GETX] CLOSE BOTTOMSHEET ${route?.settings?.name}"); 82 + GetConfig.log("[GETX] CLOSE BOTTOMSHEET ${route?.settings?.name}");
89 } else if ('${route?.settings?.name}' == 'dialog') { 83 } else if ('${route?.settings?.name}' == 'dialog') {
90 - if (GetConfig.isLogEnable)  
91 - print("[GETX] CLOSE DIALOG ${route?.settings?.name}"); 84 + GetConfig.log("[GETX] CLOSE DIALOG ${route?.settings?.name}");
92 } else { 85 } else {
93 - if (GetConfig.isLogEnable)  
94 - print("[GETX] BACK ROUTE ${route?.settings?.name}"); 86 + GetConfig.log("[GETX] BACK ROUTE ${route?.settings?.name}");
95 } 87 }
96 88
97 _routeSend.update((value) { 89 _routeSend.update((value) {
@@ -112,10 +104,8 @@ class GetObserver extends NavigatorObserver { @@ -112,10 +104,8 @@ class GetObserver extends NavigatorObserver {
112 @override 104 @override
113 void didReplace({Route newRoute, Route oldRoute}) { 105 void didReplace({Route newRoute, Route oldRoute}) {
114 super.didReplace(newRoute: newRoute, oldRoute: oldRoute); 106 super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
115 - if (GetConfig.isLogEnable)  
116 - print("[GETX] REPLACE ROUTE ${oldRoute?.settings?.name}");  
117 - if (GetConfig.isLogEnable)  
118 - print("[GETX] NEW ROUTE ${newRoute?.settings?.name}"); 107 + GetConfig.log("[GETX] REPLACE ROUTE ${oldRoute?.settings?.name}");
  108 + GetConfig.log("[GETX] NEW ROUTE ${newRoute?.settings?.name}");
119 109
120 _routeSend.update((value) { 110 _routeSend.update((value) {
121 if (newRoute is PageRoute) value.current = '${newRoute?.settings?.name}'; 111 if (newRoute is PageRoute) value.current = '${newRoute?.settings?.name}';
@@ -134,8 +124,7 @@ class GetObserver extends NavigatorObserver { @@ -134,8 +124,7 @@ class GetObserver extends NavigatorObserver {
134 @override 124 @override
135 void didRemove(Route route, Route previousRoute) { 125 void didRemove(Route route, Route previousRoute) {
136 super.didRemove(route, previousRoute); 126 super.didRemove(route, previousRoute);
137 - if (GetConfig.isLogEnable)  
138 - print("[GETX] REMOVING ROUTE ${route?.settings?.name}"); 127 + GetConfig.log("[GETX] REMOVING ROUTE ${route?.settings?.name}");
139 128
140 _routeSend.update((value) { 129 _routeSend.update((value) {
141 value.route = previousRoute; 130 value.route = previousRoute;
@@ -100,7 +100,7 @@ class Worker { @@ -100,7 +100,7 @@ class Worker {
100 final String type; 100 final String type;
101 101
102 void _message() { 102 void _message() {
103 - if (GetConfig.isLogEnable) print('[Getx] Worker $type disposed'); 103 + GetConfig.log('[Getx] Worker $type disposed');
104 } 104 }
105 105
106 void dispose() { 106 void dispose() {