tracing: Log field values that aren't valid UTF-8

This ensures that we retain as much logged information as possible,
while still enabling us to identify field values that cause problems.
This commit is contained in:
Jack Grigg 2020-08-06 11:11:09 +01:00
parent ba831f814a
commit c87205e5e0
1 changed files with 54 additions and 77 deletions

View File

@ -490,23 +490,12 @@ pub extern "C" fn tracing_log(
let meta = callsite.metadata();
assert!(meta.is_event());
if level_enabled!(*meta.level()) {
if callsite.is_enabled() {
/// Dispatch the event.
///
/// We wrap this in a function so that we can use `?` to collect and raise
/// UTF-8 errors without allocating. We use a function instead of a closure
/// so that we can force it to be inlined.
#[inline(always)]
fn dispatch_event(
meta: &'static Metadata,
field_values: &[*const c_char],
) -> Result<(), str::Utf8Error> {
if level_enabled!(*meta.level()) && callsite.is_enabled() {
let mut fi = meta.fields().iter();
let mut vi = field_values
.iter()
.map(|&p| unsafe { CStr::from_ptr(p) })
.map(|cs| cs.to_str());
.map(|cs| cs.to_string_lossy());
macro_rules! dispatch {
($n:tt) => {
@ -516,7 +505,7 @@ pub extern "C" fn tracing_log(
$n,
(
&fi.next().unwrap(),
Some(&vi.next().unwrap()? as &dyn Value)
Some(&vi.next().unwrap().as_ref() as &dyn Value)
)
)),
)
@ -559,17 +548,5 @@ pub extern "C" fn tracing_log(
32 => dispatch!(32),
_ => unimplemented!(),
}
Ok(())
}
if let Err(e) = dispatch_event(meta, field_values) {
tracing::error!(
"{}: Field value was not valid UTF-8: {}",
callsite.metadata().name(),
e
);
}
}
}
}