From 0e0800c2c42fe92139fb2dceaf89efd8051b41aa Mon Sep 17 00:00:00 2001 From: KodrAus Date: Tue, 25 Jun 2024 11:16:21 +1000 Subject: [PATCH] use Location::caller() for file and line info --- src/__private_api.rs | 19 +++++++++++-------- src/lib.rs | 4 ---- src/macros.rs | 3 +-- tests/Cargo.toml | 3 +++ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/__private_api.rs b/src/__private_api.rs index fd0a5a762..d5b907e08 100644 --- a/src/__private_api.rs +++ b/src/__private_api.rs @@ -2,6 +2,7 @@ use self::sealed::KVs; use crate::{Level, Metadata, Record}; +use std::panic::Location; use std::fmt::Arguments; pub use std::{file, format_args, line, module_path, stringify}; @@ -36,8 +37,7 @@ impl<'a> KVs<'a> for () { fn log_impl( args: Arguments, level: Level, - &(target, module_path, file): &(&str, &'static str, &'static str), - line: u32, + &(target, module_path, loc): &(&str, &'static str, &'static Location), kvs: Option<&[(&str, Value)]>, ) { #[cfg(not(feature = "kv"))] @@ -52,8 +52,8 @@ fn log_impl( .level(level) .target(target) .module_path_static(Some(module_path)) - .file_static(Some(file)) - .line(Some(line)); + .file_static(Some(loc.file())) + .line(Some(loc.line())); #[cfg(feature = "kv")] builder.key_values(&kvs); @@ -64,8 +64,7 @@ fn log_impl( pub fn log<'a, K>( args: Arguments, level: Level, - target_module_path_and_file: &(&str, &'static str, &'static str), - line: u32, + target_module_path_and_loc: &(&str, &'static str, &'static Location), kvs: K, ) where K: KVs<'a>, @@ -73,8 +72,7 @@ pub fn log<'a, K>( log_impl( args, level, - target_module_path_and_file, - line, + target_module_path_and_loc, kvs.into_kvs(), ) } @@ -83,6 +81,11 @@ pub fn enabled(level: Level, target: &str) -> bool { crate::logger().enabled(&Metadata::builder().level(level).target(target).build()) } +#[track_caller] +pub fn loc() -> &'static Location<'static> { + Location::caller() +} + #[cfg(feature = "kv")] mod kv_support { use crate::kv; diff --git a/src/lib.rs b/src/lib.rs index 19f44188c..f9e6708fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -341,10 +341,6 @@ #![warn(missing_docs)] #![deny(missing_debug_implementations, unconditional_recursion)] #![cfg_attr(all(not(feature = "std"), not(test)), no_std)] -// When compiled for the rustc compiler itself we want to make sure that this is -// an unstable crate -#![cfg_attr(rustbuild, feature(staged_api, rustc_private))] -#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))] #[cfg(any( all(feature = "max_level_off", feature = "max_level_error"), diff --git a/src/macros.rs b/src/macros.rs index 48a7447ef..b5bce841f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -50,8 +50,7 @@ macro_rules! log { $crate::__private_api::log( $crate::__private_api::format_args!($($arg)+), lvl, - &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()), - $crate::__private_api::line!(), + &($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()), (), ); } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 8754bb16a..bc8621558 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -5,6 +5,9 @@ edition = "2021" publish = false build = "src/build.rs" +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(lib_build)'] } + [features] std = ["log/std"] kv = ["log/kv"]