From a12d8da6d1489098b5459da333bd71563ae6a404 Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 6 Sep 2022 18:59:41 -0400 Subject: [PATCH] checkpoint --- veilid-cli/src/command_processor.rs | 13 ++++++++++--- veilid-core/src/api_tracing_layer.rs | 6 ++++++ veilid-core/src/veilid_api/mod.rs | 1 + veilid-flutter/example/lib/main.dart | 1 + veilid-flutter/lib/veilid.dart | 20 +++++++++++++------- veilid-flutter/lib/veilid_ffi.dart | 26 ++++++++++++++++++++++++++ veilid-flutter/lib/veilid_js.dart | 10 ++++++++++ veilid-flutter/rust/src/dart_ffi.rs | 18 ++++++++++++++++++ veilid-wasm/src/lib.rs | 18 ++++++++++++++++++ 9 files changed, 103 insertions(+), 10 deletions(-) diff --git a/veilid-cli/src/command_processor.rs b/veilid-cli/src/command_processor.rs index 0285630f..5b432f27 100644 --- a/veilid-cli/src/command_processor.rs +++ b/veilid-cli/src/command_processor.rs @@ -332,9 +332,16 @@ change_log_level - change the log level for a tracing layer } pub fn update_log(&mut self, log: veilid_core::VeilidStateLog) { - self.inner() - .ui - .add_node_event(format!("{}: {}", log.log_level, log.message)); + self.inner().ui.add_node_event(format!( + "{}: {}{}", + log.log_level, + log.message, + if let Some(bt) = log.backtrace { + format!("\nBacktrace:\n{}", bt); + } else { + "".to_owned() + } + )); } pub fn update_shutdown(&mut self) { diff --git a/veilid-core/src/api_tracing_layer.rs b/veilid-core/src/api_tracing_layer.rs index 13fe64bb..5d3db4a5 100644 --- a/veilid-core/src/api_tracing_layer.rs +++ b/veilid-core/src/api_tracing_layer.rs @@ -96,6 +96,12 @@ impl registry::LookupSpan<'a>> Layer for ApiTracingLa let message = format!("{} {}", origin, recorder); + let backtrace = if log_level <= VeilidLogLevel::Error { + Some(std::backtrace::Backtrace) + } else { + None + }; + (inner.update_callback)(VeilidUpdate::Log(VeilidStateLog { log_level, message })) } } diff --git a/veilid-core/src/veilid_api/mod.rs b/veilid-core/src/veilid_api/mod.rs index ed94fea4..960df4c0 100644 --- a/veilid-core/src/veilid_api/mod.rs +++ b/veilid-core/src/veilid_api/mod.rs @@ -219,6 +219,7 @@ impl fmt::Display for VeilidLogLevel { pub struct VeilidStateLog { pub log_level: VeilidLogLevel, pub message: String, + pub backtrace: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] diff --git a/veilid-flutter/example/lib/main.dart b/veilid-flutter/example/lib/main.dart index 951d7b37..9a1a1ac1 100644 --- a/veilid-flutter/example/lib/main.dart +++ b/veilid-flutter/example/lib/main.dart @@ -223,6 +223,7 @@ class _MyAppState extends State with UiLoggy { _updateStream = updateStream; _updateProcessor = processUpdates(); }); + await Veilid.instance.attach(); }, child: const Text('Startup'), ), diff --git a/veilid-flutter/lib/veilid.dart b/veilid-flutter/lib/veilid.dart index 23fb4a4d..a9bfc6ab 100644 --- a/veilid-flutter/lib/veilid.dart +++ b/veilid-flutter/lib/veilid.dart @@ -1234,7 +1234,8 @@ abstract class VeilidUpdate { { return VeilidUpdateLog( logLevel: veilidLogLevelFromJson(json["log_level"]), - message: json["message"]); + message: json["message"], + backtrace: json["backtrace"]); } case "Attachment": { @@ -1258,10 +1259,12 @@ abstract class VeilidUpdate { class VeilidUpdateLog implements VeilidUpdate { final VeilidLogLevel logLevel; final String message; + final String? backtrace; // VeilidUpdateLog({ required this.logLevel, required this.message, + required this.backtrace, }); @override @@ -1270,6 +1273,7 @@ class VeilidUpdateLog implements VeilidUpdate { 'kind': "Log", 'log_level': logLevel.json, 'message': message, + 'backtrace': backtrace }; } } @@ -1323,8 +1327,8 @@ class VeilidStateAttachment { class VeilidStateNetwork { final bool started; - final int bpsDown; - final int bpsUp; + final BigInt bpsDown; + final BigInt bpsUp; final List peers; VeilidStateNetwork( @@ -1335,15 +1339,15 @@ class VeilidStateNetwork { VeilidStateNetwork.fromJson(Map json) : started = json['started'], - bpsDown = json['bps_down'], - bpsUp = json['bps_up'], + bpsDown = BigInt.parse(json['bps_down']), + bpsUp = BigInt.parse(json['bps_up']), peers = json['peers'].map((j) => PeerTableData.fromJson(j)).toList(); Map get json { return { 'started': started, - 'bps_down': bpsDown, - 'bps_up': bpsUp, + 'bps_down': bpsDown.toString(), + 'bps_up': bpsUp.toString(), 'peers': peers.map((p) => p.json).toList(), }; } @@ -1563,6 +1567,8 @@ abstract class Veilid { void changeLogLevel(String layer, VeilidConfigLogLevel logLevel); Stream startupVeilidCore(VeilidConfig config); Future getVeilidState(); + Future attach(); + Future detach(); Future shutdownVeilidCore(); Future debug(String command); String veilidVersionString(); diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index edd34cee..55e4f5c0 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -41,6 +41,12 @@ typedef _StartupVeilidCoreDart = void Function(int, Pointer); // fn get_veilid_state(port: i64) typedef _GetVeilidStateC = Void Function(Int64); typedef _GetVeilidStateDart = void Function(int); +// fn attach(port: i64) +typedef _AttachC = Void Function(Int64); +typedef _AttachDart = void Function(int); +// fn detach(port: i64) +typedef _DetachC = Void Function(Int64); +typedef _DetachDart = void Function(int); // fn debug(port: i64, log_level: FfiStr) typedef _DebugC = Void Function(Int64, Pointer); typedef _DebugDart = void Function(int, Pointer); @@ -249,6 +255,8 @@ class VeilidFFI implements Veilid { final _ChangeLogLevelDart _changeLogLevel; final _StartupVeilidCoreDart _startupVeilidCore; final _GetVeilidStateDart _getVeilidState; + final _AttachDart _attach; + final _DetachDart _detach; final _ShutdownVeilidCoreDart _shutdownVeilidCore; final _DebugDart _debug; final _VeilidVersionStringDart _veilidVersionString; @@ -269,6 +277,8 @@ class VeilidFFI implements Veilid { _getVeilidState = dylib.lookupFunction<_GetVeilidStateC, _GetVeilidStateDart>( 'get_veilid_state'), + _attach = dylib.lookupFunction<_AttachC, _AttachDart>('attach'), + _detach = dylib.lookupFunction<_DetachC, _DetachDart>('detach'), _shutdownVeilidCore = dylib.lookupFunction<_ShutdownVeilidCoreC, _ShutdownVeilidCoreDart>( 'shutdown_veilid_core'), @@ -327,6 +337,22 @@ class VeilidFFI implements Veilid { return processFutureJson(VeilidState.fromJson, recvPort.first); } + @override + Future attach() async { + final recvPort = ReceivePort("attach"); + final sendPort = recvPort.sendPort; + _attach(sendPort.nativePort); + return processFutureVoid(recvPort.first); + } + + @override + Future detach() async { + final recvPort = ReceivePort("detach"); + final sendPort = recvPort.sendPort; + _detach(sendPort.nativePort); + return processFutureVoid(recvPort.first); + } + @override Future shutdownVeilidCore() async { final recvPort = ReceivePort("shutdown_veilid_core"); diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index c65cecb3..7958eb33 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -60,6 +60,16 @@ class VeilidJS implements Veilid { js_util.callMethod(wasm, "get_veilid_state", [])))); } + @override + Future attach() async { + return _wrapApiPromise(js_util.callMethod(wasm, "attach", [])); + } + + @override + Future detach() async { + return _wrapApiPromise(js_util.callMethod(wasm, "detach", [])); + } + @override Future shutdownVeilidCore() { return _wrapApiPromise( diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index 16a554af..e0430c93 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -302,6 +302,24 @@ pub extern "C" fn get_veilid_state(port: i64) { }); } +#[no_mangle] +pub extern "C" fn attach(port: i64) { + DartIsolateWrapper::new(port).spawn_result_json(async move { + let veilid_api = get_veilid_api().await?; + veilid_api.attach().await?; + APIRESULT_VOID + }); +} + +#[no_mangle] +pub extern "C" fn detach(port: i64) { + DartIsolateWrapper::new(port).spawn_result_json(async move { + let veilid_api = get_veilid_api().await?; + veilid_api.detach().await?; + APIRESULT_VOID + }); +} + #[no_mangle] #[instrument] pub extern "C" fn shutdown_veilid_core(port: i64) { diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 363f9d70..9f0e1b4b 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -232,6 +232,24 @@ pub fn get_veilid_state() -> Promise { }) } +#[wasm_bindgen()] +pub fn attach() -> Promise { + wrap_api_future(async move { + let veilid_api = get_veilid_api()?; + veilid_api.attach().await?; + APIRESULT_UNDEFINED + }) +} + +#[wasm_bindgen()] +pub fn detach() -> Promise { + wrap_api_future(async move { + let veilid_api = get_veilid_api()?; + veilid_api.detach().await?; + APIRESULT_UNDEFINED + }) +} + #[wasm_bindgen()] pub fn shutdown_veilid_core() -> Promise { wrap_api_future(async move {