Skip to content

Can't disambiguate module paths and custom targets #390

@acfoltzer

Description

@acfoltzer

I'm writing a log implementation and am hoping to use the target: "custom_target" syntax to direct logging output to various endpoints. If the target is omitted when calling the macros, though, the target is filled in with the module path of the call site. This leads to ambiguity between the special-purpose targets and the module paths.

There are a couple of workarounds I'm considering, but neither is totally satisfactory:

  1. Compare record.target() against record.module_path(), and ignore the target if they are equal. This approach has false negatives, and does the wrong thing if the target happens to share the name of the module path. This is somewhat common because the module path of a main.rs is the name of the crate.

  2. Initialize the logger with the expected custom targets, and compare record.target() for membership in that set, treating it as a custom target if there's a match. This approach incurs the performance cost of that lookup, plus false positives in the case where there's a name collision.

It would be great if the data structures just indicated whether or not a target was explicitly set by the macro caller. If I were starting from scratch, I would propose making record.target() return Option<&str>, but that's probably too disruptive a change at this point. Perhaps we could add record.is_custom_target() -> bool, and make sure that flag is set appropriately based on which pattern is matched in log!?

I'm happy to work on the code for this, but would like to have some validation of the design before digging in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions