blob: 5f0f1c272bb3514fad6c781067e785b63b212b5a [file] [log] [blame]
David Brazdil8b557772022-07-05 12:22:20 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Logger for vmbase.
16//!
17//! Internally uses the println! vmbase macro, which prints to crosvm's UART.
18//! Note: may not work if the VM is in an inconsistent state. Exception handlers
19//! should avoid using this logger and instead print with eprintln!.
20
21extern crate log;
22
23use super::println;
24use log::{LevelFilter, Log, Metadata, Record, SetLoggerError};
25
26struct Logger;
27static LOGGER: Logger = Logger;
28
29impl Log for Logger {
30 fn enabled(&self, _metadata: &Metadata) -> bool {
31 true
32 }
33
34 fn log(&self, record: &Record) {
35 println!("[{}] {}", record.level(), record.args());
36 }
37
38 fn flush(&self) {}
39}
40
41/// Initialize vmbase logger with a given max logging level.
42pub fn init(max_level: LevelFilter) -> Result<(), SetLoggerError> {
43 log::set_logger(&LOGGER)?;
44 log::set_max_level(max_level);
45 Ok(())
46}