Skip to content

Commit 02805ad

Browse files
committed
Simplify error handling around Palette
1 parent 7a858ff commit 02805ad

File tree

5 files changed

+35
-38
lines changed

5 files changed

+35
-38
lines changed

src/error_info.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
//!
4646
//! If this happens with one of `flexi_logger`s provided format functions, please open an issue.
4747
//!
48-
//! ## `Palette`
49-
//!
50-
//! This error is unexpected - please open an issue and describe your setup.
51-
//!
5248
//! ## `Poison`
5349
//!
5450
//! Log entries can be written by all threads of your program. Loggers thus must be thread-safe,

src/flexi_error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ pub enum FlexiLoggerError {
8080
Poison,
8181

8282
/// Palette parsing failed
83+
#[cfg(feature = "colors")]
8384
#[error("Palette parsing failed")]
8485
Palette(#[from] std::num::ParseIntError),
8586

87+
/// Repeated palette initialization failed
88+
#[cfg(feature = "colors")]
89+
#[error("Repeated palette initialization failed")]
90+
RepeatedPaletteInitialization,
91+
8692
/// Logger is shut down.
8793
#[cfg(feature = "async")]
8894
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]

src/formats.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
use crate::DeferredNow;
21
#[cfg(feature = "kv")]
32
use log::kv::{self, Key, Value, VisitSource};
4-
use log::Record;
5-
#[cfg(feature = "colors")]
6-
use nu_ansi_term::{Color, Style};
73
#[cfg(feature = "json")]
84
use serde_derive::Serialize;
95
#[cfg(feature = "kv")]
106
use std::collections::BTreeMap;
7+
118
#[cfg(feature = "colors")]
12-
use std::sync::OnceLock;
13-
use std::thread;
9+
use {
10+
crate::FlexiLoggerError,
11+
nu_ansi_term::{Color, Style},
12+
std::sync::OnceLock,
13+
};
14+
15+
use {crate::DeferredNow, log::Record, std::thread};
1416

1517
/// Time stamp format that is used by the provided format functions.
1618
pub const TS_DASHES_BLANK_COLONS_DOT_BLANK: &str = "%Y-%m-%d %H:%M:%S%.6f %:z";
@@ -374,27 +376,24 @@ fn palette() -> &'static Palette {
374376

375377
// Overwrites the default PALETTE value either from the environment, if set,
376378
// or from the parameter, if filled.
377-
// Returns an error if parsing failed.
379+
// Returns an error if parsing failed, or if repeated initialization is attempted.
378380
#[cfg(feature = "colors")]
379-
pub(crate) fn set_palette(input: Option<&str>) -> Result<(), std::num::ParseIntError> {
380-
use crate::util::{eprint_msg, ErrorCode};
381-
382-
PALETTE
383-
.set(match std::env::var_os("FLEXI_LOGGER_PALETTE") {
384-
Some(ref env_osstring) => Palette::from(env_osstring.to_string_lossy().as_ref())?,
385-
None => match input {
386-
Some(input_string) => Palette::from(input_string)?,
387-
None => DEFAULT_PALETTE,
388-
},
389-
})
390-
.map_err(|_palette| {
391-
eprint_msg(
392-
ErrorCode::Palette,
393-
"Failed to initialize the palette, as it is already initialized",
394-
);
395-
})
396-
.ok();
397-
Ok(())
381+
pub(crate) fn set_palette(o_palette_string: Option<&str>) -> Result<(), FlexiLoggerError> {
382+
let palette = match std::env::var_os("FLEXI_LOGGER_PALETTE") {
383+
Some(ref env_osstring) => Palette::from(env_osstring.to_string_lossy().as_ref())?,
384+
None => match o_palette_string {
385+
Some(palette_string) => Palette::from(palette_string)?,
386+
None => DEFAULT_PALETTE,
387+
},
388+
};
389+
390+
PALETTE.set(palette).or_else(|old_palette| {
391+
if old_palette == palette {
392+
Ok(())
393+
} else {
394+
Err(FlexiLoggerError::RepeatedPaletteInitialization)
395+
}
396+
})
398397
}
399398

400399
/// Helper function that is used in the provided coloring format functions to apply
@@ -432,7 +431,7 @@ const fn default_style() -> Style {
432431
}
433432
}
434433
#[cfg(feature = "colors")]
435-
#[derive(Debug)]
434+
#[derive(Copy, Clone, Debug, PartialEq)]
436435
struct Palette {
437436
pub error: Style,
438437
pub warn: Style,

src/logger.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,14 @@ impl Logger {
707707
///
708708
/// Several variants of [`FlexiLoggerError`] can occur.
709709
pub fn build(mut self) -> Result<(Box<dyn log::Log>, LoggerHandle), FlexiLoggerError> {
710-
#[cfg(feature = "colors")]
711-
set_palette(self.o_palette.as_deref())?;
712-
713710
if self.use_utc {
714711
self.flwb = self.flwb.use_utc();
715712
}
716713
set_panic_on_error_channel_error(self.panic_on_error_channel_error);
717714

715+
#[cfg(feature = "colors")]
716+
set_palette(self.o_palette.as_deref())?;
717+
718718
let a_primary_writer = Arc::new(match self.log_target {
719719
LogTarget::StdOut => {
720720
if let WriteMode::SupportCapture = self.flwb.get_write_mode() {

src/util.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ pub(crate) enum ErrorCode {
2626
LogFile,
2727
#[cfg(feature = "specfile")]
2828
LogSpecFile,
29-
#[cfg(feature = "colors")]
30-
Palette,
3129
Poison,
3230
#[cfg(target_family = "unix")]
3331
Symlink,
@@ -42,8 +40,6 @@ impl ErrorCode {
4240
Self::LogFile => "logfile",
4341
#[cfg(feature = "specfile")]
4442
Self::LogSpecFile => "logspecfile",
45-
#[cfg(feature = "colors")]
46-
Self::Palette => "palette",
4743
Self::Poison => "poison",
4844
#[cfg(target_family = "unix")]
4945
Self::Symlink => "symlink",

0 commit comments

Comments
 (0)