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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Provides logging functionality.

use std::fs::{File, OpenOptions};
use std::io::Write;
use std::str::FromStr;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};

use humphrey::http::date::DateTime;
use humphrey::monitor::event::{Event, EventType, ToEventMask};

use crate::config::Config;
use crate::AppState;

/// Event mask for the `LogLevel::Error` log level.
pub const INTERNAL_MASK_ERROR: u32 = EventType::ThreadPoolPanic as u32;

/// Event mask for the `LogLevel::Warn` log level.
pub const INTERNAL_MASK_WARN: u32 = INTERNAL_MASK_ERROR
    | EventType::RequestServedError as u32
    | EventType::RequestTimeout as u32
    | EventType::StreamDisconnectedWhileWaiting as u32
    | EventType::ThreadPoolOverload as u32
    | EventType::ThreadRestarted as u32;

/// Event mask for the `LogLevel::Info` log level.
pub const INTERNAL_MASK_INFO: u32 = INTERNAL_MASK_WARN | EventType::HTTPSRedirect as u32;

/// Event mask for the `LogLevel::Debug` log level.
pub const INTERNAL_MASK_DEBUG: u32 = INTERNAL_MASK_INFO
    | EventType::KeepAliveRespected as u32
    | EventType::ThreadPoolProcessStarted as u32
    | EventType::ConnectionSuccess as u32
    | EventType::ConnectionClosed as u32;

/// Encapsulates logging methods and configuration.
pub struct Logger {
    level: LogLevel,
    console: bool,
    file: Option<Mutex<File>>,
}

/// Represents a log level.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum LogLevel {
    /// Only errors will be logged.
    Error,
    /// Errors and warnings will be logged.
    Warn,
    /// Errors, warnings and general information will be logged.
    Info,
    /// Everything, including debug information, will be logged.
    Debug,
}

impl Default for Logger {
    fn default() -> Self {
        Self {
            level: LogLevel::Warn,
            console: true,
            file: None,
        }
    }
}

impl From<&Config> for Logger {
    fn from(config: &Config) -> Self {
        let file = config.logging.file.as_ref().map(|path| {
            Mutex::new(
                OpenOptions::new()
                    .write(true)
                    .truncate(true)
                    .create(true)
                    .open(path)
                    .unwrap(),
            )
        });

        Self {
            level: config.logging.level.clone(),
            console: config.logging.console,
            file,
        }
    }
}

impl Logger {
    /// Logs an error message.
    pub fn error(&self, message: impl AsRef<str>) {
        let string = format!("{} [ERROR] {}", Logger::time_format(), message.as_ref());
        self.log_to_console(&string);
        self.log_to_file(&string);
    }

    /// Logs a warning, provided that the log level allows this.
    pub fn warn(&self, message: impl AsRef<str>) {
        if self.level >= LogLevel::Warn {
            let string = format!("{} [WARN]  {}", Logger::time_format(), message.as_ref());
            self.log_to_console(&string);
            self.log_to_file(&string);
        }
    }

    /// Logs information, provided that the log level allows this.
    pub fn info(&self, message: impl AsRef<str>) {
        if self.level >= LogLevel::Info {
            let string = format!("{} [INFO]  {}", Logger::time_format(), message.as_ref());
            self.log_to_console(&string);
            self.log_to_file(&string);
        }
    }

    /// Logs debug information, provided that the log level allows this.
    pub fn debug(&self, message: impl AsRef<str>) {
        if self.level == LogLevel::Debug {
            let string = format!("{} [DEBUG] {}", Logger::time_format(), message.as_ref());
            self.log_to_console(&string);
            self.log_to_file(&string);
        }
    }

    /// Formats the current time into the format `YYYY-MM-DD HH:MM:SS`
    fn time_format() -> String {
        let time = DateTime::now();
        format!(
            "{}-{:02}-{:02} {:02}:{:02}:{:02}",
            time.year,
            time.month + 1,
            time.day,
            time.hour,
            time.minute,
            time.second
        )
    }

    /// Logs the string to the console, if the logging configuration allows it
    fn log_to_console(&self, string: &str) {
        if self.console {
            println!("{}", string);
        }
    }

    /// Logs the string to the log file, if the logging configuration allows it
    fn log_to_file(&self, string: &str) {
        if let Some(file_mutex) = &self.file {
            let mut file = file_mutex.lock().unwrap();
            file.write_all(string.as_bytes()).unwrap();
            file.write_all(b"\n").unwrap();
        }
    }
}

impl FromStr for LogLevel {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "error" => Ok(Self::Error),
            "warn" => Ok(Self::Warn),
            "info" => Ok(Self::Info),
            "debug" => Ok(Self::Debug),
            _ => Err("Log level was invalid"),
        }
    }
}

impl ToEventMask for LogLevel {
    fn to_event_mask(&self) -> u32 {
        match self {
            Self::Error => INTERNAL_MASK_ERROR,
            Self::Warn => INTERNAL_MASK_WARN,
            Self::Info => INTERNAL_MASK_INFO,
            Self::Debug => INTERNAL_MASK_DEBUG,
        }
    }
}

/// Monitors internal events and logs them.
pub fn monitor_thread(rx: Receiver<Event>, state: Arc<AppState>) {
    for e in rx {
        if e.kind == EventType::RequestServedError
            && !e
                .info
                .as_ref()
                .map(|i| i.starts_with("400"))
                .unwrap_or(false)
        {
            continue;
        }

        let message = if let Some(info) = e.info {
            if e.kind == EventType::RequestServedError || e.kind == EventType::RequestTimeout {
                format!(
                    "{}{}",
                    e.peer
                        .map(|p| p.ip().to_string() + ": ")
                        .unwrap_or_else(|| "".into()),
                    info
                )
            } else {
                format!(
                    "{}{}: {}",
                    e.peer
                        .map(|p| p.ip().to_string() + ": ")
                        .unwrap_or_else(|| "".into()),
                    e.kind.to_string(),
                    info
                )
            }
        } else {
            format!(
                "{}{}",
                e.peer
                    .map(|p| p.ip().to_string() + ": ")
                    .unwrap_or_else(|| "".into()),
                e.kind.to_string()
            )
        };

        if e.kind.to_event_mask() & INTERNAL_MASK_ERROR != 0 {
            state.logger.error(message);
        } else if e.kind.to_event_mask() & INTERNAL_MASK_WARN != 0 {
            state.logger.warn(message);
        } else if e.kind.to_event_mask() & INTERNAL_MASK_INFO != 0 {
            state.logger.info(message);
        } else {
            state.logger.debug(message);
        }
    }
}