teo_runtime/message/
mod.rs

1use std::time::Duration;
2use chrono::{DateTime, Local};
3use colored::{ColoredString, Colorize};
4use array_tool::vec::Join;
5
6fn timestamp() -> ColoredString {
7    let local: DateTime<Local> = Local::now();
8    let local_formatted = format!("[{}]", local.format("%Y-%m-%d %H:%M:%S")).bright_blue();
9    local_formatted
10}
11
12pub fn info_message(content: impl AsRef<str>) {
13    println!("{} {}", timestamp(), content.as_ref())
14}
15
16pub fn request_message(
17    time_elapsed: Duration,
18    method: &str,
19    path: &str,
20    handler_group_path: &Vec<String>,
21    action: &str,
22    code: u16,
23) {
24    let handler_str: String = handler_group_path.join(".") + ".";
25    let code_string = format_code_into_string(code);
26    let ms = time_elapsed.as_millis();
27    let ms_str = format!("{ms}ms").normal().clear();
28    println!("{} {} {} => {}{} {} {}", timestamp(), method.bright_blue().bold(), path.bright_yellow(), handler_str.magenta(), action.purple(), code_string, ms_str)
29}
30
31pub fn unhandled_request_message(
32    time_elapsed: Duration,
33    method: &str,
34    path: &str,
35    code: u16,
36) {
37    let code_string = format_code_into_string(code);
38    let ms = time_elapsed.as_millis();
39    let ms_str = format!("{ms}ms").normal().clear();
40    println!("{} {} {} {} {}", timestamp(), method.bright_blue().bold(), path.bright_yellow(), code_string, ms_str)
41}
42
43fn format_code_into_string(code: u16) -> ColoredString {
44    match code {
45        0..=199 => code.to_string().purple().bold(),
46        200..=299 => code.to_string().green().bold(),
47        300..=399 => code.to_string().yellow().bold(),
48        _ => code.to_string().red().bold(),
49    }
50}