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 <nathan@appflowy.io>
This commit is contained in:
Kelvin 2023-02-07 22:09:43 +08:00 committed by GitHub
parent 5004729b72
commit 781f0ab88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 10 deletions

View File

@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
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);

View File

@ -133,3 +133,21 @@ typedef _store_dart_post_cobject_C = Void Function(
typedef _store_dart_post_cobject_Dart = void Function(
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>,
);

View File

@ -1,5 +1,10 @@
// ignore: import_of_legacy_library_into_null_safe
import 'dart:ffi';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:ffi/ffi.dart' as ffi;
import 'ffi.dart';
class Log {
static final shared = Log();
@ -9,7 +14,8 @@ class Log {
_logger = Logger(
printer: PrettyPrinter(
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
colors: true, // Colorful log messages
printEmojis: true, // Print an emoji for each log message
@ -19,22 +25,50 @@ class Log {
}
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) {
Log.shared._logger.d(msg);
if (isReleaseVersion()) {
log(1, toNativeUtf8(msg));
} else {
Log.shared._logger.d(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) {
Log.shared._logger.v(msg);
if (isReleaseVersion()) {
log(2, toNativeUtf8(msg));
} else {
Log.shared._logger.v(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();
}

View File

@ -12,4 +12,6 @@ const uint8_t *sync_command(const uint8_t *input, uintptr_t len);
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);

View File

@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
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);

View File

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

View File

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

View File

@ -11,4 +11,6 @@ const uint8_t *sync_event(const uint8_t *input, uintptr_t len);
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);

View File

@ -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),
_ => (),
}
}