diff --git a/app/lib/app_init.dart b/app/lib/app_init.dart index dfe42504..62969d18 100644 --- a/app/lib/app_init.dart +++ b/app/lib/app_init.dart @@ -4,7 +4,6 @@ import 'package:event_bus/event_bus.dart'; import 'package:flutter/foundation.dart'; import 'package:kiwi/kiwi.dart'; import 'package:logging/logging.dart'; -import 'package:nc_photos/debug_util.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/entity/album.dart'; import 'package:nc_photos/entity/album/data_source.dart'; @@ -46,6 +45,7 @@ import 'package:nc_photos/mobile/self_signed_cert_manager.dart'; import 'package:nc_photos/platform/features.dart' as features; import 'package:nc_photos/platform/k.dart' as platform_k; import 'package:nc_photos/touch_manager.dart'; +import 'package:np_log/np_log.dart' as np_log; import 'package:visibility_detector/visibility_detector.dart'; enum InitIsolateType { @@ -98,43 +98,10 @@ void initLog() { return; } - Logger.root.level = kReleaseMode ? Level.WARNING : Level.ALL; - Logger.root.onRecord.listen((record) { - // dev.log( - // "${record.level.name} ${record.time}: ${record.message}", - // time: record.time, - // sequenceNumber: record.sequenceNumber, - // level: record.level.value, - // name: record.loggerName, - // ); - String msg = - "[${record.loggerName}] ${record.level.name} ${record.time}: ${record.message}"; - if (record.error != null) { - msg += " (throw: ${record.error.runtimeType} { ${record.error} })"; - } - if (record.stackTrace != null) { - msg += "\nStack Trace:\n${record.stackTrace}"; - } - - if (kDebugMode) { - // show me colors! - int color; - if (record.level >= Level.SEVERE) { - color = 91; - } else if (record.level >= Level.WARNING) { - color = 33; - } else if (record.level >= Level.INFO) { - color = 34; - } else if (record.level >= Level.FINER) { - color = 32; - } else { - color = 90; - } - msg = "\x1B[${color}m$msg\x1B[0m"; - } - debugPrint(msg, wrapWidth: 1024); - LogCapturer().onLog(msg); - }); + np_log.initLog( + isDebugMode: kDebugMode, + print: (log) => debugPrint(log, wrapWidth: 1024), + ); } void initDrift() { diff --git a/app/lib/debug_util.dart b/app/lib/debug_util.dart index ec5740ac..22e5c0c3 100644 --- a/app/lib/debug_util.dart +++ b/app/lib/debug_util.dart @@ -1,9 +1,11 @@ +import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:nc_photos/mobile/platform.dart' if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform; import 'package:np_common/string_extension.dart'; +import 'package:np_log/np_log.dart'; import 'package:path/path.dart' as path_lib; class LogCapturer { @@ -16,28 +18,23 @@ class LogCapturer { /// Start capturing logs void start() { - _isEnable = true; + _subscription ??= LogStream().stream.listen(_logs.add); } /// Stop capturing and save the captured logs Future stop() { - _isEnable = false; + _subscription?.cancel(); + _subscription = null; final saver = platform.FileSaver(); final content = const Utf8Encoder().convert(_logs.join("\n")); _logs.clear(); return saver.saveFile("nc-photos.log", content); } - void onLog(String log) { - if (_isEnable) { - _logs.add(log); - } - } - - bool get isEnable => _isEnable; + bool get isEnable => _subscription != null; final _logs = []; - bool _isEnable = false; + StreamSubscription? _subscription; static LogCapturer? _inst; } diff --git a/app/pubspec.lock b/app/pubspec.lock index 4c10dcba..d6c2604c 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -976,6 +976,13 @@ packages: relative: true source: path version: "1.0.0" + np_log: + dependency: "direct main" + description: + path: "../np_log" + relative: true + source: path + version: "1.0.0" octo_image: dependency: "direct main" description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index dfd738b3..cc5b6765 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -103,6 +103,8 @@ dependencies: path: ../codegen np_common: path: ../np_common + np_log: + path: ../np_log octo_image: any page_view_indicators: ^2.0.0 path: ^1.8.0 diff --git a/np_api/lib/src/entity/face_recognition_face_parser.dart b/np_api/lib/src/entity/face_recognition_face_parser.dart index 07855c3b..31c4498e 100644 --- a/np_api/lib/src/entity/face_recognition_face_parser.dart +++ b/np_api/lib/src/entity/face_recognition_face_parser.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:np_api/src/entity/entity.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_codegen/np_codegen.dart'; -import 'package:np_common/log.dart'; import 'package:np_common/type.dart'; part 'face_recognition_face_parser.g.dart'; diff --git a/np_api/lib/src/entity/face_recognition_person_parser.dart b/np_api/lib/src/entity/face_recognition_person_parser.dart index 8e0757a6..b7012398 100644 --- a/np_api/lib/src/entity/face_recognition_person_parser.dart +++ b/np_api/lib/src/entity/face_recognition_person_parser.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:np_api/src/entity/entity.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_codegen/np_codegen.dart'; -import 'package:np_common/log.dart'; import 'package:np_common/type.dart'; part 'face_recognition_person_parser.g.dart'; diff --git a/np_api/lib/src/entity/favorite_parser.dart b/np_api/lib/src/entity/favorite_parser.dart index e5d8205b..520c5d6e 100644 --- a/np_api/lib/src/entity/favorite_parser.dart +++ b/np_api/lib/src/entity/favorite_parser.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:xml/xml.dart'; class FavoriteParser extends XmlResponseParser { diff --git a/np_api/lib/src/entity/file_parser.dart b/np_api/lib/src/entity/file_parser.dart index 72feff49..8dc18c66 100644 --- a/np_api/lib/src/entity/file_parser.dart +++ b/np_api/lib/src/entity/file_parser.dart @@ -4,8 +4,8 @@ import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_codegen/np_codegen.dart'; -import 'package:np_common/log.dart'; import 'package:xml/xml.dart'; part 'file_parser.g.dart'; diff --git a/np_api/lib/src/entity/nc_album_item_parser.dart b/np_api/lib/src/entity/nc_album_item_parser.dart index f037a9d2..e859886e 100644 --- a/np_api/lib/src/entity/nc_album_item_parser.dart +++ b/np_api/lib/src/entity/nc_album_item_parser.dart @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:xml/xml.dart'; class NcAlbumItemParser extends XmlResponseParser { diff --git a/np_api/lib/src/entity/nc_album_parser.dart b/np_api/lib/src/entity/nc_album_parser.dart index 3fc8b3d6..ad112064 100644 --- a/np_api/lib/src/entity/nc_album_parser.dart +++ b/np_api/lib/src/entity/nc_album_parser.dart @@ -3,7 +3,7 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_common/type.dart'; import 'package:xml/xml.dart'; diff --git a/np_api/lib/src/entity/recognize_face_item_parser.dart b/np_api/lib/src/entity/recognize_face_item_parser.dart index 7f5da8ca..9347f0fa 100644 --- a/np_api/lib/src/entity/recognize_face_item_parser.dart +++ b/np_api/lib/src/entity/recognize_face_item_parser.dart @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_common/type.dart'; import 'package:xml/xml.dart'; diff --git a/np_api/lib/src/entity/recognize_face_parser.dart b/np_api/lib/src/entity/recognize_face_parser.dart index de4c198c..cf78eb3a 100644 --- a/np_api/lib/src/entity/recognize_face_parser.dart +++ b/np_api/lib/src/entity/recognize_face_parser.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:xml/xml.dart'; class RecognizeFaceParser extends XmlResponseParser { diff --git a/np_api/lib/src/entity/share_parser.dart b/np_api/lib/src/entity/share_parser.dart index 68e5ed93..18b8db2d 100644 --- a/np_api/lib/src/entity/share_parser.dart +++ b/np_api/lib/src/entity/share_parser.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:np_api/src/entity/entity.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_codegen/np_codegen.dart'; -import 'package:np_common/log.dart'; import 'package:np_common/type.dart'; part 'share_parser.g.dart'; diff --git a/np_api/lib/src/entity/sharee_parser.dart b/np_api/lib/src/entity/sharee_parser.dart index 4d64f3ad..6efb550e 100644 --- a/np_api/lib/src/entity/sharee_parser.dart +++ b/np_api/lib/src/entity/sharee_parser.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:np_api/src/entity/entity.dart'; +import 'package:np_api/src/util.dart'; import 'package:np_codegen/np_codegen.dart'; -import 'package:np_common/log.dart'; import 'package:np_common/type.dart'; part 'sharee_parser.g.dart'; diff --git a/np_api/lib/src/entity/tag_parser.dart b/np_api/lib/src/entity/tag_parser.dart index e5667435..73879b5b 100644 --- a/np_api/lib/src/entity/tag_parser.dart +++ b/np_api/lib/src/entity/tag_parser.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:xml/xml.dart'; class TagParser extends XmlResponseParser { diff --git a/np_api/lib/src/entity/tagged_file_parser.dart b/np_api/lib/src/entity/tagged_file_parser.dart index 3d5b384f..aaddc7be 100644 --- a/np_api/lib/src/entity/tagged_file_parser.dart +++ b/np_api/lib/src/entity/tagged_file_parser.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:np_api/src/entity/entity.dart'; import 'package:np_api/src/entity/parser.dart'; -import 'package:np_common/log.dart'; +import 'package:np_api/src/util.dart'; import 'package:xml/xml.dart'; class TaggedFileParser extends XmlResponseParser { diff --git a/np_api/lib/src/util.dart b/np_api/lib/src/util.dart index 03900fec..f048f008 100644 --- a/np_api/lib/src/util.dart +++ b/np_api/lib/src/util.dart @@ -1 +1,11 @@ +import 'package:flutter/foundation.dart'; +import 'package:np_log/np_log.dart' as np_log; + bool isHttpStatusGood(int status) => status ~/ 100 == 2; + +void initLog() { + np_log.initLog( + isDebugMode: kDebugMode, + print: (log) => debugPrint(log, wrapWidth: 1024), + ); +} diff --git a/np_api/pubspec.yaml b/np_api/pubspec.yaml index 5ed59046..b97997bc 100644 --- a/np_api/pubspec.yaml +++ b/np_api/pubspec.yaml @@ -18,6 +18,8 @@ dependencies: path: ../codegen np_common: path: ../np_common + np_log: + path: ../np_log to_string: git: url: https://gitlab.com/nkming2/dart-to-string diff --git a/np_common/pubspec.yaml b/np_common/pubspec.yaml index d8024a1b..f72f27c1 100644 --- a/np_common/pubspec.yaml +++ b/np_common/pubspec.yaml @@ -12,6 +12,8 @@ dependencies: flutter: sdk: flutter logging: any + np_log: + path: ../np_log dev_dependencies: np_lints: diff --git a/np_log/.gitignore b/np_log/.gitignore new file mode 100644 index 00000000..3cceda55 --- /dev/null +++ b/np_log/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ + +# Avoid committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/np_log/analysis_options.yaml b/np_log/analysis_options.yaml new file mode 100644 index 00000000..f92d2567 --- /dev/null +++ b/np_log/analysis_options.yaml @@ -0,0 +1 @@ +include: package:np_lints/np.yaml diff --git a/np_log/lib/np_log.dart b/np_log/lib/np_log.dart new file mode 100644 index 00000000..8420de49 --- /dev/null +++ b/np_log/lib/np_log.dart @@ -0,0 +1,3 @@ +library np_log; + +export 'src/log.dart'; diff --git a/np_common/lib/log.dart b/np_log/lib/src/log.dart similarity index 85% rename from np_common/lib/log.dart rename to np_log/lib/src/log.dart index 8ca09aa1..42bb56ac 100644 --- a/np_common/lib/log.dart +++ b/np_log/lib/src/log.dart @@ -1,10 +1,12 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; -void initLog() { - Logger.root.level = kReleaseMode ? Level.WARNING : Level.ALL; +void initLog({ + required bool isDebugMode, + void Function(String) print = print, +}) { + Logger.root.level = !isDebugMode ? Level.WARNING : Level.ALL; Logger.root.onRecord.listen((record) { String msg = "[${record.loggerName}] ${record.level.name} ${record.time}: ${record.message}"; @@ -15,7 +17,7 @@ void initLog() { msg += "\nStack Trace:\n${record.stackTrace}"; } - if (kDebugMode) { + if (isDebugMode) { // show me colors! int color; if (record.level >= Level.SEVERE) { @@ -31,7 +33,7 @@ void initLog() { } msg = "\x1B[${color}m$msg\x1B[0m"; } - debugPrint(msg, wrapWidth: 1024); + print(msg); LogStream().add(msg); }); } diff --git a/np_log/pubspec.yaml b/np_log/pubspec.yaml new file mode 100644 index 00000000..6e3aa690 --- /dev/null +++ b/np_log/pubspec.yaml @@ -0,0 +1,14 @@ +name: np_log +description: A starting point for Dart libraries or applications. +version: 1.0.0 +publish_to: none + +environment: + sdk: '>=2.19.6 <3.0.0' + +dependencies: + logging: ^1.1.1 + +dev_dependencies: + np_lints: + path: ../np_lints