process enable log

This commit is contained in:
Toki 2024-12-26 23:29:43 +09:00
parent 50215182ef
commit 671faca019
2 changed files with 43 additions and 33 deletions

View file

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:dart_framework/encrypt/encrypt.dart';
import 'package:dart_framework/log/log.dart';
import 'package:dart_framework/platform/process.dart';
import 'package:dart_framework/subtitle/parser_smi.dart';
import 'package:dart_framework/subtitle/parser_srt.dart';
@ -11,7 +12,7 @@ import 'package:dart_framework/charset.dart';
void main() async {
Application('dartframework', 'com.toki-labs.dartframework',() async {
// isolate.test();
isolate.test();
// var md5 = generateMd5('test');
// print('$md5 / ${md5.length}');
// var encrypted = encrypt('test', 'this is test');
@ -19,11 +20,7 @@ void main() async {
// var raw = decrypt('test', encrypted);
// print(raw);
var processes = await findProcess('MacOS/MetaSlap ');
for(var process in processes) {
Process.killPid(process.pid);
}
}, (error, stack) {
print(error);
print(stack);
@ -59,4 +56,4 @@ void testParse() {
index++;
}
}
}
}

View file

@ -5,9 +5,11 @@ import 'dart:io';
import 'dart:convert';
import 'dart:math';
import 'package:dart_framework/charset/euc_kr.dart';
import 'package:dart_framework/log/log.dart';
import 'package:dart_framework/utils/system_util.dart';
typedef LogHandler = Function(String, LogType);
class ProcessData {
late Process process;
late File file;
@ -16,12 +18,15 @@ class ProcessData {
Function(ProcessData)? _endListener;
late StringBuffer stdout;
late StringBuffer stderr;
bool _enableStdout;
bool _enableStderr;
final bool _enableStdout;
final bool _enableStderr;
LogHandler? _logHandler;
late Converter<List<int>, String> _decoder = utf8.decoder;
// ignore: no_leading_underscores_for_local_identifiers
ProcessData(this._enableStdout, this._enableStderr);
ProcessData(this._enableStdout, this._enableStderr, {LogHandler? logHandler}) {
_logHandler = logHandler;
}
Function(ProcessData)? get endListener {
return _endListener;
@ -62,19 +67,27 @@ class ProcessData {
process.stdout.transform(_decoder).listen((event) {
stdout.writeln(event);
if (_enableStdout) {
print(event);
log(event, LogType.verbose);
}
});
process.stderr.transform(_decoder).listen((event) {
stderr.writeln(event);
if (_enableStderr) {
print('======================= $event');
log('[Error] $event', LogType.error);
}
});
isRunning = true;
checkExit();
}
void log(String message, LogType logType) {
if(_logHandler == null) {
print(message);
} else {
_logHandler!(message, logType);
}
}
void addEndListener(Function(ProcessData) listener) {
_endListener = listener;
}
@ -112,24 +125,24 @@ class ProcessData {
class ProcessExecutor {
static Future<ProcessData> startExe(String exe, List<String> args,
{String? workspace, bool? printStdout, bool? printStderr}) async {
{String? workspace, bool? printStdout, bool? printStderr, LogHandler? logHandler}) async {
var arg = '$exe ${args.join(' ')}';
var printStdout_ = printStdout ?? true;
var printStderr_ = printStderr ?? true;
var processData = ProcessData(printStdout_, printStderr_);
var processData = ProcessData(printStdout_, printStderr_, logHandler: logHandler);
if (Platform.isMacOS || Platform.isWindows) {
workspace = workspace ?? Directory.systemTemp.path;
if(printStdout_) {
print('Execute Shell: $arg');
logHandler??('Execute Shell: $arg', LogType.verbose);
}
try {
processData.processValue =
await Process.start(exe, args, workingDirectory: workspace);
} on Exception catch (e) {
print(e);
logHandler??(e, LogType.error);
}
} else {
print('This platform not support process executor.');
logHandler??('This platform not support process executor.');
}
return dataFutrue(processData);
}
@ -138,12 +151,12 @@ class ProcessExecutor {
{Converter<List<int>, String>? decoder,
String? workspace,
bool? printStdout,
bool? printStderr}) async {
bool? printStderr, LogHandler? logHandler}) async {
var c = Completer<ProcessData>();
try {
var printStdout_ = printStdout ?? true;
var printStderr_ = printStderr ?? true;
var processData = ProcessData(printStdout_, printStderr_);
var processData = ProcessData(printStdout_, printStderr_, logHandler: logHandler);
if (decoder != null) {
processData.decoder = decoder;
}
@ -152,7 +165,7 @@ class ProcessExecutor {
var file = File('$workspace/$tempFileName');
processData.file = file;
if(printStdout_) {
print('Execute Shell: ${file.path} \n ${shell.toString()}');
logHandler??('Execute Shell: ${file.path} \n ${shell.toString()}', LogType.verbose);
}
if (await file.exists()) {
await file.delete();
@ -164,14 +177,14 @@ class ProcessExecutor {
processData.processValue = await Process.start(shellExe, [file.path],
workingDirectory: workspace, runInShell: false);
} on Exception catch (e) {
print(e);
logHandler??(e, LogType.error);
}
} else if (Platform.isWindows) {
workspace = workspace ?? Directory.systemTemp.path;
var file = File('$workspace/$tempFileName');
processData.file = file;
if(printStdout_) {
print('Execute Shell: ${file.path} \n ${shell.toString()}');
logHandler??('Execute Shell: ${file.path} \n ${shell.toString()}', LogType.verbose);
}
if (await file.exists()) {
await file.delete();
@ -183,27 +196,27 @@ class ProcessExecutor {
'cmd', ['/C', file.path],
workingDirectory: workspace, runInShell: false);
} on Exception catch (e) {
print(e);
logHandler??(e, LogType.error);
}
} else {
print('This platform not support process executor.');
logHandler??('This platform not support process executor.', LogType.error);
}
c.complete(processData);
await processData.waitForExit();
} on Exception catch (e) {
print('General Exception in ProcessExecutor.start: $e');
logHandler??('General Exception in ProcessExecutor.start: $e', LogType.error);
}
return c.future;
}
static Future<ProcessResult> run(StringBuffer shell,
{String? workspace, bool printStdout = true}) async {
{String? workspace, bool printStdout = true, LogHandler? logHandler}) async {
var c = Completer<ProcessResult>();
workspace = workspace ?? Directory.systemTemp.path;
var file = File('$workspace/$tempFileName');
if(printStdout) {
print('---------------------------------------------------');
print('Shell Path: ${file.path} \nExecute Shell: ${shell.toString()}');
logHandler??('---------------------------------------------------', LogType.verbose);
logHandler??('Shell Path: ${file.path} \nExecute Shell: ${shell.toString()}', LogType.verbose);
}
ProcessResult? processResult;
if (Platform.isMacOS || Platform.isLinux) {
@ -216,12 +229,12 @@ class ProcessExecutor {
processResult = result;
});
if (printStdout) {
print(
logHandler??(
// ignore: prefer_interpolation_to_compose_strings
'[ProcessExecutor] Exit Code: ${processResult!.exitCode}\nResult: ${processResult!.stdout}\nError: ${processResult!.stderr}');
'[ProcessExecutor] Exit Code: ${processResult!.exitCode}\nResult: ${processResult!.stdout}\nError: ${processResult!.stderr}', LogType.verbose);
}
} on Exception catch (e) {
print(e);
logHandler??(e, LogType.error);
} finally {
file.deleteSync();
}
@ -245,7 +258,7 @@ class ProcessExecutor {
file.deleteSync();
c.complete(processResult);
} else {
print('This platform not support process executor.');
logHandler??('This platform not support process executor.', LogType.error);
c.complete(null);
}
return c.future;