microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
petri/pipette/src/trace.rs
47lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //! [`tracing`] support. |
| 5 | |
| 6 | #![cfg(any(target_os = "linux", target_os = "windows"))] |
| 7 | |
| 8 | use std::sync::Arc; |
| 9 | use tracing_subscriber::filter::Targets; |
| 10 | use tracing_subscriber::layer::SubscriberExt; |
| 11 | use tracing_subscriber::util::SubscriberInitExt; |
| 12 | |
| 13 | /// Initialize tracing, returning a mesh pipe to read logs from. |
| 14 | pub fn init_tracing() -> mesh::pipe::ReadPipe { |
| 15 | let (log_read, log_write) = mesh::pipe::pipe(); |
| 16 | let targets = Targets::new() |
| 17 | .with_default(tracing::level_filters::LevelFilter::DEBUG) |
| 18 | .with_target("mesh_remote", tracing::level_filters::LevelFilter::INFO); |
| 19 | |
| 20 | tracing_subscriber::fmt() |
| 21 | .compact() |
| 22 | .with_ansi(false) |
| 23 | .with_timer(tracing_subscriber::fmt::time::uptime()) |
| 24 | .with_writer(Arc::new(TracingWriter(log_write))) |
| 25 | .with_max_level(tracing::level_filters::LevelFilter::DEBUG) |
| 26 | .log_internal_errors(true) |
| 27 | .finish() |
| 28 | .with(targets) |
| 29 | .init(); |
| 30 | |
| 31 | tracing::info!("tracing initialized"); |
| 32 | log_read |
| 33 | } |
| 34 | |
| 35 | struct TracingWriter(mesh::pipe::WritePipe); |
| 36 | |
| 37 | impl std::io::Write for &TracingWriter { |
| 38 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 39 | // Note that this will fail if the pipe fills up. This is probably fine |
| 40 | // for this use case. |
| 41 | self.0.write_nonblocking(buf) |
| 42 | } |
| 43 | |
| 44 | fn flush(&mut self) -> std::io::Result<()> { |
| 45 | Ok(()) |
| 46 | } |
| 47 | } |
| 48 | |