|
1 |
| -use crate::DeferredNow; |
2 | 1 | #[cfg(feature = "kv")]
|
3 | 2 | use log::kv::{self, Key, Value, VisitSource};
|
4 |
| -use log::Record; |
5 |
| -#[cfg(feature = "colors")] |
6 |
| -use nu_ansi_term::{Color, Style}; |
7 | 3 | #[cfg(feature = "json")]
|
8 | 4 | use serde_derive::Serialize;
|
9 | 5 | #[cfg(feature = "kv")]
|
10 | 6 | use std::collections::BTreeMap;
|
| 7 | + |
11 | 8 | #[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}; |
14 | 16 |
|
15 | 17 | /// Time stamp format that is used by the provided format functions.
|
16 | 18 | 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 {
|
374 | 376 |
|
375 | 377 | // Overwrites the default PALETTE value either from the environment, if set,
|
376 | 378 | // 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. |
378 | 380 | #[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 | + }) |
398 | 397 | }
|
399 | 398 |
|
400 | 399 | /// Helper function that is used in the provided coloring format functions to apply
|
@@ -432,7 +431,7 @@ const fn default_style() -> Style {
|
432 | 431 | }
|
433 | 432 | }
|
434 | 433 | #[cfg(feature = "colors")]
|
435 |
| -#[derive(Debug)] |
| 434 | +#[derive(Copy, Clone, Debug, PartialEq)] |
436 | 435 | struct Palette {
|
437 | 436 | pub error: Style,
|
438 | 437 | pub warn: Style,
|
|
0 commit comments