1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 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
198
    pub fn new(color: bool) -> Self {
31
198
        let format = if color { Format::Ansi } else { Format::Plain };
32
198
        Logger { format }
33
    }
34

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

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

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

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

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