Skip to content

Commit

Permalink
Feat: Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jun 25, 2021
1 parent 92584e3 commit 346af19
Show file tree
Hide file tree
Showing 43 changed files with 967 additions and 202 deletions.
3 changes: 2 additions & 1 deletion jsons/eh_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"showPageInterval?": true,
"orientation?": "",
"vibrate?": true,
"tagIntroImgLv?": ""
"tagIntroImgLv?": "",
"debugMode?": false
}
10 changes: 5 additions & 5 deletions lib/common/controller/cache_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CacheController extends GetxController with StateMixin<String> {

Future<String> getTotCacheSize() async {
final int _cachesize = await _loadCache();
logger.d('tot cacheSize ${renderSize(_cachesize)}');
// logger.d('tot cacheSize ${renderSize(_cachesize)}');
return renderSize(_cachesize);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ class CacheController extends GetxController with StateMixin<String> {
logger.d('临时目录大小: ' + value.toString());
return value;
} catch (err) {
print(err);
logger.e(err);
return 0;
}
}
Expand All @@ -94,7 +94,7 @@ class CacheController extends GetxController with StateMixin<String> {
}
return 0;
} catch (e) {
print(e);
logger.e('$e');
return 0;
}
}
Expand All @@ -106,7 +106,7 @@ class CacheController extends GetxController with StateMixin<String> {
//删除缓存目录
await delDir(tempDir);
} catch (e) {
print(e);
logger.e('$e');
}
}

Expand All @@ -123,7 +123,7 @@ class CacheController extends GetxController with StateMixin<String> {
await file.delete();
}
} catch (e) {
print(e);
logger.e('$e');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/common/controller/tag_trans_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TagTransController extends GetxController {
}
_dbUrl = assMap['db.raw.json.gz'] ?? '';

logger.v(_dbUrl);
logger.i(_dbUrl);
return true;
}

Expand Down Expand Up @@ -172,7 +172,7 @@ class TagTransController extends GetxController {
}
logger.d(text);
final array = text.split(RegExp(r'\s+'));
logger.v(array.map((e) => '[$e]').join(','));
logger.i(array.map((e) => '[$e]').join(','));

for (int i = 0; i < array.length; i++) {
// print(array[i]);
Expand Down
2 changes: 1 addition & 1 deletion lib/common/exts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension EhString on String {
_dohDnsCacheIndex > -1 ? _dohDnsCacheList[_dohDnsCacheIndex] : null;
realHost = dohDnsCache?.addr ?? host;
final String realUrl = replaceFirst(host, realHost);
logger.i('realUrl: $realUrl');
logger.d('realUrl: $realUrl');
return realUrl;
}
return this;
Expand Down
4 changes: 3 additions & 1 deletion lib/common/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ class Global {

await FlutterDownloader.initialize(debug: Global.inDebugMode);

if (!inDebugMode) Logger.level = Level.info;
if (!inDebugMode) {
Logger.level = Level.info;
}

canCheckBiometrics = await localAuth.canCheckBiometrics;

Expand Down
2 changes: 1 addition & 1 deletion lib/common/isolate/child.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Future<void> _fetchAllImageInfo(
}
}
} catch (e, stack) {
logger.d('$e\n$stack');
logger.e('$e\n$stack');
// rethrow;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/parser/gallery_list_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class GalleryListParser {
_translated = EHUtils.getLangeage(_langTag?.text ?? '');
}
} catch (e, stack) {
logger.d('$e\n$stack');
logger.e('$e\n$stack');
}

