mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
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:
@ -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);
|
||||||
|
@ -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>,
|
||||||
|
);
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -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
frontend/rust-lib/Cargo.lock
generated
1
frontend/rust-lib/Cargo.lock
generated
@ -681,6 +681,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -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" }
|
||||||
|
@ -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);
|
||||||
|
@ -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),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user