hyperlight_common/flatbuffer_wrappers/
guest_log_level.rs

1/*
2Copyright 2024 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
17use anyhow::{bail, Error, Result};
18use log::Level;
19use strum::EnumIter;
20#[cfg(feature = "tracing")]
21use tracing::{instrument, Span};
22
23use crate::flatbuffers::hyperlight::generated::LogLevel as FbLogLevel;
24
25#[repr(u8)]
26#[derive(Copy, Clone, Eq, PartialEq, Debug, EnumIter)]
27pub enum LogLevel {
28    Trace = 0,
29    Debug = 1,
30    Information = 2,
31    Warning = 3,
32    Error = 4,
33    Critical = 5,
34    None = 6,
35}
36
37impl From<u8> for LogLevel {
38    #[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
39    fn from(val: u8) -> LogLevel {
40        match val {
41            0 => LogLevel::Trace,
42            1 => LogLevel::Debug,
43            2 => LogLevel::Information,
44            3 => LogLevel::Warning,
45            4 => LogLevel::Error,
46            5 => LogLevel::Critical,
47            _ => LogLevel::None,
48        }
49    }
50}
51
52impl TryFrom<&FbLogLevel> for LogLevel {
53    type Error = Error;
54    #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
55    fn try_from(val: &FbLogLevel) -> Result<LogLevel> {
56        match *val {
57            FbLogLevel::Trace => Ok(LogLevel::Trace),
58            FbLogLevel::Debug => Ok(LogLevel::Debug),
59            FbLogLevel::Information => Ok(LogLevel::Information),
60            FbLogLevel::Warning => Ok(LogLevel::Warning),
61            FbLogLevel::Error => Ok(LogLevel::Error),
62            FbLogLevel::Critical => Ok(LogLevel::Critical),
63            FbLogLevel::None => Ok(LogLevel::None),
64            _ => {
65                bail!("Unsupported Flatbuffers log level: {:?}", val);
66            }
67        }
68    }
69}
70
71impl From<&LogLevel> for FbLogLevel {
72    #[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
73    fn from(val: &LogLevel) -> FbLogLevel {
74        match val {
75            LogLevel::Critical => FbLogLevel::Critical,
76            LogLevel::Debug => FbLogLevel::Debug,
77            LogLevel::Error => FbLogLevel::Error,
78            LogLevel::Information => FbLogLevel::Information,
79            LogLevel::None => FbLogLevel::None,
80            LogLevel::Trace => FbLogLevel::Trace,
81            LogLevel::Warning => FbLogLevel::Warning,
82        }
83    }
84}
85
86impl From<&LogLevel> for Level {
87    // There is a test (sandbox::outb::tests::test_log_outb_log) which emits trace record as logs
88    // which causes a panic when this function is instrumented as the logger is contained in refcell and
89    // instrumentation ends up causing a double mutborrow. So this is not instrumented.
90    //TODO: instrument this once we fix the test
91    fn from(val: &LogLevel) -> Level {
92        match val {
93            LogLevel::Trace => Level::Trace,
94            LogLevel::Debug => Level::Debug,
95            LogLevel::Information => Level::Info,
96            LogLevel::Warning => Level::Warn,
97            LogLevel::Error => Level::Error,
98            LogLevel::Critical => Level::Error,
99            // If the log level is None then we will log as trace
100            LogLevel::None => Level::Trace,
101        }
102    }
103}
104
105impl From<Level> for LogLevel {
106    fn from(val: Level) -> LogLevel {
107        match val {
108            Level::Trace => LogLevel::Trace,
109            Level::Debug => LogLevel::Debug,
110            Level::Info => LogLevel::Information,
111            Level::Warn => LogLevel::Warning,
112            Level::Error => LogLevel::Error,
113        }
114    }
115}