Home > Mobile >  How can I redirect dbg!() to stdout?
How can I redirect dbg!() to stdout?

Time:02-27

There are some situations where stderr is not available. Is there a way to have dbg!() print to stdout or an alternative command that prints the same information as dbg!() to stdout?

CodePudding user response:

For an alternative command, copy-and-paste the implementation of dbg, changing eprintln to println and $crate to ::std:

macro_rules! dbg {
    // NOTE: We cannot use `concat!` to make a static string as a format argument
    // of `println!` because `file!` could contain a `{` or
    // `$val` expression could be a block (`{ .. }`), in which case the `println!`
    // will be malformed.
    () => {
        ::std::println!("[{}:{}]", ::std::file!(), ::std::line!())
    };
    ($val:expr $(,)?) => {
        // Use of `match` here is intentional because it affects the lifetimes
        // of temporaries - https://stackoverflow.com/a/48732525/1063961
        match $val {
            tmp => {
                ::std::println!("[{}:{}] {} = {:#?}",
                    ::std::file!(), ::std::line!(), ::std::stringify!($val), &tmp);
                tmp
            }
        }
    };
    ($($val:expr),  $(,)?) => {
        ($(::std::dbg!($val)), ,)
    };
}

fn main() {
    dbg!(1   1);
}
  • Related