Skip to main content

console

Macro console 

Source
macro_rules! console {
    (pass, $($arg:tt)+) => { ... };
    (notice, $($arg:tt)+) => { ... };
    (info, $($arg:tt)+) => { ... };
    (warn, $($arg:tt)+) => { ... };
    (error, $($arg:tt)+) => { ... };
    ($result:expr, $color:expr, $($arg:tt)+) => { ... };
    ($result:expr, $($arg:tt)+) => { ... };
}
Expand description

Creates a tracing event with the required fields for our ConsoleLayer.

This macro provides a way to log events with different status levels and to handle Result types automatically.

§Usage

The macro has several forms depending on the desired outcome.

§Simple Logging

You can log a simple message with a specific status. The supported statuses are notice, info, warn, and error.

console!(notice, "This is a notice");
console!(info, "This is an informational message");
console!(warn, "This is a warning");
console!(error, "This is an error");

Finally, you can completely passthrough colors and text styles by using the pass argument to the macro.

console!(pass, "This is a string with no style");

§Handling Result Types

The macro can also be used to log the outcome of a Result. It will automatically log a success or failure message based on the variant of the Result.

If the Ok variant of the Result contains a value that implements Display, it will be appended to the message.

let successful_result: Result<&str, &str> = Ok("everything is fine");
console!(successful_result, "A successful operation");

let failed_result: Result<&str, &str> = Err("something went wrong");
console!(failed_result, "A failed operation");

If the Ok variant is a unit type (()), no extra value is appended to the message.

let successful_result: Result<(), &str> = Ok(());
console!(successful_result, "An operation with no output");

You can also customize the color of the success message output.

let successful_result: Result<&str, &str> = Ok("everything is fine");
console!(successful_result, |s: String| s.blue().to_string(), "A successful operation with custom colors");