error-chain
error-chain copied to clipboard
Adding a crate feature to integrate with logging
My application uses the general Rust log crate, as well as lots of error_chain. I'd like to contribute a feature for this crate called log, which causes quick_main! to write its output to error! instead of (or in addition to) stderr when the feature is enabled.
Additionally, I think it could be useful to have calls to chain_err write their information at the debug level; however, this could be overly verbose. At the same time, chain_err closures contain a lot of useful information that could reduce the burden to write useful logging statements, but still be able to debug an application more easily.
Would you be willing to accept this contribution?
Hum, interesting idea! It should just be optional so that people who don't need it don't pay for it.
We should take into account that all errors don't have the same impact, so maybe it should be configurable?
Here's what I think makes sense:
- The entire bit of functionality is a crate feature, so it must be explicitly enabled (if not enabled, it changes nothing).
quick_main!useserror!instead ofprintln!when the feature's enabledchain_errprints the return value of its closure usingdebug!("{:?}", err)if it ends up actually chaining an error, and if the return value of the closure implementsDebug(I think this is doable viaAnyfor a runtime check whether it supportsDebug).
Logging potentially caught errors to debug should mostly be useful for situations in which errors are being used, but logging hasn't been written. As a result, I think using any level more urgent than debug or trace isn't desirable, as this feature would make it easier to get a bit more debugging context without recompiling; it wouldn't replace proper logging.
Hello, I've created a pull request finally to solve this. I gave up for a while without realizing that #[cfg()] attributes are silently ignored or have different behavior on expressions within macros than on top level functions.