Skip to content

Add support for column information in callsite metadata. #3277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions tracing-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub mod __macro_support {
// Re-export the `core` functions that are used in macros. This allows
// a crate to be named `core` and avoid name clashes.
// See here: https://github.com/tokio-rs/tracing/issues/2761
pub use core::{file, line, module_path, option::Option};
pub use core::{column, file, line, module_path, option::Option};
}

/// Statically constructs an [`Identifier`] for the provided [`Callsite`].
Expand Down Expand Up @@ -272,11 +272,22 @@ macro_rules! metadata {
$name,
$target,
$level,
$crate::location!(),
$crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)),
$kind,
)
};
}

/// Statically constructs a new source code location.
#[macro_export]
macro_rules! location {
() => {
$crate::metadata::Location::new(
$crate::__macro_support::Option::Some($crate::__macro_support::file!()),
$crate::__macro_support::Option::Some($crate::__macro_support::line!()),
$crate::__macro_support::Option::Some($crate::__macro_support::column!()),
$crate::__macro_support::Option::Some($crate::__macro_support::module_path!()),
$crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)),
$kind,
)
};
}
Expand Down Expand Up @@ -311,7 +322,7 @@ pub use self::{
dispatch::Dispatch,
event::Event,
field::Field,
metadata::{Level, LevelFilter, Metadata},
metadata::{Level, LevelFilter, Location, Metadata},
};

pub use self::{collect::Interest, metadata::Kind};
Expand Down
168 changes: 116 additions & 52 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,8 @@ pub struct Metadata<'a> {
/// The level of verbosity of the described span.
level: Level,

/// The name of the Rust module where the span occurred, or `None` if this
/// could not be determined.
module_path: Option<&'a str>,

/// The name of the source code file where the span occurred, or `None` if
/// this could not be determined.
file: Option<&'a str>,

/// The line number in the source code file where the span occurred, or
/// `None` if this could not be determined.
line: Option<u32>,
/// The position in the source code.
location: Location<'a>,

/// The names of the key-value fields attached to the described span or
/// event.
Expand All @@ -87,6 +78,15 @@ pub struct Metadata<'a> {
kind: Kind,
}

/// Describes a source code location.
#[derive(Eq, PartialEq, Clone, Default)]
pub struct Location<'a> {
file: Option<&'a str>,
line: Option<u32>,
column: Option<u32>,
module_path: Option<&'a str>,
}

/// Indicates whether the callsite is a span or event.
#[derive(Clone, Eq, PartialEq)]
pub struct Kind(u8);
Expand Down Expand Up @@ -256,19 +256,15 @@ impl<'a> Metadata<'a> {
name: &'static str,
target: &'a str,
level: Level,
file: Option<&'a str>,
line: Option<u32>,
module_path: Option<&'a str>,
location: Location<'a>,
fields: field::FieldSet,
kind: Kind,
) -> Self {
Metadata {
name,
target,
level,
module_path,
file,
line,
location,
fields,
kind,
}
Expand Down Expand Up @@ -299,25 +295,31 @@ impl<'a> Metadata<'a> {
self.target
}

/// Returns the path to the Rust module where the span occurred, or
/// `None` if the module path is unknown.
pub fn module_path(&self) -> Option<&'a str> {
self.module_path
/// Returns the name of the source code file where this `Metadata`
/// originated from, or [`None`] if the file is unknown.
pub fn file(&self) -> Option<&'a str> {
self.location.file()
}

/// Returns the name of the source code file where the span
/// occurred, or `None` if the file is unknown
pub fn file(&self) -> Option<&'a str> {
self.file
/// Returns the path to the Rust module where this `Metadata`
/// originated from, or [`None`] if the module path is unknown.
pub fn module_path(&self) -> Option<&'a str> {
self.location.module_path()
}

/// Returns the line number in the source code file where the span
/// occurred, or `None` if the line number is unknown.
/// Returns the line number in the source code file where this `Metadata`
/// originated from, or [`None`] if the line number is unknown.
pub fn line(&self) -> Option<u32> {
self.line
self.location.line()
}

/// Returns the column in the source code file where this `Metadata`
/// originated from, or [`None`] if the line number is unknown.
pub fn column(&self) -> Option<u32> {
self.location.column()
}

