Skip to content

Commit

Permalink
feat(renderSite): 在模板中使用 print 可以在日志中查看
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-light committed Jan 15, 2025
1 parent a413a01 commit 8a30d60
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
16 changes: 8 additions & 8 deletions lib/helpers/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class Log {
static set level(Level logLevel) => Logger.level = logLevel;

/// 初始化
static Future<void> initialized() async {
static Future<void> initialized({bool background = false}) async {
Level level = kReleaseMode ? Level.info : Level.trace;
// 流输出
MemoryOutput memoryOutput = MemoryOutput(bufferSize: 50);
buffer = memoryOutput.buffer;
// 列表
final lists = [if (!kReleaseMode) Logger.defaultOutput(), memoryOutput];
// 文件输出
if (kReleaseMode) {
if (kReleaseMode && !background) {
// 应用程序支持目录, 即配置所在的目录
var path = FS.normalize((await getApplicationSupportDirectory()).path);
path = FS.join(path, 'log');
Expand All @@ -45,32 +45,32 @@ class Log {

/// 在级别记录消息 [Level.trace]
static void t(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.t(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.trace, message, time: time, error: error, stackTrace: stackTrace);
}

/// 在级别记录消息 [Level.debug]
static void d(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.d(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.debug, message, time: time, error: error, stackTrace: stackTrace);
}

/// 在级别记录消息 [Level.info]
static void i(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.i(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.info, message, time: time, error: error, stackTrace: stackTrace);
}

/// 在级别记录消息 [Level.warning]
static void w(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.w(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.warning, message, time: time, error: error, stackTrace: stackTrace);
}

/// 在级别记录消息 [Level.error]
static void e(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.e(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.error, message, time: time, error: error, stackTrace: stackTrace);
}

/// 在级别记录消息 [Level.fatal]
static void f(dynamic message, {DateTime? time, Object? error, StackTrace? stackTrace}) {
Log.instance.f(message, time: time, error: error, stackTrace: stackTrace);
Log.instance.log(Level.fatal, message, time: time, error: error, stackTrace: stackTrace);
}

/// 用 [Log.level] 级别记录消息
Expand Down
8 changes: 8 additions & 0 deletions lib/helpers/render/filter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:collection/collection.dart' show IterableExtension;
import 'package:glidea/helpers/log.dart';
import 'package:glidea/interfaces/types.dart';
import 'package:glidea/library/worker/worker.dart';

/// jinja 使用的筛选器
class RenderFilter {
Expand All @@ -9,6 +11,7 @@ class RenderFilter {
'substring': _doSubstring,
'sublist': _doSublist,
'dedup': _doDeduplication,
'print': _doPrint,
};

/// sort 筛选器, 将列表进行排序
Expand Down Expand Up @@ -65,4 +68,9 @@ class RenderFilter {

return values.toSet() ?? {};
}

/// 打印 value
static void _doPrint(Object? value) {
BackgroundProcess.instance?.log(value.toString());
}
}
67 changes: 41 additions & 26 deletions lib/library/worker/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ base mixin BackgroundAction on BackgroundWorker {
static const Symbol _loadSiteData = Symbol('loadSiteData');
static const Symbol _saveSiteData = Symbol('saveSiteData');
static const Symbol _loadAsset = Symbol('loadAsset');
static const Symbol _log = Symbol('log');

@override
Future<dynamic> invoke(Invocation invocation) async {
final post = invocation.positionalArguments;
return switch (invocation.memberName) {
BackgroundAction._loadAsset => _loadAssetBundle(post[0]),
_ => await super.invoke(invocation),
};
Future<void> onInit() async {
await super.onInit();
_invokes.addAll({
BackgroundAction._loadAsset: _loadAssetBundle,
BackgroundAction._log: _printLog,
});
}

/// 发布站点
Expand Down Expand Up @@ -51,24 +52,39 @@ base mixin BackgroundAction on BackgroundWorker {
Future<Uint8List> _loadAssetBundle(String key) async {
return (await rootBundle.load(key)).buffer.asUint8List();
}

// 打印日志
Future<void> _printLog(Level level, dynamic message) async {
Log.log(level, message);
}
}

/// 用于调用前台的操作
base mixin ActionBack on WorkerProcess {
/// 加载资源猫
Future<Uint8List> loadAssets(String path) async {
return await call<Uint8List>(BackgroundAction._loadAsset, [path]);
}

/// 打印消息
Future<void> log(dynamic message, {Level level = Level.info}) async {
await call(BackgroundAction._log, [level, message]);
}
}

/// 远程操作
base mixin RemoteBack on WorkerProcess {
base mixin RemoteBack on ActionBack {
/// 今天文件服务
HttpServer? fileServer;

@override
Future<dynamic> invoke(Invocation invocation) async {
final post = invocation.positionalArguments;
switch (invocation.memberName) {
case BackgroundAction._publishSite:
await publishSite(post[0]);
case BackgroundAction._previewSite:
await previewSite(post[0]);
case BackgroundAction._remoteDetect:
await remoteDetect(post[0], post[1], post[2]);
}
Future<void> onInit() async {
await super.onInit();
_invokes.addAll({
BackgroundAction._publishSite: publishSite,
BackgroundAction._previewSite: previewSite,
BackgroundAction._remoteDetect: remoteDetect,
});
}

/// 发布站点
Expand Down Expand Up @@ -121,18 +137,17 @@ base mixin RemoteBack on WorkerProcess {
}

/// 加载本地数据
base mixin DataBack on RemoteBack {
base mixin DataBack on ActionBack {
// 创建开始
bool _isCreate = true;

@override
Future<dynamic> invoke(Invocation invocation) async {
final post = invocation.positionalArguments;
return switch (invocation.memberName) {
BackgroundAction._loadSiteData => await loadSiteData(),
BackgroundAction._saveSiteData => await saveSiteData(post[0]),
_ => super.invoke(invocation),
};
Future<void> onInit() async {
await super.onInit();
_invokes.addAll({
BackgroundAction._loadSiteData: loadSiteData,
BackgroundAction._saveSiteData: saveSiteData,
});
}

/// 从 config/config.json 中加载配置
Expand Down Expand Up @@ -203,7 +218,7 @@ base mixin DataBack on RemoteBack {
if (isCreate) {
site.appDir = defaultSiteDir;
// 在 Isolate 中无法使用 rootBundle.load
final bytes = await call<Uint8List>(BackgroundAction._loadAsset, ['assets/public/default-files.zip']);
final bytes = await loadAssets('assets/public/default-files.zip');
// 将不存在的文件解压到指定路径
FS.unzip(bytes: bytes, target: site.appDir, cover: false);
}
Expand Down
21 changes: 16 additions & 5 deletions lib/library/worker/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:glidea/interfaces/types.dart';
import 'package:glidea/models/application.dart';
import 'package:glidea/models/setting.dart';
import 'package:glidea/models/worker.dart';
import 'package:logger/logger.dart' show Level;
import 'package:package_info_plus/package_info_plus.dart' show PackageInfo;
import 'package:path_provider/path_provider.dart' show getApplicationDocumentsDirectory, getApplicationSupportDirectory;
import 'package:shelf/shelf_io.dart' as shelf_io show serve;
Expand Down Expand Up @@ -46,6 +47,9 @@ abstract class BaseWorker {

Map<int, Completer> get tasks => _tasks;

/// 记录自身需要被调用的函数
final Map<Symbol, Function> _invokes = {};

/// 初始化数据
@protected
Future<void> onInit() async {}
Expand Down Expand Up @@ -100,7 +104,11 @@ abstract class BaseWorker {

/// 调用自身的函数发送对应的数据
@protected
Future<dynamic> invoke(Invocation invocation) async {}
Future<dynamic> invoke(Invocation invocation) async {
final fun = _invokes[invocation.memberName];
if (fun == null) return;
return Function.apply(fun, invocation.positionalArguments, invocation.namedArguments);
}
}

/// 管理后台进程
Expand Down Expand Up @@ -132,10 +140,10 @@ base class BackgroundWorker extends BaseWorker {
isolate = await Isolate.spawn((params) async {
// 初始化
BackgroundIsolateBinaryMessenger.ensureInitialized(params.token);
await Log.initialized();
await Log.initialized(background: true);
JsonHelp.initialized();
// 创建 [_BackgroundProcess]
BackgroundProcess(send: params.send, id: initTaskId);
BackgroundProcess.instance = BackgroundProcess(send: params.send, id: initTaskId);
}, initData);
// 返回任务
return initTask.future;
Expand All @@ -146,7 +154,7 @@ base class BackgroundWorker extends BaseWorker {
final class Background extends BackgroundWorker with BackgroundAction {
static Background? _instance;

/// 后台实例
/// 用于控制后台实例
static Background get instance {
if (_instance == null) {
throw StateError('The Background.instance value is invalid ');
Expand All @@ -171,6 +179,9 @@ base class WorkerProcess extends BaseWorker {
}

/// 后台数据处理进程
final class BackgroundProcess extends WorkerProcess with RemoteBack, DataBack {
final class BackgroundProcess extends WorkerProcess with ActionBack, RemoteBack, DataBack {
BackgroundProcess({required super.send, super.receive, super.id});

/// 先前台发送信息的实例
static BackgroundProcess? instance;
}

0 comments on commit 8a30d60

Please sign in to comment.