Skip to content

Commit

Permalink
Add a fallback for when TLS is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
divergentdave committed Oct 16, 2019
1 parent 29b9830 commit 477f68f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ impl Log for Logger {
static FORMATTER: RefCell<Option<Formatter>> = RefCell::new(None);
}

FORMATTER.with(|tl_buf| {
let tls_result = FORMATTER.try_with(|tl_buf| {
// It's possible for implementations to sometimes
// log-while-logging (e.g. a `std::fmt` implementation logs
// internally) but it's super rare. If this happens make sure we
Expand Down Expand Up @@ -842,6 +842,15 @@ impl Log for Logger {
// Always clear the buffer afterwards
formatter.clear();
});

if tls_result.is_err() {
// The thread-local storage was not available (because its
// destructor has already run). Create a new single-use
// Formatter on the stack for this call.
let mut formatter = Formatter::new(&self.writer);
let _ = (self.format)(&mut formatter, record)
.and_then(|_| formatter.print(&self.writer));
}
}
}

Expand Down

0 comments on commit 477f68f

Please sign in to comment.