David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 1 | // 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 | |
| 21 | extern crate log; |
| 22 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 23 | use crate::console::println; |
| 24 | use core::sync::atomic::{AtomicBool, Ordering}; |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 25 | use log::{LevelFilter, Log, Metadata, Record, SetLoggerError}; |
| 26 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 27 | struct Logger { |
| 28 | is_enabled: AtomicBool, |
| 29 | } |
| 30 | static mut LOGGER: Logger = Logger::new(); |
| 31 | |
| 32 | impl Logger { |
| 33 | const fn new() -> Self { |
| 34 | Self { is_enabled: AtomicBool::new(true) } |
| 35 | } |
| 36 | |
| 37 | fn swap_enabled(&mut self, enabled: bool) -> bool { |
| 38 | self.is_enabled.swap(enabled, Ordering::Relaxed) |
| 39 | } |
| 40 | } |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 41 | |
| 42 | impl Log for Logger { |
| 43 | fn enabled(&self, _metadata: &Metadata) -> bool { |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 44 | self.is_enabled.load(Ordering::Relaxed) |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | fn log(&self, record: &Record) { |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 48 | if self.enabled(record.metadata()) { |
| 49 | println!("[{}] {}", record.level(), record.args()); |
| 50 | } |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | fn flush(&self) {} |
| 54 | } |
| 55 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 56 | /// An RAII implementation of a log suppressor. When the instance is dropped, logging is re-enabled. |
| 57 | pub struct SuppressGuard { |
| 58 | old_enabled: bool, |
| 59 | } |
| 60 | |
| 61 | impl SuppressGuard { |
| 62 | fn new() -> Self { |
| 63 | // Safe because it modifies an atomic. |
| 64 | unsafe { Self { old_enabled: LOGGER.swap_enabled(false) } } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl Drop for SuppressGuard { |
| 69 | fn drop(&mut self) { |
| 70 | // Safe because it modifies an atomic. |
| 71 | unsafe { |
| 72 | LOGGER.swap_enabled(self.old_enabled); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 77 | /// Initialize vmbase logger with a given max logging level. |
| 78 | pub fn init(max_level: LevelFilter) -> Result<(), SetLoggerError> { |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 79 | // Safe because it only sets the global logger. |
| 80 | unsafe { |
| 81 | log::set_logger(&LOGGER)?; |
| 82 | } |
David Brazdil | 8b55777 | 2022-07-05 12:22:20 +0100 | [diff] [blame] | 83 | log::set_max_level(max_level); |
| 84 | Ok(()) |
| 85 | } |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame^] | 86 | |
| 87 | /// Suppress logging until the return value goes out of scope. |
| 88 | pub fn suppress() -> SuppressGuard { |
| 89 | SuppressGuard::new() |
| 90 | } |