From a476e8c0079faf6239afae12474df278be25f499 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 27 Aug 2020 14:55:17 -0400 Subject: [PATCH 1/3] Use `std::panic::Location` for file & line information this commit adds a feature, `track_caller`, that allows the user to opt-in to using location information from `std::panic::Location` instead of the built-in `file!` and `line!` macros. This will allow log record location information to be manipulated in the same way that `#[track_caller]` allows panic location information to be manipulated --- Cargo.toml | 3 +++ src/macros.rs | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4021fb041..01afb0ecd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,9 @@ std = [] kv_unstable = [] kv_unstable_sval = ["kv_unstable", "sval/fmt"] +# requires 1.46.0 +track_caller = [] + [dependencies] cfg-if = "0.1.2" serde = { version = "1.0", optional = true, default-features = false } diff --git a/src/macros.rs b/src/macros.rs index ae6080d9b..e28cae583 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -249,7 +249,11 @@ macro_rules! __log_module_path { #[macro_export] macro_rules! __log_file { () => { - file!() + if cfg!(feature = "track_caller") { + ::std::panic::Location::caller().file() + } else { + file!() + } }; } @@ -257,6 +261,10 @@ macro_rules! __log_file { #[macro_export] macro_rules! __log_line { () => { - line!() + if cfg!(feature = "track_caller") { + ::std::panic::Location::caller().line() + } else { + line!() + } }; } From 88fa5d00ef5e43099ec45b273eccecfbeade34fb Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 27 Aug 2020 16:11:30 -0400 Subject: [PATCH 2/3] Use `#[cfg()]` instead of `if cfg!` --- src/macros.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index e28cae583..dcb0531d4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -249,9 +249,12 @@ macro_rules! __log_module_path { #[macro_export] macro_rules! __log_file { () => { - if cfg!(feature = "track_caller") { + #[cfg(feature = "track_caller")] + { ::std::panic::Location::caller().file() - } else { + } + #[cfg(not(feature = "track_caller"))] + { file!() } }; @@ -261,9 +264,12 @@ macro_rules! __log_file { #[macro_export] macro_rules! __log_line { () => { - if cfg!(feature = "track_caller") { + #[cfg(feature = "track_caller")] + { ::std::panic::Location::caller().line() - } else { + } + #[cfg(not(feature = "track_caller"))] + { line!() } }; From 10636ec27a217ded9a8635a6a6889a0058d13320 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 27 Aug 2020 16:22:32 -0400 Subject: [PATCH 3/3] make sure `cfg` is evaluated in the right context --- src/macros.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index dcb0531d4..30a861345 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -247,30 +247,36 @@ macro_rules! __log_module_path { #[doc(hidden)] #[macro_export] +#[cfg(feature = "track_caller")] macro_rules! __log_file { () => { - #[cfg(feature = "track_caller")] - { - ::std::panic::Location::caller().file() - } - #[cfg(not(feature = "track_caller"))] - { - file!() - } + ::std::panic::Location::caller().file() + }; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(not(feature = "track_caller"))] +macro_rules! __log_file { + () => { + file!() }; } #[doc(hidden)] #[macro_export] +#[cfg(feature = "track_caller")] macro_rules! __log_line { () => { - #[cfg(feature = "track_caller")] - { - ::std::panic::Location::caller().line() - } - #[cfg(not(feature = "track_caller"))] - { - line!() - } + ::std::panic::Location::caller().line() + }; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(not(feature = "track_caller"))] +macro_rules! __log_line { + () => { + line!() }; }