Skip to content
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

Add column numbers #581

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion src/__private_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
use crate::{Level, Metadata, Record};
use std::fmt::Arguments;
pub use std::option::Option;
pub use std::{file, format_args, line, module_path, stringify};
pub use std::{column, file, format_args, line, module_path, stringify};

#[cfg(not(feature = "kv_unstable"))]
pub fn log(
args: Arguments,
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
line: u32,
column: u32,
kvs: Option<&[(&str, &str)]>,
) {
if kvs.is_some() {
Expand All @@ -27,6 +28,7 @@ pub fn log(
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.column(Some(column))
.build(),
);
}
Expand All @@ -37,6 +39,7 @@ pub fn log(
level: Level,
&(target, module_path, file): &(&str, &'static str, &'static str),
line: u32,
column: u32,
kvs: Option<&[(&str, &dyn crate::kv::ToValue)]>,
) {
crate::logger().log(
Expand All @@ -47,6 +50,7 @@ pub fn log(
.module_path_static(Some(module_path))
.file_static(Some(file))
.line(Some(line))
.column(Some(column))
.key_values(&kvs)
.build(),
);
Expand Down
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ pub struct Record<'a> {
module_path: Option<MaybeStaticStr<'a>>,
file: Option<MaybeStaticStr<'a>>,
line: Option<u32>,
column: Option<u32>,
#[cfg(feature = "kv_unstable")]
key_values: KeyValues<'a>,
}
Expand Down Expand Up @@ -812,6 +813,12 @@ impl<'a> Record<'a> {
self.line
}

/// The column containing the message.
#[inline]
pub fn column(&self) -> Option<u32> {
self.column
}

/// The structured key-value pairs associated with the message.
#[cfg(feature = "kv_unstable")]
#[inline]
Expand All @@ -833,6 +840,7 @@ impl<'a> Record<'a> {
module_path: self.module_path,
file: self.file,
line: self.line,
column: self.column,
key_values: self.key_values.clone(),
},
}
Expand All @@ -856,6 +864,7 @@ impl<'a> Record<'a> {
/// .target("myApp")
/// .file(Some("server.rs"))
/// .line(Some(144))
/// .column(Some(12))
/// .module_path(Some("server"))
/// .build();
/// ```
Expand All @@ -874,6 +883,7 @@ impl<'a> Record<'a> {
/// .metadata(error_metadata)
/// .args(format_args!("Error!"))
/// .line(Some(433))
/// .column(Some(22))
/// .file(Some("app.rs"))
/// .module_path(Some("server"))
/// .build();
Expand All @@ -893,6 +903,7 @@ impl<'a> RecordBuilder<'a> {
/// - `module_path`: `None`
/// - `file`: `None`
/// - `line`: `None`
/// - `column`: `None`
///
/// [`format_args!("")`]: https://doc.rust-lang.org/std/macro.format_args.html
/// [`Metadata::builder().build()`]: struct.MetadataBuilder.html#method.build
Expand All @@ -905,6 +916,7 @@ impl<'a> RecordBuilder<'a> {
module_path: None,
file: None,
line: None,
column: None,
#[cfg(feature = "kv_unstable")]
key_values: KeyValues(&Option::None::<(kv::Key, kv::Value)>),
},
Expand Down Expand Up @@ -974,6 +986,13 @@ impl<'a> RecordBuilder<'a> {
self
}

/// Set [`column`](struct.Record.html#method.column)
#[inline]
pub fn column(&mut self, column: Option<u32>) -> &mut RecordBuilder<'a> {
self.record.column = column;
self
}

/// Set [`key_values`](struct.Record.html#method.key_values)
#[cfg(feature = "kv_unstable")]
#[inline]
Expand Down Expand Up @@ -1700,11 +1719,13 @@ mod tests {
.module_path(Some("foo"))
.file(Some("bar"))
.line(Some(30))
.column(Some(12))
.build();
assert_eq!(record_test.metadata().target(), "myApp");
assert_eq!(record_test.module_path(), Some("foo"));
assert_eq!(record_test.file(), Some("bar"));
assert_eq!(record_test.line(), Some(30));
assert_eq!(record_test.column(), Some(12));
}

#[test]
Expand All @@ -1719,11 +1740,13 @@ mod tests {
.module_path(Some("foo"))
.file(Some("bar"))
.line(Some(30))
.column(Some(12))
.build();
assert_eq!(record_test.target(), "myApp");
assert_eq!(record_test.module_path(), Some("foo"));
assert_eq!(record_test.file(), Some("bar"));
assert_eq!(record_test.line(), Some(30));
assert_eq!(record_test.column(), Some(12));
}

#[test]
Expand All @@ -1734,6 +1757,7 @@ mod tests {
.module_path(Some("foo"))
.file(Some("bar"))
.line(Some(30))
.column(Some(12))
.target(target)
.level(Level::Error)
.build();
Expand All @@ -1742,6 +1766,7 @@ mod tests {
assert_eq!(record_test.module_path(), Some("foo"));
assert_eq!(record_test.file(), Some("bar"));
assert_eq!(record_test.line(), Some(30));
assert_eq!(record_test.column(), Some(12));
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ macro_rules! log {
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::column!(),
$crate::__private_api::Option::Some(&[$(($crate::__log_key!($key), &$value)),+])
);
}
Expand All @@ -52,6 +53,7 @@ macro_rules! log {
lvl,
&($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
$crate::__private_api::line!(),
$crate::__private_api::column!(),
$crate::__private_api::Option::None,
);
}
Expand Down