[][src]Crate log_derive

Log Derive

log-derive provides a simple attribute macro that facilitates logs as part of the log facade
Right now the only macro is logfn, this macro is only for functions but it still have a lot of power.

Use

The basic use of the macro is by putting it on top of the function like this: #[logfn(INFO)]
The return type of the function must implement Debug in order for this to work.
The macro will accept all log levels provided by the log facade.
If the function return a Result type the macro will accept the following additional attributes: (ok = "LEVEL") and (err = "LEVEL") this can provide different log levels if the function failed or not.
By default the macro uses the following formatting to print the message: ("LOG DERIVE: {:?}", return_val)
This can be easily changed using the fmt attribute: #[logfn(LEVEL, fmt = "Important Result: {:}") which will accept format strings similar to println!.

Examples

#[macro_use]
extern crate log_derive;
#[macro_use]
extern crate log;

struct Error;
struct Success;
enum Status { Alive, Dead, Unknown }

#[logfn(Warn)]
fn is_alive(person: &Person) -> Status {
   match person.ping() {
       Pong => Status::Alive,
       Timeout => if person.is_awake() {
           Unknown
       } else {
           Dead
       }
  }
}

#[logfn(ok = "TRACE", err = "ERROR")]
fn call_isan(num: &str) -> Result<Success, Error> {
    if num.len() >= 10 && num.len() <= 15 {
        Ok(Success)
    } else {
        Err(Error)
    }
}

#[logfn(INFO, fmt = "a + b = {}")]
fn addition(a: usize, b: usize) -> usize {
    a + b
}

Attribute Macros

logfn

Logs the result of the function it's above.