|
| 1 | +/* |
| 2 | +Copyright 2025 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations 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::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)] |
| 27 | +pub enum GuestLogFilter { |
| 28 | + Off, |
| 29 | + Error, |
| 30 | + Warn, |
| 31 | + Info, |
| 32 | + Debug, |
| 33 | + Trace, |
| 34 | +} |
| 35 | + |
| 36 | +impl 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 | + |
| 49 | +impl 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. |
| 64 | +impl 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`. |
| 80 | +impl 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. |
| 98 | +impl 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 | +} |
0 commit comments