// 封面图片
Expand Down
9 changes: 9 additions & 0 deletions lib/common/service/ehconfig_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class EhConfigService extends ProfileService {
// 震动总开关
RxBool vibrate = true.obs;

final _debugMode = false.obs;
get debugMode => _debugMode.value;
set debugMode(val) => _debugMode.value = val;

@override
void onInit() {
super.onInit();
Expand Down Expand Up @@ -212,6 +216,11 @@ class EhConfigService extends ProfileService {
TagIntroImgLv.nonh;
everFromEunm(tagIntroImgLv,
(String value) => ehConfig = ehConfig.copyWith(tagIntroImgLv: value));

//
debugMode = ehConfig.debugMode ?? false;
everProfile<bool>(_debugMode,
(bool value) => ehConfig = ehConfig.copyWith(debugMode: value));
}

/// 收藏排序
Expand Down
97 changes: 97 additions & 0 deletions lib/common/service/log_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:io';

import 'package:fehviewer/utils/logger.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart' as path;

import '../global.dart';

const _kMaxTime = Duration(days: 7);
const _kFilenameFormat = 'yyyy-MM-dd HH:mm:ss';
const _kSuffix = '.log';

class LogService extends GetxService {
final RxList<File> _logFiles = <File>[].obs;
List<File> get logFiles {
return _logFiles.value;
}

set logFiles(val) => _logFiles.value = val;

final _curFileName = 'eh.log'.obs;
get curFileName => _curFileName.value;
set curFileName(val) => _curFileName.value = val;

final _logPath = ''.obs;
get logPath => _logPath.value;
set logPath(val) => _logPath.value = val;

final Rx<Level> _logLevel = Level.error.obs;
Level get logLevel => _logLevel.value;
set logLevel(val) => _logLevel.value = val;

@override
void onInit() {
super.onInit();

logPath = path.join(
GetPlatform.isAndroid ? Global.extStorePath : Global.appDocPath, 'log');
if (!Directory(logPath).existsSync()) {
Directory(logPath).createSync(recursive: true);
}

final DateTime _now = DateTime.now();
final DateFormat formatter = DateFormat(_kFilenameFormat);
final String _fileName = formatter.format(_now);
curFileName = '$_fileName$_kSuffix';
}

Future<List<File>?> loadFiles() async {
List<File> _files = [];
final Directory appDocDir = Directory(logPath);
final Stream<FileSystemEntity> entityList =
appDocDir.list(recursive: false, followLinks: false);
await for (final FileSystemEntity entity in entityList) {
if (entity.path.endsWith(_kSuffix)) {
final File _logFile = File(entity.path);

try {
// 超过最大时间的文件删除
final _fileName = path.basename(_logFile.path);
final _timeString = _fileName.replaceAll(_kSuffix, '');
logger.i('log file time $_timeString');

final DateTime _nowTime = DateTime.now();
final DateFormat formatter = DateFormat(_kFilenameFormat);

final _fileTime = formatter.parse(_timeString);
// final _fileTime = DateTime.parse(_timeString);
final _diff = _nowTime.difference(_fileTime);
if (_diff.compareTo(_kMaxTime) > 0) {
logger.d('delete $_fileName');
_logFile.delete();
}
} catch (_) {}

_files.add(_logFile);
}
}
_files
.sort((a, b) => path.basename(b.path).compareTo(path.basename(a.path)));
_logFiles(_files);
}

void removeLogAt(int index) {
_logFiles[index].deleteSync();
_logFiles.removeAt(index);
}

void removeAll() {
for (final _file in _logFiles) {
_file.delete();
}
_logFiles.clear();
}
}
11 changes: 11 additions & 0 deletions lib/const/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,15 @@ class EHConst {
'thai': 'TH',
'vietnamese': 'VI',
};

static List<String> fontFamilyFallback = [
'miui',
'sans-serif',
];
static List<String> monoFontFamilyFallback = [
'monaco',
'monospace',
'Menlo',
'Courier New',
];
}
19 changes: 15 additions & 4 deletions lib/const/theme_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import 'package:fehviewer/common/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'const.dart';

final ehTextTheme = const CupertinoTextThemeData().copyWith(
textStyle: const CupertinoTextThemeData().textStyle.copyWith(
fontFamilyFallback: EHConst.fontFamilyFallback,
));

enum ThemesModeEnum {
system,
ligthMode,
Expand Down Expand Up @@ -132,31 +139,35 @@ class ThemeColors {

/// 主题配置
/// 浅色主题
static CupertinoThemeData ligthTheme = const CupertinoThemeData(
static CupertinoThemeData ligthTheme = CupertinoThemeData(
brightness: Brightness.light,
barBackgroundColor: navigationBarBackground,
textTheme: ehTextTheme,
// scaffoldBackgroundColor: CupertinoColors.secondarySystemBackground,
);

/// 浅色主题
static CupertinoThemeData ligthThemeSecondary = const CupertinoThemeData(
static CupertinoThemeData ligthThemeSecondary = CupertinoThemeData(
brightness: Brightness.light,
barBackgroundColor: navigationBarBackground,
scaffoldBackgroundColor: CupertinoColors.secondarySystemBackground,
textTheme: ehTextTheme,
);

/// 深色纯黑主题
static CupertinoThemeData darkPureTheme = const CupertinoThemeData(
static CupertinoThemeData darkPureTheme = CupertinoThemeData(
brightness: Brightness.dark,
barBackgroundColor: navigationBarBackground,
// scaffoldBackgroundColor: CupertinoColors.secondarySystemBackground,
textTheme: ehTextTheme,
);

/// 深色灰黑
static CupertinoThemeData darkGrayTheme = const CupertinoThemeData(
static CupertinoThemeData darkGrayTheme = CupertinoThemeData(
brightness: Brightness.dark,
scaffoldBackgroundColor: Color.fromARGB(255, 30, 30, 30),
barBackgroundColor: navigationBarBackgroundGray,
textTheme: ehTextTheme,
);

// 画廊类型主题色
Expand Down
19 changes: 18 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import 'package:fehviewer/common/global.dart';
import 'package:fehviewer/common/service/dns_service.dart';
import 'package:fehviewer/common/service/ehconfig_service.dart';
import 'package:fehviewer/common/service/locale_service.dart';
import 'package:fehviewer/common/service/log_service.dart';
import 'package:fehviewer/common/service/theme_service.dart';
import 'package:fehviewer/generated/l10n.dart';
import 'package:fehviewer/pages/controller/fav_dialog_controller.dart';
import 'package:fehviewer/pages/controller/favorite_sel_controller.dart';
import 'package:fehviewer/pages/tab/controller/download_view_controller.dart';
import 'package:fehviewer/pages/tab/controller/splash_controller.dart';
import 'package:fehviewer/pages/tab/view/splash_page.dart';
import 'package:fehviewer/route/app_pages.dart';
import 'package:fehviewer/route/routes.dart';
import 'package:fehviewer/store/get_store.dart';
Expand All @@ -22,6 +25,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';
import 'package:oktoast/oktoast.dart';

import 'common/controller/advance_search_controller.dart';
Expand All @@ -40,12 +44,14 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await _initializeFlutterFire();
runZonedGuarded<Future<void>>(() async {
Get.lazyPut(() => LogService(), fenix: true);
Get.lazyPut(() => GStore());
await Global.init();

await _initializeFlutterFire();

Get.lazyPut(() => EhConfigService(), fenix: true);

//LocaleController
Get.lazyPut(() => LocaleService(), fenix: true);
// ThemeController
Expand Down Expand Up @@ -76,6 +82,16 @@ Future<void> main() async {
Get.lazyPut(() => UnlockPageController(), fenix: true);
Get.lazyPut(() => TagTransController(), fenix: true);

Get.lazyPut(() => SplashController());

if (Get.find<EhConfigService>().debugMode) {
print('Level.verbose');
Logger.level = Level.debug;
logger.v('Level.verbose');
} else {
Logger.level = Level.error;
}

runApp(MyApp());
}, (Object error, StackTrace stackTrace) {
logger.e('runZonedGuarded: Caught error in my root zone.');
Expand Down Expand Up @@ -165,7 +181,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
// ],
getPages: AppPages.routes,
defaultTransition: Transition.cupertino,
initialRoute: EHRoutes.root,
// initialRoute: EHRoutes.root,
home: SplashPage(),
theme: theme,
locale: locale,
logWriterCallback: loggerGetx,
Expand Down
Loading

0 comments on commit 346af19

Please sign in to comment.