mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: pass platform info to rust side (#5079)
* chore: pass platform info to rust side * chore: pass platform info to rust side * chore: fix test * chore: fix test * chore: fix test * chore: enable ios log
This commit is contained in:
@ -16,6 +16,7 @@ serde_json.workspace = true
|
||||
serde.workspace = true
|
||||
chrono = "0.4"
|
||||
lazy_static = "1.4.0"
|
||||
lib-infra.workspace = true
|
||||
|
||||
[features]
|
||||
use_bunyan = []
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::sync::RwLock;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use chrono::Local;
|
||||
use lazy_static::lazy_static;
|
||||
use lib_infra::util::Platform;
|
||||
use tracing::subscriber::set_global_default;
|
||||
use tracing_appender::rolling::Rotation;
|
||||
use tracing_appender::{non_blocking::WorkerGuard, rolling::RollingFileAppender};
|
||||
@ -10,8 +11,10 @@ use tracing_subscriber::fmt::format::Writer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
|
||||
|
||||
use crate::layer::FlowyFormattingLayer;
|
||||
use crate::stream_log::{StreamLog, StreamLogSender};
|
||||
|
||||
mod layer;
|
||||
pub mod stream_log;
|
||||
|
||||
lazy_static! {
|
||||
static ref LOG_GUARD: RwLock<Option<WorkerGuard>> = RwLock::new(None);
|
||||
@ -22,10 +25,18 @@ pub struct Builder {
|
||||
name: String,
|
||||
env_filter: String,
|
||||
file_appender: RollingFileAppender,
|
||||
#[allow(dead_code)]
|
||||
platform: Platform,
|
||||
stream_log_sender: Option<Arc<dyn StreamLogSender>>,
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
pub fn new(name: &str, directory: &str) -> Self {
|
||||
pub fn new(
|
||||
name: &str,
|
||||
directory: &str,
|
||||
platform: &Platform,
|
||||
stream_log_sender: Option<Arc<dyn StreamLogSender>>,
|
||||
) -> Self {
|
||||
let file_appender = RollingFileAppender::builder()
|
||||
.rotation(Rotation::DAILY)
|
||||
.filename_prefix(name)
|
||||
@ -35,8 +46,10 @@ impl Builder {
|
||||
|
||||
Builder {
|
||||
name: name.to_owned(),
|
||||
env_filter: "Info".to_owned(),
|
||||
env_filter: "info".to_owned(),
|
||||
file_appender,
|
||||
platform: platform.clone(),
|
||||
stream_log_sender,
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,23 +60,37 @@ impl Builder {
|
||||
|
||||
pub fn build(self) -> Result<(), String> {
|
||||
let env_filter = EnvFilter::new(self.env_filter);
|
||||
|
||||
// let std_out_layer = std::fmt::layer().with_writer(std::io::stdout).pretty();
|
||||
let (non_blocking, guard) = tracing_appender::non_blocking(self.file_appender);
|
||||
let file_layer = FlowyFormattingLayer::new(non_blocking);
|
||||
|
||||
let subscriber = tracing_subscriber::fmt()
|
||||
.with_timer(CustomTime)
|
||||
.with_ansi(true)
|
||||
.with_max_level(tracing::Level::TRACE)
|
||||
.with_thread_ids(false)
|
||||
.pretty()
|
||||
.with_env_filter(env_filter)
|
||||
.finish()
|
||||
.with(JsonStorageLayer)
|
||||
.with(file_layer);
|
||||
|
||||
set_global_default(subscriber).map_err(|e| format!("{:?}", e))?;
|
||||
if let Some(stream_log_sender) = &self.stream_log_sender {
|
||||
let subscriber = tracing_subscriber::fmt()
|
||||
.with_timer(CustomTime)
|
||||
.with_max_level(tracing::Level::TRACE)
|
||||
.with_ansi(self.platform.is_not_ios())
|
||||
.with_writer(StreamLog {
|
||||
sender: stream_log_sender.clone(),
|
||||
})
|
||||
.with_thread_ids(false)
|
||||
.pretty()
|
||||
.with_env_filter(env_filter)
|
||||
.finish()
|
||||
.with(JsonStorageLayer)
|
||||
.with(file_layer);
|
||||
set_global_default(subscriber).map_err(|e| format!("{:?}", e))?;
|
||||
} else {
|
||||
let subscriber = tracing_subscriber::fmt()
|
||||
.with_timer(CustomTime)
|
||||
.with_max_level(tracing::Level::TRACE)
|
||||
.with_ansi(true)
|
||||
.with_thread_ids(false)
|
||||
.pretty()
|
||||
.with_env_filter(env_filter)
|
||||
.finish()
|
||||
.with(JsonStorageLayer)
|
||||
.with(file_layer);
|
||||
set_global_default(subscriber).map_err(|e| format!("{:?}", e))?;
|
||||
};
|
||||
|
||||
*LOG_GUARD.write().unwrap() = Some(guard);
|
||||
Ok(())
|
||||
|
37
frontend/rust-lib/lib-log/src/stream_log.rs
Normal file
37
frontend/rust-lib/lib-log/src/stream_log.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
use tracing_subscriber::fmt::MakeWriter;
|
||||
|
||||
pub struct StreamLog {
|
||||
pub sender: Arc<dyn StreamLogSender>,
|
||||
}
|
||||
|
||||
impl<'a> MakeWriter<'a> for StreamLog {
|
||||
type Writer = SenderWriter;
|
||||
|
||||
fn make_writer(&'a self) -> Self::Writer {
|
||||
SenderWriter {
|
||||
sender: self.sender.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StreamLogSender: Send + Sync {
|
||||
fn send(&self, message: &[u8]);
|
||||
}
|
||||
|
||||
pub struct SenderWriter {
|
||||
sender: Arc<dyn StreamLogSender>,
|
||||
}
|
||||
|
||||
impl Write for SenderWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.sender.send(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user