1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use hurl_core::error::{DisplaySourceError, OutputFormat};
19
use hurl_core::input::Input;
20
use hurl_core::text::{Format, Style, StyledString};
21

            
22
/// A simple logger to log app related event (start, high levels error, etc...).
23
pub struct Logger {
24
    /// Format of the message in the terminal: ANSI or plain.
25
    format: Format,
26
}
27

            
28
impl Logger {
29
    /// Creates a new logger using `color`.
30
189
    pub fn new(color: bool) -> Self {
31
189
        let format = if color { Format::Ansi } else { Format::Plain };
32
189
        Logger { format }
33
    }
34

            
35
    /// Prints an error `message` on standard error.
36
3
    pub fn error(&self, message: &str) {
37
3
        let mut s = StyledString::new();
38
3
        s.push_with("error", Style::new().red().bold());
39
3
        s.push(": ");
40
3
        s.push_with(message, Style::new().bold());
41
3
        eprintln!("{}", s.to_string(self.format));
42
    }
43

            
44
    /// Displays a Hurl parsing error.
45
6
    pub fn error_parsing<E: DisplaySourceError>(&self, content: &str, file: &Input, error: &E) {
46
6
        // FIXME: peut-être qu'on devrait faire rentrer le prefix `error:` qui est
47
6
        // fournit par `self.error_rich` dans la méthode `error.to_string`
48
6
        let message = error.to_string(
49
6
            &file.to_string(),
50
6
            content,
51
6
            None,
52
6
            OutputFormat::Terminal(self.format == Format::Ansi),
53
6
        );
54
6

            
55
6
        let mut s = StyledString::new();
56
6
        s.push_with("error", Style::new().red().bold());
57
6
        s.push(": ");
58
6
        s.push(&message);
59
6
        s.push("\n");
60
6
        eprintln!("{}", s.to_string(self.format));
61
    }
62

            
63
    /// Displays a lint warning.
64
9
    pub fn warn_lint<E: DisplaySourceError>(&self, content: &str, file: &Input, error: &E) {
65
9
        let message = error.to_string(
66
9
            &file.to_string(),
67
9
            content,
68
9
            None,
69
9
            OutputFormat::Terminal(self.format == Format::Ansi),
70
9
        );
71
9

            
72
9
        let mut s = StyledString::new();
73
9
        s.push_with("warning", Style::new().yellow().bold());
74
9
        s.push(": ");
75
9
        s.push(&message);
76
9
        s.push("\n");
77
9
        eprintln!("{}", s.to_string(self.format));
78
    }
79
}