I wrote a new crate `log-derive` for auto logging result of functions


#1

Hi,

I wrote a new crate called log-derive that contains a proc-macro to easily log the result of functions even if they return Result and contain multiple ? operators.

Would love to hear feedback on usability, code and documentation :slight_smile:

https://crates.io/crates/log-derive

The usage is very simple:

 #[logfn(Err = "Error", fmt = "Failed Sending Packet: {:?}")]
 fn send_hi(addr: SocketAddr) -> Result<(), io::Error> {
     let mut stream = TcpStream::connect(addr)?;
     stream.write(b"Hi!")?;
     Ok( () )
 }

#[logfn(Trace)]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}

#2

Looks useful!

I think this project could also be a platform for method tracing; each assignment statement is directly followed by a trace!() macro call that writes out the left hand side followed by the evaluated expression. Finding out if a type implements Debug/Display/ToString could be an issue though… Maybe Mutagen related posts have answered this already?


#3

Isn’t that just the dbg!() macro?
Right now my next change I want to make is to log the function call too(with the inputs)
But I’m not sure if it should be a separate macro, somehow the same one or something like:
foo("bar", 5) => Ok(7)