Expand description
A procedural macro crate to be used with the hclog crate
For more informations on hclog please refer to the hclog crate
documentation.
This crate is a stripped down and modified version of the strum_macros crate
by Peter Glotfelty. It is used to derive the necessary traits for the hclog crate via the
HCLog derive macro. It also provides a few additional attributes to customize the LogKey
variants.
§Usage
Currently only enums with unit variants are supported. The HCLog derive macro will generate
the necessary code to implement the hclog traits for the given enum.
The macro allows to define attributes on the enum and its variants to customize the behavior of
the generated code. The following attributes are supported by the #[hclog()] attribute:
-
enum attributes:
scope: theScopetheLogKeys belong to. It expects a value of typeScopeKey.default_level: the defaultLevelfor allLogKeys. It expects a value of typeLevel.default_facade: the defaultFacadeVariantfor allLogKeys. It expects a value of typeFacadeVariant.with_log: initialize thehclogcompatibility mode with cratelog. It expects a boolean value.
-
variant attributes:
name: theDisplayname of theLogKey. It expects astrvalue.level: theLevelof theLogKey. It expects a value of typeLevel.facade: theFacadeVariantof theLogKey. It expects a value of typeFacadeVariant.
The enum attributes are used as a default for all variants if they don’t define their own
attributes. All attributes are optional and can be omitted if the default behavior is sufficient.
If no attributes are given the defaults from the hclog crate are used.
§Example
§Derive the HCLog trait
use hclog_macros::HCLog;
#[derive(HCLog, Copy, Clone, Debug, PartialEq)]
enum MyLog {
AA,
AB,
AC,
AD,
}§Extend the HCLog trait with additional attributes
It’s possible to set additional attributes via the hclog attribute. For detailed informations
of the attributes consult the hclog crate documentation.
use hclog_macros::HCLog;
use hclog::{Level, FacadeVariant, ScopeKey};
#[derive(HCLog, Copy, Clone, Debug, PartialEq)]
#[hclog(scope = ScopeKey::Application, default_level = Level::Info, default_facade = FacadeVariant::None)]
enum MyLog {
#[hclog(name = "AA", level = Level::Debug1, facade = FacadeVariant::StdOut)]
AA,
#[hclog(name = "AB", level = Level::Info, facade = FacadeVariant::StdErr)]
AB,
#[hclog(name = "AC", level = Level::Warn, facade = FacadeVariant::Syslog("user".to_string()))]
AC,
#[hclog(name = "AD", level = Level::Error, facade = FacadeVariant::File("log.txt".into(), false))]
AD,
}