Skip to content

Commit

Permalink
Proxy log messages to dart handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lbirkert committed Aug 22, 2024
1 parent 8c1d335 commit af0c8a5
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 16 deletions.
10 changes: 7 additions & 3 deletions qb-mobile/lib/service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:ui';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
Expand All @@ -6,6 +7,7 @@ import 'package:process/process.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:qb_mobile/src/rust/api/daemon.dart';
import 'package:qb_mobile/src/rust/api/log.dart';
import 'package:qb_mobile/src/rust/frb_generated.dart';

const ProcessManager processManager = LocalProcessManager();
Expand Down Expand Up @@ -49,6 +51,8 @@ Future<bool> onIosBackground(ServiceInstance service) async {
@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
await RustLib.init();
initLog().listen((msg) => print(utf8.decode(msg)));

final dir = await getApplicationDocumentsDirectory();
final daemon = await DaemonWrapper.init(path: dir.path);

Expand All @@ -61,9 +65,9 @@ void onStart(ServiceInstance service) async {
print("background service started!");
});

Timer.periodic(const Duration(seconds: 1), (timer) {
print("service is successfully running ${DateTime.now().second}");
});
//Timer.periodic(const Duration(seconds: 1), (timer) {
// print("service is successfully running ${DateTime.now().second}");
//});

while(true) {
await daemon.process();
Expand Down
12 changes: 12 additions & 0 deletions qb-mobile/lib/src/rust/api/log.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.3.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// These types are ignored because they are not used by any `pub` functions: `LogWriter`
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `clone`, `flush`, `make_writer_for`, `make_writer`, `write`

Stream<Uint8List> initLog() => RustLib.instance.api.crateApiLogInitLog();
78 changes: 77 additions & 1 deletion qb-mobile/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field

import 'api/daemon.dart';
import 'api/log.dart';
import 'dart:async';
import 'dart:convert';
import 'frb_generated.dart';
Expand Down Expand Up @@ -66,7 +67,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.3.0';

@override
int get rustContentHash => 1900925057;
int get rustContentHash => 174884596;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
Expand All @@ -84,6 +85,8 @@ abstract class RustLibApi extends BaseApi {
Future<void> crateApiDaemonDaemonWrapperProcess(
{required DaemonWrapper that});

Stream<Uint8List> crateApiLogInitLog();

RustArcIncrementStrongCountFnType
get rust_arc_increment_strong_count_DaemonWrapper;

Expand Down Expand Up @@ -183,6 +186,32 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["that"],
);

@override
Stream<Uint8List> crateApiLogInitLog() {
final logSink = RustStreamSink<Uint8List>();
unawaited(handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_StreamSink_list_prim_u_8_strict_Sse(logSink, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 4, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
decodeErrorData: null,
),
constMeta: kCrateApiLogInitLogConstMeta,
argValues: [logSink],
apiImpl: this,
)));
return logSink.stream;
}

TaskConstMeta get kCrateApiLogInitLogConstMeta => const TaskConstMeta(
debugName: "init_log",
argNames: ["logSink"],
);

RustArcIncrementStrongCountFnType
get rust_arc_increment_strong_count_DaemonWrapper => wire
.rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper;
Expand All @@ -191,6 +220,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
get rust_arc_decrement_strong_count_DaemonWrapper => wire
.rust_arc_decrement_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper;

@protected
AnyhowException dco_decode_AnyhowException(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return AnyhowException(raw as String);
}

@protected
DaemonWrapper
dco_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand Down Expand Up @@ -223,6 +258,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return DaemonWrapperImpl.frbInternalDcoDecode(raw as List<dynamic>);
}

@protected
RustStreamSink<Uint8List> dco_decode_StreamSink_list_prim_u_8_strict_Sse(
dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
throw UnimplementedError();
}

@protected
String dco_decode_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
Expand Down Expand Up @@ -253,6 +295,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return dcoDecodeU64(raw);
}

@protected
AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_String(deserializer);
return AnyhowException(inner);
}

@protected
DaemonWrapper
sse_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand Down Expand Up @@ -289,6 +338,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_decode_usize(deserializer), sse_decode_i_32(deserializer));
}

@protected
RustStreamSink<Uint8List> sse_decode_StreamSink_list_prim_u_8_strict_Sse(
SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
throw UnimplementedError('Unreachable ()');
}

@protected
String sse_decode_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -332,6 +388,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getUint8() != 0;
}

@protected
void sse_encode_AnyhowException(
AnyhowException self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_String(self.message, serializer);
}

@protected
void
sse_encode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand Down Expand Up @@ -372,6 +435,19 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer);
}

@protected
void sse_encode_StreamSink_list_prim_u_8_strict_Sse(
RustStreamSink<Uint8List> self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_String(
self.setupAndSerialize(
codec: SseCodec(
decodeSuccessData: sse_decode_list_prim_u_8_strict,
decodeErrorData: sse_decode_AnyhowException,
)),
serializer);
}

