|
| 1 | +// Copyright lowRISC contributors. |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +//! Debug-logging functionality. |
| 6 | +//! |
| 7 | +//! This module is still present when the `log` feature is disabled, but all |
| 8 | +//! logging operations are redacted. Redaction completely compiles out log |
| 9 | +//! statements: not even the format strings remain in the final binary. |
| 10 | +//! |
| 11 | +//! Manticore code *should not* call into the [`log`] crate directly outside of |
| 12 | +//! this module. |
| 13 | +
|
| 14 | +#![allow(unused)] |
| 15 | + |
| 16 | +#[cfg(doc)] |
| 17 | +use __raw_log as log; |
| 18 | + |
| 19 | +/// Checks a condition, logging if it fails. |
| 20 | +/// |
| 21 | +/// If the condition does not hold, constructs the given error, logs it, and |
| 22 | +/// returns out of the current function with it. |
| 23 | +macro_rules! check { |
| 24 | + ($cond:expr, $error:expr) => { |
| 25 | + if !$cond { |
| 26 | + let e = $error; |
| 27 | + error!( |
| 28 | + "check failure: `{}`; returned {:?}" |
| 29 | + stringify!($cond), e |
| 30 | + ); |
| 31 | + return Err(e); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Logs a newly-created error value and returns it. |
| 37 | +/// |
| 38 | +/// This function is useful for marking where errors originate in tests. |
| 39 | +/// For example, instead of writing `foo.ok_or(MyError)`, instead write |
| 40 | +/// `foo.ok_or_else(|| trace!(MyError))`. |
| 41 | +macro_rules! trace { |
| 42 | + ($error:expr, $($format:tt)+) => {{ |
| 43 | + error!($($format)+); |
| 44 | + $error |
| 45 | + }}; |
| 46 | + ($error:expr) => {{ |
| 47 | + let e = $error; |
| 48 | + error!("generated error: `{:?}`", e); |
| 49 | + e |
| 50 | + }}; |
| 51 | +} |
| 52 | + |
| 53 | +/// Redactable version of [`log::info!()`]. |
| 54 | +macro_rules! info { |
| 55 | + ($($args:tt)*) => { |
| 56 | + #[cfg(feature = "log")] |
| 57 | + let _ = __raw_log::info!($($args)*); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Redactable version of [`log::warn!()`]. |
| 62 | +macro_rules! warn { |
| 63 | + ($($args:tt)*) => { |
| 64 | + #[cfg(feature = "log")] |
| 65 | + let _ = __raw_log::warn!($($args)*); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Redactable version of [`log::error!()`]. |
| 70 | +macro_rules! error { |
| 71 | + ($($args:tt)*) => { |
| 72 | + #[cfg(feature = "log")] |
| 73 | + let _ = __raw_log::error!($($args)*); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Set up some life-before-main code that initializes a basic logger for the |
| 78 | +/// test binary. |
| 79 | +/// |
| 80 | +/// This needs to happen here, since the test binary's main() cannot be |
| 81 | +/// overriden. |
| 82 | +#[cfg(test)] |
| 83 | +#[ctor::ctor] |
| 84 | +fn init_test_logger() { |
| 85 | + env_logger::builder() |
| 86 | + .format(move |_, record| { |
| 87 | + use std::io::Write; |
| 88 | + |
| 89 | + let thread = std::thread::current(); |
| 90 | + let name = thread.name().unwrap_or("<unknown>"); |
| 91 | + for line in record.args().to_string().trim().lines() { |
| 92 | + // NOTE: we explicitly print to stderr, since this allows the |
| 93 | + // Rust test harness to supress log statements originating from |
| 94 | + // passing tests. |
| 95 | + eprintln!( |
| 96 | + "[{level}({thread}) {file}:{line}] {msg}", |
| 97 | + level = record.level(), |
| 98 | + thread = name, |
| 99 | + file = record.file().unwrap_or("<unknown>"), |
| 100 | + line = record.line().unwrap_or(0), |
| 101 | + msg = line, |
| 102 | + ) |
| 103 | + } |
| 104 | + Ok(()) |
| 105 | + }) |
| 106 | + .init(); |
| 107 | +} |
0 commit comments