2022-01-16 16:19:01 +00:00
|
|
|
import 'dart:async';
|
2022-03-09 03:32:12 +00:00
|
|
|
import 'dart:typed_data';
|
2022-06-16 01:51:38 +00:00
|
|
|
import 'dart:convert';
|
2022-01-16 16:19:01 +00:00
|
|
|
|
2022-01-29 18:23:10 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-06-16 01:51:38 +00:00
|
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
2022-01-16 16:19:01 +00:00
|
|
|
import 'package:veilid/veilid.dart';
|
2022-03-04 01:45:39 +00:00
|
|
|
import 'package:flutter_loggy/flutter_loggy.dart';
|
|
|
|
import 'package:loggy/loggy.dart';
|
2022-02-15 18:40:17 +00:00
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
import 'config.dart';
|
|
|
|
|
|
|
|
// Loggy tools
|
|
|
|
const LogLevel traceLevel = LogLevel('trace', 1);
|
|
|
|
|
2022-03-10 14:51:53 +00:00
|
|
|
class ConsolePrinter extends LoggyPrinter {
|
|
|
|
ConsolePrinter(this.childPrinter) : super();
|
|
|
|
|
|
|
|
final LoggyPrinter childPrinter;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onLog(LogRecord record) {
|
|
|
|
debugPrint(record.toString());
|
|
|
|
childPrinter.onLog(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
extension TraceLoggy on Loggy {
|
|
|
|
void trace(dynamic message, [Object? error, StackTrace? stackTrace]) =>
|
|
|
|
log(traceLevel, message, error, stackTrace);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogOptions getLogOptions(LogLevel? level) {
|
|
|
|
return LogOptions(
|
|
|
|
level ?? LogLevel.all,
|
|
|
|
stackTraceLevel: LogLevel.error,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-10 21:07:10 +00:00
|
|
|
VeilidConfigLogLevel convertToVeilidConfigLogLevel(LogLevel? level) {
|
|
|
|
if (level == null) {
|
|
|
|
return VeilidConfigLogLevel.off;
|
|
|
|
}
|
|
|
|
switch (level) {
|
|
|
|
case LogLevel.error:
|
|
|
|
return VeilidConfigLogLevel.error;
|
|
|
|
case LogLevel.warning:
|
|
|
|
return VeilidConfigLogLevel.warn;
|
|
|
|
case LogLevel.info:
|
|
|
|
return VeilidConfigLogLevel.info;
|
|
|
|
case LogLevel.debug:
|
|
|
|
return VeilidConfigLogLevel.debug;
|
|
|
|
case traceLevel:
|
|
|
|
return VeilidConfigLogLevel.trace;
|
|
|
|
}
|
|
|
|
return VeilidConfigLogLevel.off;
|
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
void setRootLogLevel(LogLevel? level) {
|
|
|
|
Loggy('').level = getLogOptions(level);
|
2022-07-01 16:13:52 +00:00
|
|
|
Veilid.instance.changeLogLevel("all", convertToVeilidConfigLogLevel(level));
|
2022-03-04 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initLoggy() {
|
|
|
|
Loggy.initLoggy(
|
2022-03-11 12:35:41 +00:00
|
|
|
logPrinter: StreamPrinter(ConsolePrinter(
|
2022-03-04 01:45:39 +00:00
|
|
|
const PrettyDeveloperPrinter(),
|
2022-03-11 12:35:41 +00:00
|
|
|
)),
|
2022-03-04 01:45:39 +00:00
|
|
|
logOptions: getLogOptions(null),
|
|
|
|
);
|
2022-02-15 18:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entrypoint
|
2022-01-16 16:19:01 +00:00
|
|
|
void main() {
|
2022-03-04 01:45:39 +00:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
initLoggy();
|
2022-06-16 01:51:38 +00:00
|
|
|
if (kIsWeb) {
|
|
|
|
var platformConfig = VeilidWASMConfig(
|
|
|
|
logging: VeilidWASMConfigLogging(
|
|
|
|
performance: VeilidWASMConfigLoggingPerformance(
|
|
|
|
enabled: true,
|
2022-07-01 16:13:52 +00:00
|
|
|
level: VeilidConfigLogLevel.debug,
|
2022-06-16 01:51:38 +00:00
|
|
|
logsInTimings: true,
|
2022-07-01 16:13:52 +00:00
|
|
|
logsInConsole: true),
|
|
|
|
api: VeilidWASMConfigLoggingApi(
|
|
|
|
enabled: true, level: VeilidConfigLogLevel.info)));
|
2022-07-01 20:20:43 +00:00
|
|
|
Veilid.instance.initializeVeilidCore(platformConfig.json);
|
2022-06-16 01:51:38 +00:00
|
|
|
} else {
|
|
|
|
var platformConfig = VeilidFFIConfig(
|
|
|
|
logging: VeilidFFIConfigLogging(
|
|
|
|
terminal: VeilidFFIConfigLoggingTerminal(
|
|
|
|
enabled: false,
|
2022-07-01 16:13:52 +00:00
|
|
|
level: VeilidConfigLogLevel.debug,
|
2022-06-16 01:51:38 +00:00
|
|
|
),
|
|
|
|
otlp: VeilidFFIConfigLoggingOtlp(
|
|
|
|
enabled: false,
|
2022-07-01 16:13:52 +00:00
|
|
|
level: VeilidConfigLogLevel.trace,
|
2022-06-16 01:51:38 +00:00
|
|
|
grpcEndpoint: "localhost:4317",
|
2022-07-01 16:13:52 +00:00
|
|
|
serviceName: "VeilidExample"),
|
|
|
|
api: VeilidFFIConfigLoggingApi(
|
|
|
|
enabled: true, level: VeilidConfigLogLevel.info)));
|
2022-07-01 20:20:43 +00:00
|
|
|
Veilid.instance.initializeVeilidCore(platformConfig.json);
|
2022-06-16 01:51:38 +00:00
|
|
|
}
|
2022-03-04 01:45:39 +00:00
|
|
|
|
|
|
|
runApp(MaterialApp(
|
|
|
|
title: 'Veilid Plugin Demo',
|
|
|
|
theme: ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
|
|
|
),
|
|
|
|
home: const MyApp()));
|
2022-01-16 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:40:17 +00:00
|
|
|
// Main App
|
2022-01-16 16:19:01 +00:00
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyApp> createState() => _MyAppState();
|
|
|
|
}
|
|
|
|
|
2022-03-04 01:45:39 +00:00
|
|
|
class _MyAppState extends State<MyApp> with UiLoggy {
|
2022-01-29 18:23:10 +00:00
|
|
|
String _veilidVersion = 'Unknown';
|
2022-03-04 01:45:39 +00:00
|
|
|
Stream<VeilidUpdate>? _updateStream;
|
2022-03-09 03:32:12 +00:00
|
|
|
Future<void>? _updateProcessor;
|
2022-01-16 16:19:01 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2022-06-16 01:51:38 +00:00
|
|
|
setRootLogLevel(LogLevel.info);
|
2022-01-16 16:19:01 +00:00
|
|
|
initPlatformState();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Platform messages are asynchronous, so we initialize in an async method.
|
|
|
|
Future<void> initPlatformState() async {
|
2022-01-29 18:23:10 +00:00
|
|
|
String veilidVersion;
|
2022-01-16 16:19:01 +00:00
|
|
|
// Platform messages may fail, so we use a try/catch PlatformException.
|
|
|
|
// We also handle the message potentially returning null.
|
|
|
|
try {
|
2022-03-17 14:31:10 +00:00
|
|
|
veilidVersion = Veilid.instance.veilidVersionString();
|
2022-03-11 12:35:41 +00:00
|
|
|
} on Exception {
|
2022-01-29 18:23:10 +00:00
|
|
|
veilidVersion = 'Failed to get veilid version.';
|
2022-01-16 16:19:01 +00:00
|
|
|
}
|
2022-03-11 12:35:41 +00:00
|
|
|
|
|
|
|
// In case of hot restart shut down first
|
|
|
|
try {
|
|
|
|
await Veilid.instance.shutdownVeilidCore();
|
|
|
|
} on Exception {
|
|
|
|
//
|
|
|
|
}
|
2022-01-16 16:19:01 +00:00
|
|
|
|
|
|
|
// If the widget was removed from the tree while the asynchronous platform
|
|
|
|
// message was in flight, we want to discard the reply rather than calling
|
|
|
|
// setState to update our non-existent appearance.
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
setState(() {
|
2022-01-29 18:23:10 +00:00
|
|
|
_veilidVersion = veilidVersion;
|
2022-01-16 16:19:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-09 03:32:12 +00:00
|
|
|
Future<void> processUpdateLog(VeilidUpdateLog update) async {
|
|
|
|
switch (update.logLevel) {
|
|
|
|
case VeilidLogLevel.error:
|
|
|
|
loggy.error(update.message);
|
|
|
|
break;
|
|
|
|
case VeilidLogLevel.warn:
|
|
|
|
loggy.warning(update.message);
|
|
|
|
break;
|
|
|
|
case VeilidLogLevel.info:
|
|
|
|
loggy.info(update.message);
|
|
|
|
break;
|
|
|
|
case VeilidLogLevel.debug:
|
|
|
|
loggy.debug(update.message);
|
|
|
|
break;
|
|
|
|
case VeilidLogLevel.trace:
|
|
|
|
loggy.trace(update.message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> processUpdates() async {
|
|
|
|
var stream = _updateStream;
|
|
|
|
if (stream != null) {
|
|
|
|
await for (final update in stream) {
|
|
|
|
if (update is VeilidUpdateLog) {
|
|
|
|
await processUpdateLog(update);
|
|
|
|
} else {
|
2022-09-06 20:49:43 +00:00
|
|
|
loggy.trace("Update: ${update.json}");
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 16:19:01 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-03-04 01:45:39 +00:00
|
|
|
final ButtonStyle buttonStyle =
|
|
|
|
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
|
|
|
|
|
|
|
|
return Scaffold(
|
2022-01-16 16:19:01 +00:00
|
|
|
appBar: AppBar(
|
2022-02-15 18:40:17 +00:00
|
|
|
title: Text('Veilid Plugin Version $_veilidVersion'),
|
2022-01-16 16:19:01 +00:00
|
|
|
),
|
2022-03-04 01:45:39 +00:00
|
|
|
body: Column(children: [
|
|
|
|
Expanded(
|
|
|
|
child: Container(
|
|
|
|
color: ThemeData.dark().scaffoldBackgroundColor,
|
|
|
|
height: MediaQuery.of(context).size.height * 0.4,
|
|
|
|
child: LoggyStreamWidget(logLevel: loggy.level.logLevel),
|
|
|
|
)),
|
|
|
|
Container(
|
|
|
|
padding: const EdgeInsets.fromLTRB(8, 8, 8, 12),
|
|
|
|
child: Row(children: [
|
|
|
|
ElevatedButton(
|
|
|
|
style: buttonStyle,
|
2022-03-09 03:32:12 +00:00
|
|
|
onPressed: _updateStream != null
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
var updateStream = Veilid.instance.startupVeilidCore(
|
|
|
|
await getDefaultVeilidConfig());
|
|
|
|
setState(() {
|
|
|
|
_updateStream = updateStream;
|
|
|
|
_updateProcessor = processUpdates();
|
|
|
|
});
|
|
|
|
},
|
2022-03-04 01:45:39 +00:00
|
|
|
child: const Text('Startup'),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
style: buttonStyle,
|
2022-03-09 03:32:12 +00:00
|
|
|
onPressed: _updateStream == null
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
await Veilid.instance.shutdownVeilidCore();
|
|
|
|
if (_updateProcessor != null) {
|
|
|
|
await _updateProcessor;
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
_updateProcessor = null;
|
|
|
|
_updateStream = null;
|
|
|
|
});
|
|
|
|
},
|
2022-03-04 01:45:39 +00:00
|
|
|
child: const Text('Shutdown'),
|
|
|
|
),
|
|
|
|
])),
|
|
|
|
Row(children: [
|
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'Debug Command'),
|
|
|
|
textInputAction: TextInputAction.send,
|
|
|
|
onSubmitted: (String v) async {
|
2022-08-25 00:59:09 +00:00
|
|
|
loggy.info(await Veilid.instance.debug(v));
|
2022-03-04 01:45:39 +00:00
|
|
|
})),
|
|
|
|
DropdownButton<LogLevel>(
|
|
|
|
value: loggy.level.logLevel,
|
|
|
|
onChanged: (LogLevel? newLevel) {
|
|
|
|
setState(() {
|
|
|
|
setRootLogLevel(newLevel);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
items: const [
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: LogLevel.error, child: Text("Error")),
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: LogLevel.warning, child: Text("Warning")),
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: LogLevel.info, child: Text("Info")),
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: LogLevel.debug, child: Text("Debug")),
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: traceLevel, child: Text("Trace")),
|
|
|
|
DropdownMenuItem<LogLevel>(
|
|
|
|
value: LogLevel.all, child: Text("All")),
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
]));
|
2022-01-16 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|