/// Returns an opaque `Identifier` that uniquely identifies the callsite
/// Returns an opaque [`Identifier`](callsite::Identifier) that uniquely identifies the callsite
/// this `Metadata` originated from.
#[inline]
pub fn callsite(&self) -> callsite::Identifier {
Expand All @@ -340,31 +342,99 @@ impl fmt::Debug for Metadata<'_> {
let mut meta = f.debug_struct("Metadata");
meta.field("name", &self.name)
.field("target", &self.target)
.field("level", &self.level);
.field("level", &self.level)
.field("location", &self.location)
.field("fields", &format_args!("{}", self.fields))
.field("callsite", &self.callsite())
.field("kind", &self.kind)
.finish()
}
}

// ===== impl Location =====
impl<'a> Location<'a> {
/// Constructs a new source code location where the [`Metadata`] originated from.
pub const fn new(
file: Option<&'a str>,
line: Option<u32>,
column: Option<u32>,
module_path: Option<&'a str>,
) -> Self {
Self {
file,
line,
column,
module_path,
}
}

/// Returns the name of the source code file where this `Metadata`
/// originated from, or [`None`] if the file is unknown.
pub fn column(&self) -> Option<u32> {
self.column
}

/// Returns the path to the Rust module where this
/// `Metadata` originated from, or `None` if the module path is unknown.
pub fn module_path(&self) -> Option<&'a str> {
self.module_path
}

/// Returns the path to the Rust module where this `Metadata`
/// originated from, or [`None`] if the module path is unknown.
pub fn file(&self) -> Option<&'a str> {
self.file
}

/// Returns the column in the source code file where this `Metadata`
/// originated from, or [`None`] if the line number is unknown.
pub fn line(&self) -> Option<u32> {
self.line
}
}

impl<'a> fmt::Debug for Location<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut loc = f.debug_struct("Location");

if let Some(path) = self.module_path() {
meta.field("module_path", &path);
loc.field("module_path", &path);
}

if let Some(column) = self.column() {
loc.field("column", &column);
}

match (self.file(), self.line()) {
(Some(file), Some(line)) => {
meta.field("location", &format_args!("{}:{}", file, line));
match (self.file(), self.line(), self.column()) {
(Some(file), Some(line), Some(column)) => {
loc.field("location", &format_args!("{}:{}:{}", file, line, column));
}
(Some(file), Some(line), None) => {
loc.field("file", &format_args!("{}:{}", file, line));
}
(Some(file), None) => {
meta.field("file", &format_args!("{}", file));
(Some(file), None, None) => {
loc.field("file", &format_args!("{}", file));
}
(None, None, Some(column)) => {
loc.field("column", &column);
}

// Note: a line num with no file is a kind of weird case that _probably_ never occurs...
(None, Some(line)) => {
meta.field("line", &line);
(None, Some(line), None) => {
loc.field("line", &line);
}
(None, Some(line), Some(column)) => {
loc.field("column", &column);
loc.field("line", &line);
}
(None, None) => {}
(Some(file), None, Some(column)) => {
loc.field("column", &column);
loc.field("file", &file);
}
(None, None, None) => {}
};

meta.field("fields", &format_args!("{}", self.fields))
.field("callsite", &self.callsite())
.field("kind", &self.kind)
.finish()
loc.finish()
}
}

Expand Down Expand Up @@ -465,9 +535,7 @@ impl PartialEq for Metadata<'_> {
name: lhs_name,
target: lhs_target,
level: lhs_level,
module_path: lhs_module_path,
file: lhs_file,
line: lhs_line,
location: lhs_location,
fields: lhs_fields,
kind: lhs_kind,
} = self;
Expand All @@ -476,9 +544,7 @@ impl PartialEq for Metadata<'_> {
name: rhs_name,
target: rhs_target,
level: rhs_level,
module_path: rhs_module_path,
file: rhs_file,
line: rhs_line,
location: rhs_location,
fields: rhs_fields,
kind: rhs_kind,
} = &other;
Expand All @@ -490,9 +556,7 @@ impl PartialEq for Metadata<'_> {
&& lhs_name == rhs_name
&& lhs_target == rhs_target
&& lhs_level == rhs_level
&& lhs_module_path == rhs_module_path
&& lhs_file == rhs_file
&& lhs_line == rhs_line
&& lhs_location == rhs_location
&& lhs_fields == rhs_fields
&& lhs_kind == rhs_kind
}
Expand Down
Loading