microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e1cdda602823a24e5785dbcb350e21da7f113215

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
8use std::sync::Arc;
9use tracing_subscriber::filter::Targets;
10use tracing_subscriber::layer::SubscriberExt;
11use tracing_subscriber::util::SubscriberInitExt;
12
13/// Initialize tracing, returning a mesh pipe to read logs from.
14pub 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
35struct TracingWriter(mesh::pipe::WritePipe);
36
37impl 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