Skip to content

Commit 477f68f

Browse files
committed
Add a fallback for when TLS is unavailable
1 parent 29b9830 commit 477f68f

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ impl Log for Logger {
807807
static FORMATTER: RefCell<Option<Formatter>> = RefCell::new(None);
808808
}
809809

810-
FORMATTER.with(|tl_buf| {
810+
let tls_result = FORMATTER.try_with(|tl_buf| {
811811
// It's possible for implementations to sometimes
812812
// log-while-logging (e.g. a `std::fmt` implementation logs
813813
// internally) but it's super rare. If this happens make sure we
@@ -842,6 +842,15 @@ impl Log for Logger {
842842
// Always clear the buffer afterwards
843843
formatter.clear();
844844
});
845+
846+
if tls_result.is_err() {
847+
// The thread-local storage was not available (because its
848+
// destructor has already run). Create a new single-use
849+
// Formatter on the stack for this call.
850+
let mut formatter = Formatter::new(&self.writer);
851+
let _ = (self.format)(&mut formatter, record)
852+
.and_then(|_| formatter.print(&self.writer));
853+
}
845854
}
846855
}
847856

0 commit comments

Comments
 (0)