Skip to content

Commit 8999a41

Browse files
authored
Merge branch 'master' into keyword-ident-fields
2 parents 3eb7a9a + 690a9a6 commit 8999a41

File tree

10 files changed

+93
-29
lines changed

10 files changed

+93
-29
lines changed

tracing-appender/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ default-features = false
3333
features = ["fmt", "std"]
3434

3535
[dev-dependencies]
36-
criterion = { version = "0.3.6", default_features = false }
36+
criterion = { version = "0.3.6", default-features = false }
3737
tracing = { path = "../tracing", version = "0.2" }
3838
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
3939
tempfile = "3.3.0"

tracing-core/src/field.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use crate::callsite;
3939
use core::{
4040
borrow::Borrow,
41-
fmt,
41+
fmt::{self, Write},
4242
hash::{Hash, Hasher},
4343
num,
4444
ops::Range,
@@ -224,6 +224,11 @@ pub trait Visit {
224224
self.record_debug(field, &value)
225225
}
226226

227+
/// Visit a byte slice.
228+
fn record_bytes(&mut self, field: &Field, value: &[u8]) {
229+
self.record_debug(field, &HexBytes(value))
230+
}
231+
227232
/// Records a type implementing `Error`.
228233
///
229234
/// <div class="example-wrap" style="display:inline-block">
@@ -283,6 +288,26 @@ where
283288
DebugValue(t)
284289
}
285290

291+
struct HexBytes<'a>(&'a [u8]);
292+
293+
impl<'a> fmt::Debug for HexBytes<'a> {
294+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295+
f.write_char('[')?;
296+
297+
let mut bytes = self.0.iter();
298+
299+
if let Some(byte) = bytes.next() {
300+
f.write_fmt(format_args!("{byte:02x}"))?;
301+
}
302+
303+
for byte in bytes {
304+
f.write_fmt(format_args!(" {byte:02x}"))?;
305+
}
306+
307+
f.write_char(']')
308+
}
309+
}
310+
286311
// ===== impl Visit =====
287312

288313
impl<'a, 'b> Visit for fmt::DebugStruct<'a, 'b> {
@@ -443,6 +468,14 @@ impl Value for str {
443468
}
444469
}
445470

471+
impl crate::sealed::Sealed for [u8] {}
472+
473+
impl Value for [u8] {
474+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
475+
visitor.record_bytes(key, self)
476+
}
477+
}
478+
446479
#[cfg(feature = "std")]
447480
impl crate::sealed::Sealed for dyn std::error::Error + 'static {}
448481

