Skip to content

Commit

Permalink
Merge pull request #633 from rust-lang/feat/panic-info
Browse files Browse the repository at this point in the history
Use Location::caller() for file and line info
  • Loading branch information
KodrAus authored Jun 26, 2024
2 parents e0d389c + c9e5e13 commit 46894ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
26 changes: 12 additions & 14 deletions src/__private_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use self::sealed::KVs;
use crate::{Level, Metadata, Record};
use std::fmt::Arguments;
pub use std::{file, format_args, line, module_path, stringify};
use std::panic::Location;
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,25 +64,23 @@ 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,
kvs.into_kvs(),
)
log_impl(args, level, target_module_path_and_loc, kvs.into_kvs())
}

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
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

0 comments on commit 46894ef

Please sign in to comment.