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

Code before:

    fn flush(&self) {}
}

fn create_log_message(record: &log::Record<'_>, target: &str) -> String {
    let now = time::strftime("%F %T.%f", &time::now()).unwrap();
    let mut message = format!("[{}] [{:<5}] [{}] ", now, record.level(), target);

    // Alignment for the lines after the first new line character in the payload. We don't deal
    // with fullwidth/unicode chars here, so just `message.len()` is sufficient.
    let alignment = message.len();

    // Push lines with added extra padding on the next line, which is trimmed later.
    let lines = record.args().to_string();
    for line in lines.split('\n') {
        let line = format!("{}\n{:width$}", line, "", width = alignment);
        message.push_str(&line);
    }

    // Drop extra trailing alignment.
    message.truncate(message.len() - alignment);
    message
}

fn is_allowed_target(level: Level, target: &str) -> bool {
    use Level::{Error, Warn};
    use LevelFilter::Trace;

    match (level, log::max_level()) {
        (Error, Trace) | (Warn, Trace) => true,
        _ => ALLOWED_TARGETS.contains(&target),
    }
}

struct OnDemandLogFile {
    file: Option<LineWriter<File>>,
    created: Arc<AtomicBool>,
    path: PathBuf,
}

impl OnDemandLogFile {
    fn new() -> Self {
        let mut path = env::temp_dir();
        path.push(format!("Alacritty-{}.log", process::id()));

        // Set log path as an environment variable.
        env::set_var(ALACRITTY_LOG_ENV, path.as_os_str());

        OnDemandLogFile { path, file: None, created: Arc::new(AtomicBool::new(false)) }
    }


Review comment (suggestion):
```suggestion
/// Check if log messages from a crate should be logged.
fn is_allowed_target(level: Level, target: &str) -> bool {
```

Wording might be a bit awkward, but I think a little comment would be nice in case it's not immediately obvious what "allowed target" means in this context.

Code after:

    fn flush(&self) {}
}

fn create_log_message(record: &log::Record<'_>, target: &str) -> String {
    let now = time::strftime("%F %T.%f", &time::now()).unwrap();
    let mut message = format!("[{}] [{:<5}] [{}] ", now, record.level(), target);

    // Alignment for the lines after the first new line character in the payload. We don't deal
    // with fullwidth/unicode chars here, so just `message.len()` is sufficient.
    let alignment = message.len();

    // Push lines with added extra padding on the next line, which is trimmed later.
    let lines = record.args().to_string();
    for line in lines.split('\n') {
        let line = format!("{}\n{:width$}", line, "", width = alignment);
        message.push_str(&line);
    }

    // Drop extra trailing alignment.
    message.truncate(message.len() - alignment);
    message
}

/// Check if log messages from a crate should be logged.
fn is_allowed_target(level: Level, target: &str) -> bool {
    match (level, log::max_level()) {
        (Level::Error, LevelFilter::Trace) | (Level::Warn, LevelFilter::Trace) => true,
        _ => ALLOWED_TARGETS.contains(&target),
    }
}

struct OnDemandLogFile {
    file: Option<LineWriter<File>>,
    created: Arc<AtomicBool>,
    path: PathBuf,
}

impl OnDemandLogFile {
    fn new() -> Self {
        let mut path = env::temp_dir();
        path.push(format!("Alacritty-{}.log", process::id()));

        // Set log path as an environment variable.
        env::set_var(ALACRITTY_LOG_ENV, path.as_os_str());

        OnDemandLogFile { path, file: None, created: Arc::new(AtomicBool::new(false)) }
    }

    fn file(&mut self) -> Result<&mut LineWriter<File>, io::Error> {
        // Allow to recreate the file if it has been deleted at runtime.