mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 08:46:18 +01:00
Refactor: extract log package
This commit is contained in:
parent
4e5322553f
commit
9d7d97b924
24 changed files with 79 additions and 65 deletions
|
@ -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() {
|
||||
|
|
|
@ -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<dynamic> 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 = <String>[];
|
||||
bool _isEnable = false;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
static LogCapturer? _inst;
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,6 +12,8 @@ dependencies:
|
|||
flutter:
|
||||
sdk: flutter
|
||||
logging: any
|
||||
np_log:
|
||||
path: ../np_log
|
||||
|
||||
dev_dependencies:
|
||||
np_lints:
|
||||
|
|
7
np_log/.gitignore
vendored
Normal file
7
np_log/.gitignore
vendored
Normal file
|
@ -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
|
1
np_log/analysis_options.yaml
Normal file
1
np_log/analysis_options.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
include: package:np_lints/np.yaml
|
3
np_log/lib/np_log.dart
Normal file
3
np_log/lib/np_log.dart
Normal file
|
@ -0,0 +1,3 @@
|
|||
library np_log;
|
||||
|
||||
export '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);
|
||||
});
|
||||
}
|
14
np_log/pubspec.yaml
Normal file
14
np_log/pubspec.yaml
Normal file
|
@ -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
|
Loading…
Reference in a new issue