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:
-
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.
-
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.
I'm writing a
logimplementation and am hoping to use thetarget: "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:
Compare
record.target()againstrecord.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 amain.rsis the name of the crate.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()returnOption<&str>, but that's probably too disruptive a change at this point. Perhaps we could addrecord.is_custom_target() -> bool, and make sure that flag is set appropriately based on which pattern is matched inlog!?I'm happy to work on the code for this, but would like to have some validation of the design before digging in.