88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- //! A simple logger configured via an environment variable which writes
11+ //! A simple logger configured via environment variables which writes
1212//! to stdout or stderr, for use with the logging facade exposed by the
1313//! [`log` crate][log-crate-url].
1414//!
@@ -426,7 +426,7 @@ impl Builder {
426426 /// This method is kept private because the only way we support building
427427 /// loggers is by installing them as the single global logger for the
428428 /// `log` crate.
429- fn build ( & mut self ) -> Logger {
429+ pub fn build ( & mut self ) -> Logger {
430430 Logger {
431431 writer : self . writer . build ( ) ,
432432 filter : self . filter . build ( ) ,
@@ -460,11 +460,9 @@ impl Log for Logger {
460460 // so will always at least have capacity for the largest log record formatted
461461 // on that thread.
462462 //
463- // Because these buffers are tied to a particular logger, we don't let callers
464- // create instances of `Logger` themselves, or they'll race to configure the
465- // thread local buffer with their own configuration. This is still potentially
466- // an issue if a caller attempts to set and use the global logger multiple times,
467- // but in that case it's clearer that there's shared state at play.
463+ // If multiple `Logger`s are used by the same threads then the thread-local
464+ // formatter might have different color support. If this is the case the
465+ // formatter and its buffer are discarded and recreated.
468466
469467 thread_local ! {
470468 static FORMATTER : RefCell <Option <Formatter >> = RefCell :: new( None ) ;
@@ -473,8 +471,15 @@ impl Log for Logger {
473471 FORMATTER . with ( |tl_buf| {
474472 let mut tl_buf = tl_buf. borrow_mut ( ) ;
475473
476- if tl_buf. is_none ( ) {
477- * tl_buf = Some ( Formatter :: new ( & self . writer ) ) ;
474+ // Check the buffer style. If it's different from the logger's
475+ // style then drop the buffer and recreate it.
476+ match * tl_buf {
477+ Some ( ref mut formatter) => {
478+ if formatter. write_style ( ) != self . writer . write_style ( ) {
479+ * formatter = Formatter :: new ( & self . writer )
480+ }
481+ } ,
482+ ref mut tl_buf => * tl_buf = Some ( Formatter :: new ( & self . writer ) )
478483 }
479484
480485 // The format is guaranteed to be `Some` by this point
0 commit comments