Repository: alacritty/alacritty
File: alacritty/src/logging.rs (Rust)

Code before:
/// Name for the environment variable containing the log file's path.
const ALACRITTY_LOG_ENV: &str = "ALACRITTY_LOG";

/// Name for the environment variable containing extra logging targets.
///
/// The targets are semicolon separated.
const ALACRITTY_EXTRA_LOG_TARGETS_ENV: &str = "ALACRITTY_EXTRA_LOG_TARGETS";

/// User configurable extra log targets to include.
static EXTRA_LOG_TARGETS: Lazy<Vec<String>> = Lazy::new(|| {
    env::var(ALACRITTY_EXTRA_LOG_TARGETS_ENV)
        .map_or(Vec::new(), |targets| targets.split(';').map(ToString::to_string).collect())
});

/// List of targets which will be logged by Alacritty.
const ALLOWED_TARGETS: &[&str] = &[
    LOG_TARGET_IPC_CONFIG,
    LOG_TARGET_CONFIG,
    "alacritty_config_derive",
    "alacritty_terminal",
    "alacritty",
    "crossfont",
];

/// Track whether logging has been initialized yet.
static mut LOGGING_INITIALIZED: bool = false;

/// Initialize the logger to its defaults.
pub fn initialize(
    options: &Options,
    event_proxy: EventLoopProxy<Event>,
) -> Result<Option<PathBuf>, log::SetLoggerError> {
    log::set_max_level(options.log_level());

    let logger = Logger::new(event_proxy);
    let path = logger.file_path();
    log::set_boxed_logger(Box::new(logger))?;

    unsafe { LOGGING_INITIALIZED = true };

    Ok(path)
}

/// Check if logging has been initialized.
pub fn is_initialized() -> bool {
    unsafe { LOGGING_INITIALIZED }
}

pub struct Logger {
    logfile: Mutex<OnDemandLogFile>,
    stdout: Mutex<LineWriter<Stdout>>,

Review comment (suggestion):
nothing stops you from using `OnceCell`/`Atomic` instead of unsafe.

Code after:

/// Logging target for config error messages.
pub const LOG_TARGET_CONFIG: &str = "alacritty_config_derive";

/// Name for the environment variable containing extra logging targets.
///
/// The targets are semicolon separated.
const ALACRITTY_EXTRA_LOG_TARGETS_ENV: &str = "ALACRITTY_EXTRA_LOG_TARGETS";

/// User configurable extra log targets to include.
static EXTRA_LOG_TARGETS: Lazy<Vec<String>> = Lazy::new(|| {
    env::var(ALACRITTY_EXTRA_LOG_TARGETS_ENV)
        .map_or(Vec::new(), |targets| targets.split(';').map(ToString::to_string).collect())
});

/// List of targets which will be logged by Alacritty.
const ALLOWED_TARGETS: &[&str] = &[
    LOG_TARGET_IPC_CONFIG,
    LOG_TARGET_CONFIG,
    "alacritty_config_derive",
    "alacritty_terminal",
    "alacritty",
    "crossfont",
];

/// Initialize the logger to its defaults.
pub fn initialize(
    options: &Options,
    event_proxy: EventLoopProxy<Event>,
) -> Result<Option<PathBuf>, log::SetLoggerError> {
    log::set_max_level(options.log_level());

    let logger = Logger::new(event_proxy);
    let path = logger.file_path();
    log::set_boxed_logger(Box::new(logger))?;

    Ok(path)
}

pub struct Logger {
    logfile: Mutex<OnDemandLogFile>,
    stdout: Mutex<LineWriter<Stdout>>,
    event_proxy: Mutex<EventLoopProxy<Event>>,
    start: Instant,
}

impl Logger {
    fn new(event_proxy: EventLoopProxy<Event>) -> Self {
        let logfile = Mutex::new(OnDemandLogFile::new());
        let stdout = Mutex::new(LineWriter::new(io::stdout()));