platform conditional code refactoring
This commit is contained in:
parent
b2d22069b2
commit
49f52b224f
4 changed files with 36 additions and 39 deletions
|
|
@ -85,10 +85,11 @@ class Log {
|
|||
static String? path;
|
||||
int index = 0;
|
||||
StringBuffer buffer = StringBuffer();
|
||||
final LogWriter _writer = LogWriter();
|
||||
Log._privateConstructor() {
|
||||
initializeLog();
|
||||
}
|
||||
static late Log _instance = Log._privateConstructor();
|
||||
static Log _instance = Log._privateConstructor();
|
||||
static Log get to => _instance;
|
||||
|
||||
final List<LogItem> logs = [];
|
||||
|
|
@ -108,7 +109,7 @@ class Log {
|
|||
}
|
||||
}
|
||||
if (path != null && path!.isNotEmpty) {
|
||||
startLogWriter(path!, _onWriterEvent);
|
||||
_writer.start(path!, _onWriterEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -127,7 +128,7 @@ class Log {
|
|||
buffer.writeln(logs[i].toString());
|
||||
}
|
||||
index = logs.length;
|
||||
writeLogWriter(buffer.toString());
|
||||
_writer.write(buffer.toString());
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -148,6 +149,6 @@ class Log {
|
|||
|
||||
void end() {
|
||||
write();
|
||||
endLogWriter();
|
||||
_writer.end();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:dart_framework/platform/platform_base.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'package:dart_framework/platform/isolate_manager.dart';
|
||||
import 'package:dart_framework/utils/string_util.dart';
|
||||
import 'package:dart_framework/data/isolate_data.dart';
|
||||
|
||||
class LogWriterIO extends IsolateBase {
|
||||
class LogWriterIO extends IsolateBase implements LogWriter {
|
||||
late int _id;
|
||||
late File _file;
|
||||
@override
|
||||
void onReady(Object? initData) async {
|
||||
|
|
@ -18,7 +20,7 @@ class LogWriterIO extends IsolateBase {
|
|||
await _file.create();
|
||||
print('[LogWriterIO] onReady - path: ${_file.path}');
|
||||
send(IsoMessege('ready', message: _file.path));
|
||||
addEventListener('write', write);
|
||||
addEventListener('write', onWrite);
|
||||
requester();
|
||||
}
|
||||
}
|
||||
|
|
@ -31,26 +33,29 @@ class LogWriterIO extends IsolateBase {
|
|||
}
|
||||
}
|
||||
|
||||
void write(Object? message) {
|
||||
void onWrite(Object? message) {
|
||||
// print('write in file: $message');
|
||||
_file.writeAsStringSync(message as String, mode: FileMode.append);
|
||||
}
|
||||
|
||||
@override
|
||||
void start(String path, Function(IsoMessege) isoMessageListener) {
|
||||
IsolateManager.create<LogWriterIO>(LogWriterIO(), path, (event, data) {
|
||||
if (event == IsolateEvent.ready) {
|
||||
_id = data as int;
|
||||
}
|
||||
}, isoMessageListener);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(String log) {
|
||||
IsolateManager.send(_id, IsoMessege('write', message: log));
|
||||
}
|
||||
|
||||
@override
|
||||
void end() {
|
||||
IsolateManager.exit(_id);
|
||||
}
|
||||
}
|
||||
|
||||
int _id = 0;
|
||||
|
||||
void startLog(String path, Function(IsoMessege) listener) {
|
||||
IsolateManager.create<LogWriterIO>(LogWriterIO(), path, (event, data) {
|
||||
if (event == IsolateEvent.ready) {
|
||||
_id = data as int;
|
||||
}
|
||||
}, listener);
|
||||
}
|
||||
|
||||
void writeLog(String log) {
|
||||
IsolateManager.send(_id, IsoMessege('write', message: log));
|
||||
}
|
||||
|
||||
void endLog() {
|
||||
IsolateManager.exit(_id);
|
||||
}
|
||||
LogWriter getLogWriter() => LogWriterIO();
|
||||
|
|
|
|||
|
|
@ -3,14 +3,10 @@ import 'package:dart_framework/platform/platform_stub.dart'
|
|||
if (dart.library.io) 'package:dart_framework/log/log_writer_io.dart'
|
||||
if (dart.library.html) 'package:dart_framework/log/log_writer_web.dart';
|
||||
|
||||
void startLogWriter(String path, Function(IsoMessege) listener) {
|
||||
startLog(path, listener);
|
||||
}
|
||||
abstract class LogWriter {
|
||||
factory LogWriter() => getLogWriter();
|
||||
|
||||
void writeLogWriter(String log) {
|
||||
writeLog(log);
|
||||
}
|
||||
|
||||
void endLogWriter() {
|
||||
endLog();
|
||||
void start(String path, Function(IsoMessege) isoMessageListener);
|
||||
void write(String log);
|
||||
void end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,3 @@
|
|||
import 'package:dart_framework/platform/platform_base.dart';
|
||||
import 'package:dart_framework/data/isolate_data.dart';
|
||||
|
||||
void startLog(String path, Function(IsoMessege) listener) {}
|
||||
|
||||
void writeLog(String log) {}
|
||||
|
||||
void endLog() {}
|
||||
LogWriter getLogWriter() => throw UnsupportedError('Cannot create a instance');
|
||||
|
|
|
|||
Loading…
Reference in a new issue