diff --git a/Cargo.lock b/Cargo.lock index d71ec75a..76cd4726 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2919,12 +2919,10 @@ dependencies = [ "hyper 1.5.1", "indexmap 2.6.0", "interprocess", - "libc", "maxminddb", "metrics", "metrics-exporter-prometheus", "num_enum", - "os_pipe", "ouisync", "ouisync-rand", "ouisync-tracing-fmt", diff --git a/service/Cargo.toml b/service/Cargo.toml index cca38eef..e5fd1760 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -23,7 +23,6 @@ hmac = { workspace = true } hyper = { version = "1.4.1", features = ["server", "http1"] } indexmap = { workspace = true } interprocess = { version = "2.2.2", features = ["tokio"] } -libc = "0.2.147" maxminddb = "0.24.0" metrics = { workspace = true } metrics-exporter-prometheus = { workspace = true } @@ -31,7 +30,6 @@ num_enum = { workspace = true } ouisync = { path = "../lib" } ouisync-tracing-fmt = { path = "../tracing_fmt" } ouisync-vfs = { path = "../vfs" } -os_pipe = { version = "1.1.4", features = ["io_safety"] } pem = { workspace = true } rand = { workspace = true } rmp-serde = { workspace = true } diff --git a/service/src/ffi.rs b/service/src/ffi.rs index 216c5eb2..2162c2bc 100644 --- a/service/src/ffi.rs +++ b/service/src/ffi.rs @@ -256,10 +256,7 @@ unsafe fn try_init_log( (builder, None) }; - let logger = builder - .redirect() - .build() - .map_err(Error::InitializeLogger)?; + let logger = builder.build().map_err(Error::InitializeLogger)?; LOGGER .set(LoggerWrapper { diff --git a/service/src/logger/mod.rs b/service/src/logger/mod.rs index a5a97b38..b697b451 100644 --- a/service/src/logger/mod.rs +++ b/service/src/logger/mod.rs @@ -1,7 +1,6 @@ mod callback; mod color; mod format; -mod redirect; mod stdout; pub use callback::{BufferPool, Callback}; @@ -34,7 +33,6 @@ pub struct Builder<'a> { callback: Option<(Box, BufferPool)>, format: LogFormat, color: LogColor, - redirect: bool, } impl<'a> Builder<'a> { @@ -72,14 +70,6 @@ impl<'a> Builder<'a> { Self { color, ..self } } - /// Redirect stdout and stderr to the log - pub fn redirect(self) -> Self { - Self { - redirect: true, - ..self - } - } - pub fn build(self) -> io::Result { if let Some(parent) = self.file.and_then(|path| path.parent()) { fs::create_dir_all(parent)?; @@ -119,17 +109,11 @@ impl<'a> Builder<'a> { default_panic_hook(panic_info); })); - let redirect = self.redirect.then(redirect::Redirect::new).transpose()?; - - Ok(Logger { - _redirect: redirect, - }) + Ok(Logger) } } -pub struct Logger { - _redirect: Option, -} +pub struct Logger; impl Logger { pub fn builder<'a>() -> Builder<'a> { @@ -139,7 +123,6 @@ impl Logger { callback: None, format: LogFormat::Human, color: LogColor::Auto, - redirect: false, } } diff --git a/service/src/logger/redirect.rs b/service/src/logger/redirect.rs deleted file mode 100644 index b8e18e9f..00000000 --- a/service/src/logger/redirect.rs +++ /dev/null @@ -1,120 +0,0 @@ -use std::{ - io::{self, BufRead, BufReader, Stderr, Stdout}, - os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}, - thread, -}; - -use os_pipe::PipeWriter; -use tracing::{Level, Span}; - -/// Redirect stdout / stderr to tracing events -pub(super) struct Redirect { - _stdout: Guard, - _stderr: Guard, -} - -impl Redirect { - pub fn new() -> io::Result { - Ok(Self { - _stdout: redirect(io::stdout(), tracing::info_span!("stdout"), Level::INFO)?, - _stderr: redirect(io::stderr(), tracing::info_span!("stderr"), Level::ERROR)?, - }) - } -} - -fn redirect(stream: S, span: Span, level: Level) -> io::Result> { - let (reader, writer) = os_pipe::pipe()?; - let redirect = Guard::new(stream, writer)?; - - thread::spawn(move || { - let mut reader = BufReader::new(reader); - let mut line = String::new(); - - let _enter = span.enter(); - - loop { - match reader.read_line(&mut line) { - Ok(n) if n > 0 => { - // Remove the trailing newline - if line.ends_with('\n') { - line.pop(); - } - - // Unfornutally it doesn't seem to be possible to pass non constant level to - // `tracing::event!`... - match level { - Level::ERROR => tracing::error!("{line}"), - Level::WARN => tracing::warn!("{line}"), - Level::INFO => tracing::info!("{line}"), - Level::DEBUG => tracing::debug!("{line}"), - Level::TRACE => tracing::trace!("{line}"), - } - } - Ok(_) => break, // EOF - Err(error) => { - tracing::error!("{error:?}"); - break; - } - } - } - }); - - Ok(redirect) -} - -/// Redirect stdout / stderr -struct Guard -where - S: AsFd, - D: AsFd, -{ - src: S, - src_old: OwnedFd, - _dst: D, -} - -impl Guard -where - S: AsFd, - D: AsFd, -{ - fn new(src: S, dst: D) -> io::Result { - // Remember the old fd so we can point it to where it pointed before when we are done. - let src_old = src.as_fd().try_clone_to_owned()?; - - dup2(dst.as_fd(), src.as_fd())?; - - Ok(Self { - src, - src_old, - _dst: dst, - }) - } -} - -impl Drop for Guard -where - S: AsFd, - D: AsFd, -{ - fn drop(&mut self) { - if let Err(error) = dup2(self.src_old.as_fd(), self.src.as_fd()) { - tracing::error!( - ?error, - "Failed to point the redirected file descriptor to its original target" - ); - } - } -} - -fn dup2(dst: BorrowedFd<'_>, src: BorrowedFd<'_>) -> io::Result<()> { - // SAFETY: Both file descriptors are valid because they are obtained using `as_raw_fd` - // from valid io objects. - unsafe { - if libc::dup2(dst.as_raw_fd(), src.as_raw_fd()) >= 0 { - Ok(()) - } else { - Err(io::Error::last_os_error()) - } - } -}