Ver código fonte

feat: propagate log from flutter to rust backend (#1723)

* feat: draft commit for getting guidance on send log to backend issue

* feat: modify according to guidance

* feat: add tracing dependencies

* feat: continue implement for sending log to backend

* fix: compile errors

* feat: remove un-necessary code

---------

Co-authored-by: nathan <[email protected]>
Kelvin 2 anos atrás
pai
commit
781f0ab88b

+ 3 - 1
frontend/app_flowy/linux/flutter/dart_ffi/binding.h

@@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
 
 
 int32_t set_stream_port(int64_t port);
 int32_t set_stream_port(int64_t port);
 
 
-void link_me_please(void);
+void link_me_please(void);
+
+void backend_log(int64_t level, const char *data);

+ 18 - 0
frontend/app_flowy/packages/appflowy_backend/lib/ffi.dart

@@ -133,3 +133,21 @@ typedef _store_dart_post_cobject_C = Void Function(
 typedef _store_dart_post_cobject_Dart = void Function(
 typedef _store_dart_post_cobject_Dart = void Function(
   Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
   Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
 );
 );
+
+void log(
+  int level,
+  Pointer<ffi.Utf8> data,
+) {
+  _invoke_log(level, data);
+}
+
+final _invoke_log_Dart _invoke_log = _dart_ffi_lib
+    .lookupFunction<_invoke_log_C, _invoke_log_Dart>('backend_log');
+typedef _invoke_log_C = Void Function(
+  Int64 level,
+  Pointer<ffi.Utf8> data,
+);
+typedef _invoke_log_Dart = void Function(
+  int level,
+  Pointer<ffi.Utf8>,
+);

+ 40 - 6
frontend/app_flowy/packages/appflowy_backend/lib/log.dart

@@ -1,5 +1,10 @@
 // ignore: import_of_legacy_library_into_null_safe
 // ignore: import_of_legacy_library_into_null_safe
+import 'dart:ffi';
+
+import 'package:flutter/foundation.dart';
 import 'package:logger/logger.dart';
 import 'package:logger/logger.dart';
+import 'package:ffi/ffi.dart' as ffi;
+import 'ffi.dart';
 
 
 class Log {
 class Log {
   static final shared = Log();
   static final shared = Log();
@@ -9,7 +14,8 @@ class Log {
     _logger = Logger(
     _logger = Logger(
       printer: PrettyPrinter(
       printer: PrettyPrinter(
           methodCount: 2, // number of method calls to be displayed
           methodCount: 2, // number of method calls to be displayed
-          errorMethodCount: 8, // number of method calls if stacktrace is provided
+          errorMethodCount:
+              8, // number of method calls if stacktrace is provided
           lineLength: 120, // width of the output
           lineLength: 120, // width of the output
           colors: true, // Colorful log messages
           colors: true, // Colorful log messages
           printEmojis: true, // Print an emoji for each log message
           printEmojis: true, // Print an emoji for each log message
@@ -19,22 +25,50 @@ class Log {
   }
   }
 
 
   static void info(dynamic msg) {
   static void info(dynamic msg) {
-    Log.shared._logger.i(msg);
+    if (isReleaseVersion()) {
+      log(0, toNativeUtf8(msg));
+    } else {
+      Log.shared._logger.i(msg);
+    }
   }
   }
 
 
   static void debug(dynamic msg) {
   static void debug(dynamic msg) {
-    Log.shared._logger.d(msg);
+    if (isReleaseVersion()) {
+      log(1, toNativeUtf8(msg));
+    } else {
+      Log.shared._logger.d(msg);
+    }
   }
   }
 
 
   static void warn(dynamic msg) {
   static void warn(dynamic msg) {
-    Log.shared._logger.w(msg);
+    if (isReleaseVersion()) {
+      log(3, toNativeUtf8(msg));
+    } else {
+      Log.shared._logger.w(msg);
+    }
   }
   }
 
 
   static void trace(dynamic msg) {
   static void trace(dynamic msg) {
-    Log.shared._logger.v(msg);
+    if (isReleaseVersion()) {
+      log(2, toNativeUtf8(msg));
+    } else {
+      Log.shared._logger.v(msg);
+    }
   }
   }
 
 
   static void error(dynamic msg) {
   static void error(dynamic msg) {
-    Log.shared._logger.e(msg);
+    if (isReleaseVersion()) {
+      log(4, toNativeUtf8(msg));
+    } else {
+      Log.shared._logger.e(msg);
+    }
   }
   }
 }
 }
+
+bool isReleaseVersion() {
+  return kReleaseMode;
+}
+
+Pointer<ffi.Utf8> toNativeUtf8(dynamic msg) {
+  return "$msg".toNativeUtf8();
+}

+ 3 - 1
frontend/app_flowy/packages/appflowy_backend/linux/Classes/binding.h

@@ -12,4 +12,6 @@ const uint8_t *sync_command(const uint8_t *input, uintptr_t len);
 
 
 int32_t set_stream_port(int64_t port);
 int32_t set_stream_port(int64_t port);
 
 
-void link_me_please(void);
+void link_me_please(void);
+
+void backend_log(int64_t level, const char *data);

+ 3 - 1
frontend/app_flowy/packages/appflowy_backend/macos/Classes/binding.h

@@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
 
 
 int32_t set_stream_port(int64_t port);
 int32_t set_stream_port(int64_t port);
 
 
-void link_me_please(void);
+void link_me_please(void);
+
+void backend_log(int64_t level, const char *data);

+ 1 - 0
frontend/rust-lib/Cargo.lock

@@ -681,6 +681,7 @@ dependencies = [
  "serde",
  "serde",
  "serde_json",
  "serde_json",
  "tokio",
  "tokio",
+ "tracing",
 ]
 ]
 
 
 [[package]]
 [[package]]

+ 2 - 0
frontend/rust-lib/dart-ffi/Cargo.toml

@@ -23,6 +23,8 @@ bytes = { version = "1.0" }
 crossbeam-utils = "0.8.7"
 crossbeam-utils = "0.8.7"
 lazy_static = "1.4.0"
 lazy_static = "1.4.0"
 parking_lot = "0.12.1"
 parking_lot = "0.12.1"
+tracing = { version = "0.1", features = ["log"] }
+
 
 
 lib-dispatch = { path = "../lib-dispatch" }
 lib-dispatch = { path = "../lib-dispatch" }
 flowy-core = { path = "../flowy-core" }
 flowy-core = { path = "../flowy-core" }

+ 3 - 1
frontend/rust-lib/dart-ffi/binding.h

@@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
 
 
 int32_t set_stream_port(int64_t port);
 int32_t set_stream_port(int64_t port);
 
 
-void link_me_please(void);
+void link_me_please(void);
+
+void backend_log(int64_t level, const char *data);

+ 16 - 0
frontend/rust-lib/dart-ffi/src/lib.rs

@@ -111,3 +111,19 @@ async fn post_to_flutter(response: AFPluginEventResponse, port: i64) {
         }
         }
     }
     }
 }
 }
+
+#[no_mangle]
+pub extern "C" fn backend_log(level: i64, data: *const c_char) {
+    let c_str = unsafe { CStr::from_ptr(data) };
+    let log_str = c_str.to_str().unwrap();
+
+    // Don't change the mapping relation between number and level
+    match level {
+        0 => tracing::info!("{}", log_str),
+        1 => tracing::debug!("{}", log_str),
+        2 => tracing::trace!("{}", log_str),
+        3 => tracing::warn!("{}", log_str),
+        4 => tracing::error!("{}", log_str),
+        _ => (),
+    }
+}