@protected
void sse_encode_String(String self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down
23 changes: 23 additions & 0 deletions qb-mobile/lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field

import 'api/daemon.dart';
import 'api/log.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:ffi' as ffi;
Expand All @@ -22,6 +23,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
get rust_arc_decrement_strong_count_DaemonWrapperPtr => wire
._rust_arc_decrement_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapperPtr;

@protected
AnyhowException dco_decode_AnyhowException(dynamic raw);

@protected
DaemonWrapper
dco_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -42,6 +46,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
dco_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
dynamic raw);

@protected
RustStreamSink<Uint8List> dco_decode_StreamSink_list_prim_u_8_strict_Sse(
dynamic raw);

@protected
String dco_decode_String(dynamic raw);

Expand All @@ -57,6 +65,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
BigInt dco_decode_usize(dynamic raw);

@protected
AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer);

@protected
DaemonWrapper
sse_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -77,6 +88,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
sse_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
SseDeserializer deserializer);

@protected
RustStreamSink<Uint8List> sse_decode_StreamSink_list_prim_u_8_strict_Sse(
SseDeserializer deserializer);

@protected
String sse_decode_String(SseDeserializer deserializer);

Expand All @@ -98,6 +113,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool sse_decode_bool(SseDeserializer deserializer);

@protected
void sse_encode_AnyhowException(
AnyhowException self, SseSerializer serializer);

@protected
void
sse_encode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -118,6 +137,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
sse_encode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
DaemonWrapper self, SseSerializer serializer);

@protected
void sse_encode_StreamSink_list_prim_u_8_strict_Sse(
RustStreamSink<Uint8List> self, SseSerializer serializer);

@protected
void sse_encode_String(String self, SseSerializer serializer);

Expand Down
23 changes: 23 additions & 0 deletions qb-mobile/lib/src/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// ignore_for_file: argument_type_not_assignable

import 'api/daemon.dart';
import 'api/log.dart';
import 'dart:async';
import 'dart:convert';
import 'frb_generated.dart';
Expand All @@ -24,6 +25,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
get rust_arc_decrement_strong_count_DaemonWrapperPtr => wire
.rust_arc_decrement_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper;

@protected
AnyhowException dco_decode_AnyhowException(dynamic raw);

@protected
DaemonWrapper
dco_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -44,6 +48,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
dco_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
dynamic raw);

@protected
RustStreamSink<Uint8List> dco_decode_StreamSink_list_prim_u_8_strict_Sse(
dynamic raw);

@protected
String dco_decode_String(dynamic raw);

Expand All @@ -59,6 +67,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
BigInt dco_decode_usize(dynamic raw);

@protected
AnyhowException sse_decode_AnyhowException(SseDeserializer deserializer);

@protected
DaemonWrapper
sse_decode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -79,6 +90,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
sse_decode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
SseDeserializer deserializer);

@protected
RustStreamSink<Uint8List> sse_decode_StreamSink_list_prim_u_8_strict_Sse(
SseDeserializer deserializer);

@protected
String sse_decode_String(SseDeserializer deserializer);

Expand All @@ -100,6 +115,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
bool sse_decode_bool(SseDeserializer deserializer);

@protected
void sse_encode_AnyhowException(
AnyhowException self, SseSerializer serializer);

@protected
void
sse_encode_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
Expand All @@ -120,6 +139,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
sse_encode_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDaemonWrapper(
DaemonWrapper self, SseSerializer serializer);

@protected
void sse_encode_StreamSink_list_prim_u_8_strict_Sse(
RustStreamSink<Uint8List> self, SseSerializer serializer);

@protected
void sse_encode_String(String self, SseSerializer serializer);

Expand Down
1 change: 1 addition & 0 deletions qb-mobile/rust/src/api.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod daemon;
pub mod log;
48 changes: 48 additions & 0 deletions qb-mobile/rust/src/api/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::frb_generated::StreamSink;
use std::sync::Arc;
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt, util::SubscriberInitExt, Layer};

#[derive(Clone)]
struct LogWriter {
sink: Arc<StreamSink<Vec<u8>>>,
}

impl std::io::Write for LogWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.sink.add(buf.into()).unwrap();
Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
// Nothing to do here
Ok(())
}
}

impl<'a> MakeWriter<'a> for LogWriter {
type Writer = Self;

fn make_writer(&'a self) -> Self::Writer {
self.clone()
}

fn make_writer_for(&'a self, _meta: &tracing::Metadata<'_>) -> Self::Writer {
self.clone()
}
}

pub fn init_log(log_sink: StreamSink<Vec<u8>>) {
let make_writer = LogWriter {
sink: Arc::new(log_sink),
};

let stdout_log = tracing_subscriber::fmt::layer()
.pretty()
.with_writer(make_writer);
tracing_subscriber::registry()
.with(stdout_log.with_filter(LevelFilter::DEBUG))
.init();

info!("logging initialized...");
}
Loading

0 comments on commit af0c8a5

Please sign in to comment.