hyperlight_guest/
logging.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 alloc::string::ToString;
18use alloc::vec::Vec;
19
20use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
21use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
22
23use crate::host_function_call::{outb, OutBAction};
24use crate::shared_output_data::push_shared_output_data;
25
26fn write_log_data(
27    log_level: LogLevel,
28    message: &str,
29    source: &str,
30    caller: &str,
31    source_file: &str,
32    line: u32,
33) {
34    let guest_log_data = GuestLogData::new(
35        message.to_string(),
36        source.to_string(),
37        log_level,
38        caller.to_string(),
39        source_file.to_string(),
40        line,
41    );
42
43    let bytes: Vec<u8> = guest_log_data
44        .try_into()
45        .expect("Failed to convert GuestLogData to bytes");
46
47    push_shared_output_data(bytes).expect("Unable to push log data to shared output data");
48}
49
50pub fn log_message(
51    log_level: LogLevel,
52    message: &str,
53    source: &str,
54    caller: &str,
55    source_file: &str,
56    line: u32,
57) {
58    write_log_data(log_level, message, source, caller, source_file, line);
59    outb(OutBAction::Log as u16, 0);
60}