1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Logging and progress reporting.

use std::sync::{Arc, Mutex, Weak};

use console::style;
use nom::lib::std::fmt::Display;

use crate::progress::{FastProgressBar, ProgressTracker};
use chrono::Local;

/// Determines the size of the task tracked by ProgressTracker.
#[derive(Debug, Clone, Copy)]
pub enum ProgressBarLength {
    Items(u64),
    Bytes(u64),
    Unknown,
}

#[derive(Debug, Clone, Copy)]
pub enum LogLevel {
    Info,
    Warn,
    Error,
}

/// Common interface for logging diagnostics and progress.
pub trait Log: Sync + Send {
    /// Clears any previous progress bar or spinner and installs a new progress bar.
    fn progress_bar(&self, msg: &str, len: ProgressBarLength) -> Arc<dyn ProgressTracker>;

    /// Logs a message.
    fn log(&self, level: LogLevel, msg: String);
}

/// Additional convenience methods for logging.
pub trait LogExt {
    /// Logs an info message.
    fn info(&self, msg: impl Display);
    /// Logs an warning.
    fn warn(&self, msg: impl Display);
    /// Logs an error.
    fn err(&self, msg: impl Display);
}

/// Additional convenience methods for logging.
impl<L: Log + ?Sized> LogExt for L {
    /// Logs an info message.
    fn info(&self, msg: impl Display) {
        self.log(LogLevel::Info, msg.to_string())
    }

    /// Logs an warning.
    fn warn(&self, msg: impl Display) {
        self.log(LogLevel::Warn, msg.to_string())
    }

    /// Logs an error.
    fn err(&self, msg: impl Display) {
        self.log(LogLevel::Error, msg.to_string())
    }
}

/// A logger that uses standard error stream to communicate with the user.
pub struct StdLog {
    program_name: String,
    progress_bar: Mutex<Weak<FastProgressBar>>,
    pub log_stderr_to_stdout: bool,
    pub no_progress: bool,
}

impl StdLog {
    pub fn new() -> StdLog {
        StdLog {
            progress_bar: Mutex::new(Weak::default()),
            program_name: std::env::current_exe()
                .unwrap()
                .file_name()
                .unwrap()
                .to_string_lossy()
                .to_string(),
            log_stderr_to_stdout: false,
            no_progress: false,
        }
    }

    /// Clears any previous progress bar or spinner and installs a new spinner.
    pub fn spinner(&self, msg: &str) -> Arc<FastProgressBar> {
        if self.no_progress {
            return Arc::new(FastProgressBar::new_hidden());
        }
        self.progress_bar
            .lock()
            .unwrap()
            .upgrade()
            .iter()
            .for_each(|pb| pb.finish_and_clear());
        let result = Arc::new(FastProgressBar::new_spinner(msg));
        *self.progress_bar.lock().unwrap() = Arc::downgrade(&result);
        result
    }

    /// Clears any previous progress bar or spinner and installs a new progress bar.
    pub fn progress_bar(&self, msg: &str, len: u64) -> Arc<FastProgressBar> {
        if self.no_progress {
            return Arc::new(FastProgressBar::new_hidden());
        }
        let result = Arc::new(FastProgressBar::new_progress_bar(msg, len));
        *self.progress_bar.lock().unwrap() = Arc::downgrade(&result);
        result
    }

    /// Creates a no-op progressbar that doesn't display itself.
    pub fn hidden(&self) -> Arc<FastProgressBar> {
        Arc::new(FastProgressBar::new_hidden())
    }

    /// Clears any previous progress bar or spinner and installs a new progress bar.
    pub fn bytes_progress_bar(&self, msg: &str, len: u64) -> Arc<FastProgressBar> {
        if self.no_progress {
            return Arc::new(FastProgressBar::new_hidden());
        }
        self.progress_bar
            .lock()
            .unwrap()
            .upgrade()
            .iter()
            .for_each(|pb| pb.finish_and_clear());
        let result = Arc::new(FastProgressBar::new_bytes_progress_bar(msg, len));
        *self.progress_bar.lock().unwrap() = Arc::downgrade(&result);
        result
    }

    /// Prints a message to stderr.
    /// Does not interfere with progress bar.
    fn eprintln<I: Display>(&self, msg: I) {
        match self.progress_bar.lock().unwrap().upgrade() {
            Some(pb) if pb.is_visible() => pb.println(format!("{msg}")),
            _ if self.log_stderr_to_stdout => println!("{msg}"),
            _ => eprintln!("{msg}"),
        }
    }

    const TIMESTAMP_FMT: &'static str = "[%Y-%m-%d %H:%M:%S.%3f]";
}

impl Log for StdLog {
    fn progress_bar(&self, msg: &str, len: ProgressBarLength) -> Arc<dyn ProgressTracker> {
        match len {
            ProgressBarLength::Items(count) => self.progress_bar(msg, count),
            ProgressBarLength::Bytes(count) => self.bytes_progress_bar(msg, count),
            ProgressBarLength::Unknown => self.spinner(msg),
        }
    }

    fn log(&self, level: LogLevel, msg: String) {
        let timestamp = Local::now();
        let level = match level {
            LogLevel::Info => style(" info:").for_stderr().green(),
            LogLevel::Warn => style("warn:").for_stderr().yellow(),
            LogLevel::Error => style("error:").for_stderr().red(),
        };
        let msg = format!(
            "{} {}: {} {}",
            style(timestamp.format(Self::TIMESTAMP_FMT))
                .for_stderr()
                .dim()
                .white(),
            style(&self.program_name).for_stderr().yellow(),
            level,
            msg
        );
        self.eprintln(msg);
    }
}

impl Default for StdLog {
    fn default() -> Self {
        StdLog::new()
    }
}