Skip to main content

hyperlight_common/
log_level.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/// This type is a unified definition of log level filters between the guest and host.
18///
19/// This is needed because currently the guest uses both the `log` and `tracing` crates,
20/// and needs each type of `LevelFilter` from both crates.
21///
22/// To avoid as much as possible the amount of conversions between the two types, we define a
23/// single type that can be converted to both `log::LevelFilter` and `tracing_core::LevelFilter`.
24/// NOTE: This also takes care of the fact that the `tracing` and `log` enum types for the log
25/// levels are not guaranteed to have the same discriminants, so we can't just cast between them.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum GuestLogFilter {
28    Off,
29    Error,
30    Warn,
31    Info,
32    Debug,
33    Trace,
34}
35
36impl From<GuestLogFilter> for tracing_core::LevelFilter {
37    fn from(filter: GuestLogFilter) -> Self {
38        match filter {
39            GuestLogFilter::Off => tracing_core::LevelFilter::OFF,
40            GuestLogFilter::Error => tracing_core::LevelFilter::ERROR,
41            GuestLogFilter::Warn => tracing_core::LevelFilter::WARN,
42            GuestLogFilter::Info => tracing_core::LevelFilter::INFO,
43            GuestLogFilter::Debug => tracing_core::LevelFilter::DEBUG,
44            GuestLogFilter::Trace => tracing_core::LevelFilter::TRACE,
45        }
46    }
47}
48
49impl From<GuestLogFilter> for log::LevelFilter {
50    fn from(filter: GuestLogFilter) -> Self {
51        match filter {
52            GuestLogFilter::Off => log::LevelFilter::Off,
53            GuestLogFilter::Error => log::LevelFilter::Error,
54            GuestLogFilter::Warn => log::LevelFilter::Warn,
55            GuestLogFilter::Info => log::LevelFilter::Info,
56            GuestLogFilter::Debug => log::LevelFilter::Debug,
57            GuestLogFilter::Trace => log::LevelFilter::Trace,
58        }
59    }
60}
61
62/// Used by the host to convert a [`tracing_core::LevelFilter`] to the intermediary [`GuestLogFilter`]
63/// filter that is later converted to `u64` and passed to the guest via the C API.
64impl From<tracing_core::LevelFilter> for GuestLogFilter {
65    fn from(value: tracing_core::LevelFilter) -> Self {
66        match value {
67            tracing_core::LevelFilter::OFF => Self::Off,
68            tracing_core::LevelFilter::ERROR => Self::Error,
69            tracing_core::LevelFilter::WARN => Self::Warn,
70            tracing_core::LevelFilter::INFO => Self::Info,
71            tracing_core::LevelFilter::DEBUG => Self::Debug,
72            tracing_core::LevelFilter::TRACE => Self::Trace,
73        }
74    }
75}
76
77/// Used by the guest to convert a `u64` value passed from the host via the C API to the
78/// intermediary [`GuestLogFilter`] filter that is later converted to both
79/// `tracing_core::LevelFilter` and `log::LevelFilter`.
80impl TryFrom<u64> for GuestLogFilter {
81    type Error = ();
82
83    fn try_from(value: u64) -> Result<Self, <GuestLogFilter as TryFrom<u64>>::Error> {
84        match value {
85            0 => Ok(Self::Off),
86            1 => Ok(Self::Error),
87            2 => Ok(Self::Warn),
88            3 => Ok(Self::Info),
89            4 => Ok(Self::Debug),
90            5 => Ok(Self::Trace),
91            _ => Err(()),
92        }
93    }
94}
95
96/// Used by the host to convert the [`GuestLogFilter`] to a `u64` that is passed to the guest via
97/// the C API.
98impl From<GuestLogFilter> for u64 {
99    fn from(value: GuestLogFilter) -> Self {
100        match value {
101            GuestLogFilter::Off => 0,
102            GuestLogFilter::Error => 1,
103            GuestLogFilter::Warn => 2,
104            GuestLogFilter::Info => 3,
105            GuestLogFilter::Debug => 4,
106            GuestLogFilter::Trace => 5,
107        }
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::GuestLogFilter;
114
115    #[test]
116    fn guest_log_filter_u64_roundtrip() {
117        let variants = [
118            GuestLogFilter::Off,
119            GuestLogFilter::Error,
120            GuestLogFilter::Warn,
121            GuestLogFilter::Info,
122            GuestLogFilter::Debug,
123            GuestLogFilter::Trace,
124        ];
125
126        for variant in variants {
127            let as_u64: u64 = variant.into();
128            let back =
129                GuestLogFilter::try_from(as_u64).expect("conversion from u64 should succeed");
130            assert_eq!(variant, back);
131        }
132    }
133
134    #[test]
135    fn guest_log_filter_tracing_roundtrip() {
136        let variants = [
137            GuestLogFilter::Off,
138            GuestLogFilter::Error,
139            GuestLogFilter::Warn,
140            GuestLogFilter::Info,
141            GuestLogFilter::Debug,
142            GuestLogFilter::Trace,
143        ];
144
145        for variant in variants {
146            let tracing_filter: tracing_core::LevelFilter = variant.into();
147            let back: GuestLogFilter = tracing_filter.into();
148            assert_eq!(variant, back);
149        }
150    }
151
152    #[test]
153    fn guest_log_filter_log_conversion() {
154        let variants = [
155            GuestLogFilter::Off,
156            GuestLogFilter::Error,
157            GuestLogFilter::Warn,
158            GuestLogFilter::Info,
159            GuestLogFilter::Debug,
160            GuestLogFilter::Trace,
161        ];
162
163        let log_variants = [
164            log::LevelFilter::Off,
165            log::LevelFilter::Error,
166            log::LevelFilter::Warn,
167            log::LevelFilter::Info,
168            log::LevelFilter::Debug,
169            log::LevelFilter::Trace,
170        ];
171
172        for (variant, log_variant) in variants.into_iter().zip(log_variants) {
173            let log_filter = log::LevelFilter::from(variant);
174            assert_eq!(log_filter, log_variant);
175        }
176    }
177
178    #[test]
179    fn guest_log_filter_try_from_u64_rejects_invalid() {
180        // Any value outside the defined range [0, 5] should be rejected.
181        assert!(GuestLogFilter::try_from(u64::MAX).is_err());
182        assert!(GuestLogFilter::try_from(6).is_err());
183    }
184}