underhill_core/get_tracing/
kmsg_writer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Implements a type for writing tracing events to `/dev/kmsg`.

use std::fs::File;
use std::io::IoSlice;
use std::io::Write;
use tracing::Level;
use tracing_subscriber::fmt::MakeWriter;

/// Implements [`MakeWriter`] on top of `/dev/kmsg`.
pub struct KmsgWriter {
    file: File,
    facility: u8,
}

impl KmsgWriter {
    /// Opens `/dev/kmsg`.
    pub fn new(facility: u8) -> std::io::Result<Self> {
        let file = fs_err::OpenOptions::new()
            .write(true)
            .open("/dev/kmsg")?
            .into();

        Ok(Self { facility, file })
    }

    fn make_prefix(&self, level: Level, target: &str) -> String {
        let level = match level {
            Level::ERROR => 3,
            Level::WARN => 4,
            Level::INFO => 6,
            Level::DEBUG | Level::TRACE => 7,
        };

        let n = (level as u8) | (self.facility << 3);
        format!("<{}>{}: ", n, target)
    }
}

pub struct KmsgWithPrefix<'a> {
    kmsg: &'a File,
    prefix: String,
}

impl<'a> MakeWriter<'a> for KmsgWriter {
    type Writer = KmsgWithPrefix<'a>;

    fn make_writer(&'a self) -> Self::Writer {
        KmsgWithPrefix {
            kmsg: &self.file,
            prefix: self.make_prefix(Level::INFO, "underhill"),
        }
    }

    fn make_writer_for(&'a self, meta: &tracing::Metadata<'_>) -> Self::Writer {
        KmsgWithPrefix {
            kmsg: &self.file,
            prefix: self.make_prefix(*meta.level(), meta.target()),
        }
    }
}

impl Write for KmsgWithPrefix<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        // The maximum length for a kmsg write is either 976 or 992 bytes
        // depending on kernel configuration. Lets be safe.
        let max_buf_len = 976 - self.prefix.len();

        let buf_end = std::cmp::min(buf.len(), max_buf_len);
        let bytes_written = self.kmsg.write_vectored(&[
            IoSlice::new(self.prefix.as_bytes()),
            IoSlice::new(&buf[..buf_end]),
        ])?;

        Ok(bytes_written - self.prefix.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}