Skip to content

Commit

Permalink
use Location::caller() for file and line info
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jun 25, 2024
1 parent c879b01 commit f796400
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
21 changes: 12 additions & 9 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

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};
pub use std::{format_args, module_path, stringify};

#[cfg(not(feature = "kv"))]
pub type Value<'a> = &'a str;
Expand Down Expand Up @@ -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"))]
Expand All @@ -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);
Expand All @@ -64,17 +64,15 @@ 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>,
{
log_impl(
args,
level,
target_module_path_and_file,
line,
target_module_path_and_loc,
kvs.into_kvs(),
)
}
Expand All @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
6 changes: 2 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,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()),
&[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+]
);
}
Expand All @@ -50,8 +49,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()),
(),
);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit f796400

Please sign in to comment.