@@ -713,9 +746,9 @@ impl FieldSet {
713746
/// Returns the [`Field`] named `name`, or `None` if no such field exists.
714747
///
715748
/// [`Field`]: super::Field
716-
pub fn field<Q: ?Sized>(&self, name: &Q) -> Option<Field>
749+
pub fn field<Q>(&self, name: &Q) -> Option<Field>
717750
where
718-
Q: Borrow<str>,
751+
Q: Borrow<str> + ?Sized,
719752
{
720753
let name = &name.borrow();
721754
self.names.iter().position(|f| f == name).map(|i| Field {
@@ -1131,4 +1164,23 @@ mod test {
11311164
});
11321165
assert_eq!(result, format!("{}", err));
11331166
}
1167+
1168+
#[test]
1169+
fn record_bytes() {
1170+
let fields = TEST_META_1.fields();
1171+
let first = &b"abc"[..];
1172+
let second: &[u8] = &[192, 255, 238];
1173+
let values = &[
1174+
(&fields.field("foo").unwrap(), Some(&first as &dyn Value)),
1175+
(&fields.field("bar").unwrap(), Some(&" " as &dyn Value)),
1176+
(&fields.field("baz").unwrap(), Some(&second as &dyn Value)),
1177+
];
1178+
let valueset = fields.value_set(values);
1179+
let mut result = String::new();
1180+
valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| {
1181+
use core::fmt::Write;
1182+
write!(&mut result, "{:?}", value).unwrap();
1183+
});
1184+
assert_eq!(result, format!("{}", r#"[61 62 63]" "[c0 ff ee]"#));
1185+
}
11341186
}

tracing-subscriber/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ tracing = { path = "../tracing", version = "0.2" }
7070
tracing-mock = { path = "../tracing-mock", features = ["tracing-subscriber"] }
7171
log = "0.4.17"
7272
tracing-log = { path = "../tracing-log", version = "0.2" }
73-
criterion = { version = "0.3.6", default_features = false }
73+
criterion = { version = "0.3.6", default-features = false }
7474
regex = { version = "1.6.0", default-features = false, features = ["std"] }
7575
tracing-futures = { path = "../tracing-futures", version = "0.3", default-features = false, features = ["std-future", "std"] }
7676
tokio = { version = "1.20.0", features = ["rt", "macros"] }

tracing-subscriber/src/filter/env/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl fmt::Display for ValueMatch {
267267
match self {
268268
ValueMatch::Bool(ref inner) => fmt::Display::fmt(inner, f),
269269
ValueMatch::F64(ref inner) => fmt::Display::fmt(inner, f),
270-
ValueMatch::NaN => fmt::Display::fmt(&std::f64::NAN, f),
270+
ValueMatch::NaN => fmt::Display::fmt(&f64::NAN, f),
271271
ValueMatch::I64(ref inner) => fmt::Display::fmt(inner, f),
272272
ValueMatch::U64(ref inner) => fmt::Display::fmt(inner, f),
273273
ValueMatch::Debug(ref inner) => fmt::Display::fmt(inner, f),
@@ -507,7 +507,7 @@ impl<'a> Visit for MatchVisitor<'a> {
507507
matched.store(true, Release);
508508
}
509509
Some((ValueMatch::F64(ref e), ref matched))
510-
if (value - *e).abs() < std::f64::EPSILON =>
510+
if (value - *e).abs() < f64::EPSILON =>
511511
{
512512
matched.store(true, Release);
513513
}

tracing-subscriber/src/fmt/format/json.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ impl<'a> field::Visit for JsonVisitor<'a> {
488488
.insert(field.name(), serde_json::Value::from(value));
489489
}
490490

491+
fn record_bytes(&mut self, field: &Field, value: &[u8]) {
492+
self.values
493+
.insert(field.name(), serde_json::Value::from(value));
494+
}
495+
491496
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
492497
match field.name() {
493498
// Skip fields that are actually log metadata that have already been handled
@@ -528,13 +533,19 @@ mod test {
528533
#[test]
529534
fn json() {
530535
let expected =
531-
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n";
536+
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":[97,98,99]},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":[97,98,99]}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n";
532537
let collector = collector()
533538
.flatten_event(false)
534539
.with_current_span(true)
535540
.with_span_list(true);
536541
test_json(expected, collector, || {
537-
let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3);
542+
let span = tracing::span!(
543+
tracing::Level::INFO,
544+
"json_span",
545+
answer = 42,
546+
number = 3,
547+
slice = &b"abc"[..]
548+
);
538549
let _guard = span.enter();
539550
tracing::info!("some json test");
540551
});

tracing-subscriber/src/fmt/time/datetime.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ impl From<std::time::SystemTime> for DateTime {
247247
fn from(timestamp: std::time::SystemTime) -> DateTime {
248248
let (t, nanos) = match timestamp.duration_since(std::time::UNIX_EPOCH) {
249249
Ok(duration) => {
250-
debug_assert!(duration.as_secs() <= std::i64::MAX as u64);
250+
debug_assert!(duration.as_secs() <= i64::MAX as u64);
251251
(duration.as_secs() as i64, duration.subsec_nanos())
252252
}
253253
Err(error) => {
254254
let duration = error.duration();
255-
debug_assert!(duration.as_secs() <= std::i64::MAX as u64);
255+
debug_assert!(duration.as_secs() <= i64::MAX as u64);
256256
let (secs, nanos) = (duration.as_secs() as i64, duration.subsec_nanos());
257257
if nanos == 0 {
258258
(-secs, 0)
@@ -332,7 +332,6 @@ impl From<std::time::SystemTime> for DateTime {
332332

333333
#[cfg(test)]
334334
mod tests {
335-
use std::i32;
336335
use std::time::{Duration, UNIX_EPOCH};
337336

338337
use super::*;
@@ -382,8 +381,8 @@ mod tests {
382381
1,
383382
);
384383

385-
case("2038-01-19T03:14:07.000000Z", std::i32::MAX as i64, 0);
386-
case("2038-01-19T03:14:08.000000Z", std::i32::MAX as i64 + 1, 0);
384+
case("2038-01-19T03:14:07.000000Z", i32::MAX as i64, 0);
385+
case("2038-01-19T03:14:08.000000Z", i32::MAX as i64 + 1, 0);
387386
case("1901-12-13T20:45:52.000000Z", i32::MIN as i64, 0);
388387
case("1901-12-13T20:45:51.000000Z", i32::MIN as i64 - 1, 0);
389388

@@ -392,8 +391,8 @@ mod tests {
392391
// high date value tests to panic
393392
#[cfg(not(target_os = "windows"))]
394393
{
395-
case("+292277026596-12-04T15:30:07.000000Z", std::i64::MAX, 0);
396-
case("+292277026596-12-04T15:30:06.000000Z", std::i64::MAX - 1, 0);
394+
case("+292277026596-12-04T15:30:07.000000Z", i64::MAX, 0);
395+
case("+292277026596-12-04T15:30:06.000000Z", i64::MAX - 1, 0);
397396
case("-292277022657-01-27T08:29:53.000000Z", i64::MIN + 1, 0);
398397
}
399398

tracing-subscriber/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ feature! {
210210
#![all(feature = "registry", feature = "std")]
211211
pub use registry::Registry;
212212

213+
/// Creates a default [`Registry`], a [`Collect`](tracing_core::Collect)
214+
/// implementation which tracks per-span data and exposes it to
215+
/// [`Subscribe`]s.
213216
///
217+
/// For more information see [`Registry`].
214218
pub fn registry() -> Registry {
215219
Registry::default()
216220
}

tracing-subscriber/src/registry/sharded.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl Collect for Registry {
348348

349349
let refs = span.ref_count.fetch_sub(1, Ordering::Release);
350350
if !std::thread::panicking() {
351-
assert!(refs < std::usize::MAX, "reference count overflow!");
351+
assert!(refs < usize::MAX, "reference count overflow!");
352352
}
353353
if refs > 1 {
354354
return false;
@@ -541,10 +541,6 @@ mod tests {
541541
Collect,
542542
};
543543

544-
#[derive(Debug)]
545-
struct DoesNothing;
546-
impl<C: Collect> Subscribe<C> for DoesNothing {}
547-
548544
struct AssertionSubscriber;
549545
impl<C> Subscribe<C> for AssertionSubscriber
550546
where

tracing/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ tracing-attributes = { path = "../tracing-attributes", version = "0.2", optional
3737
pin-project-lite = "0.2.9"
3838

3939
[dev-dependencies]
40-
criterion = { version = "0.3.6", default_features = false }
41-
futures = { version = "0.3.21", default_features = false }
40+
criterion = { version = "0.3.6", default-features = false }
41+
futures = { version = "0.3.21", default-features = false }
4242
log = "0.4.17"
4343
tracing-mock = { path = "../tracing-mock" }
4444

tracing/src/span.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,19 +1105,19 @@ impl Span {
11051105

11061106
/// Returns a [`Field`](super::field::Field) for the field with the
11071107
/// given `name`, if one exists,
1108-
pub fn field<Q: ?Sized>(&self, field: &Q) -> Option<field::Field>
1108+
pub fn field<Q>(&self, field: &Q) -> Option<field::Field>
11091109
where
1110-
Q: field::AsField,
1110+
Q: field::AsField + ?Sized,
11111111
{
11121112
self.metadata().and_then(|meta| field.as_field(meta))
11131113
}
11141114

11151115
/// Returns true if this `Span` has a field for the given
11161116
/// [`Field`](super::field::Field) or field name.
11171117
#[inline]
1118-
pub fn has_field<Q: ?Sized>(&self, field: &Q) -> bool
1118+
pub fn has_field<Q>(&self, field: &Q) -> bool
11191119
where
1120-
Q: field::AsField,
1120+
Q: field::AsField + ?Sized,
11211121
{
11221122
self.field(field).is_some()
11231123
}
@@ -1193,9 +1193,9 @@ impl Span {
11931193
///
11941194
/// [`field::Empty`]: super::field::Empty
11951195
/// [`Metadata`]: super::Metadata
1196-
pub fn record<Q: ?Sized, V>(&self, field: &Q, value: V) -> &Self
1196+
pub fn record<Q, V>(&self, field: &Q, value: V) -> &Self
11971197
where
1198-
Q: field::AsField,
1198+
Q: field::AsField + ?Sized,
11991199
V: field::Value,
12001200
{
12011201
if let Some(meta) = self.meta {
@@ -1594,9 +1594,11 @@ unsafe impl Sync for PhantomNotSend {}
15941594
mod test {
15951595
use super::*;
15961596

1597+
#[allow(dead_code)]
15971598
trait AssertSend: Send {}
15981599
impl AssertSend for Span {}
15991600

1601+
#[allow(dead_code)]
16001602
trait AssertSync: Sync {}
16011603
impl AssertSync for Span {}
16021604
impl AssertSync for Entered<'_> {}

0 commit comments

Comments
 (0)