1 unstable release

new 0.1.0 May 20, 2025

#865 in Procedural macros


Used in hclog

Custom license

24KB
488 lines

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: the Scope the LogKeys belong to. It expects a value of type ScopeKey.
    • default_level: the default Level for all LogKeys. It expects a value of type Level.
    • default_facade: the default FacadeVariant for all LogKeys. It expects a value of type FacadeVariant.
    • with_log: initialize the hclog compatibility mode with crate log. It expects a boolean value.
  • variant attributes:

    • name: the Display name of the LogKey. It expects a [str] value.
    • level: the Level of the LogKey. It expects a value of type Level.
    • facade: the FacadeVariant of the LogKey. It expects a value of type FacadeVariant.

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,
}


hclog Macros

Procedural macros for use with the hclog crate.

Compability

This crate is compatible with rustc >= 1.65.

Documentation

Further documentation is available in the /docs directory.

License

See LICENSE for more information

This crate is based on the strum_macros crate by Peter Glotfelty which is licensed under MIT terms.

Contribution

Dependencies

~200–630KB
~15K SLoC