mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-11-23 00:44:55 +03:00
Move BaseLogger outside of the hurl crate.
This commit is contained in:
parent
fbd9ab1cb3
commit
dfef6931dd
59
packages/hurl/src/cli/logger.rs
Normal file
59
packages/hurl/src/cli/logger.rs
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Hurl (https://hurl.dev)
|
||||
* Copyright (C) 2024 Orange
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
use colored::Colorize;
|
||||
|
||||
/// A simple logger to log app related event (start, high levels error, etc...).
|
||||
pub struct BaseLogger {
|
||||
/// Uses ANSI color codes or not.
|
||||
pub color: bool,
|
||||
/// Prints debug message or not.
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
impl BaseLogger {
|
||||
/// Creates a new base logger using `color` and `verbose`.
|
||||
pub fn new(color: bool, verbose: bool) -> BaseLogger {
|
||||
BaseLogger { color, verbose }
|
||||
}
|
||||
|
||||
/// Prints an informational `message` on standard error.
|
||||
pub fn info(&self, message: &str) {
|
||||
eprintln!("{message}");
|
||||
}
|
||||
|
||||
/// Prints a debug `message` on standard error if the logger is in verbose mode.
|
||||
pub fn debug(&self, message: &str) {
|
||||
if !self.verbose {
|
||||
return;
|
||||
}
|
||||
if self.color {
|
||||
eprintln!("{} {message}", "*".blue().bold());
|
||||
} else {
|
||||
eprintln!("* {message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Prints an error `message` on standard error.
|
||||
pub fn error(&self, message: &str) {
|
||||
if self.color {
|
||||
eprintln!("{}: {}", "error".red().bold(), message.bold());
|
||||
} else {
|
||||
eprintln!("error: {message}");
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +17,9 @@
|
||||
*/
|
||||
mod error;
|
||||
mod interactive;
|
||||
mod logger;
|
||||
pub(crate) mod options;
|
||||
|
||||
pub(crate) use self::error::CliError;
|
||||
pub(crate) use self::logger::BaseLogger;
|
||||
pub(crate) use self::options::OutputType;
|
||||
|
@ -23,12 +23,11 @@ use std::path::Path;
|
||||
use std::time::Instant;
|
||||
use std::{env, process, thread};
|
||||
|
||||
use crate::cli::CliError;
|
||||
use crate::cli::{BaseLogger, CliError};
|
||||
use colored::control;
|
||||
use hurl::report::{html, junit, tap};
|
||||
use hurl::runner;
|
||||
use hurl::runner::{HurlResult, Input};
|
||||
use hurl::util::logger::BaseLogger;
|
||||
|
||||
use crate::cli::options::CliOptionsError;
|
||||
|
||||
|
@ -24,50 +24,6 @@ use hurl_core::error::Error;
|
||||
use crate::runner::Value;
|
||||
use crate::util::term::Stderr;
|
||||
|
||||
/// A simple logger to log app related event (start, high levels error, etc...).
|
||||
/// When we run an [`hurl_core::ast::HurlFile`], user has to provide a dedicated Hurl logger (see [`Logger`]).
|
||||
pub struct BaseLogger {
|
||||
pub color: bool,
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
impl BaseLogger {
|
||||
pub fn new(color: bool, verbose: bool) -> BaseLogger {
|
||||
BaseLogger { color, verbose }
|
||||
}
|
||||
|
||||
pub fn info(&self, message: &str) {
|
||||
eprintln!("{message}");
|
||||
}
|
||||
|
||||
pub fn debug(&self, message: &str) {
|
||||
if !self.verbose {
|
||||
return;
|
||||
}
|
||||
if self.color {
|
||||
eprintln!("{} {message}", "*".blue().bold());
|
||||
} else {
|
||||
eprintln!("* {message}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn warning(&self, message: &str) {
|
||||
if self.color {
|
||||
eprintln!("{}: {}", "warning".yellow().bold(), message.bold());
|
||||
} else {
|
||||
eprintln!("warning: {message}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(&self, message: &str) {
|
||||
if self.color {
|
||||
eprintln!("{}: {}", "error".red().bold(), message.bold());
|
||||
} else {
|
||||
eprintln!("error: {message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ErrorFormat {
|
||||
Short,
|
||||
@ -90,8 +46,7 @@ impl Verbosity {
|
||||
}
|
||||
}
|
||||
|
||||
/// A Hurl dedicated logger for an Hurl file. Contrary to [`BaseLogger`], this logger can display
|
||||
/// rich error for parsing and runtime errors.
|
||||
/// A dedicated logger for an Hurl file. This logger can display rich parsing and runtime errors.
|
||||
#[derive(Clone)]
|
||||
pub struct Logger {
|
||||
pub(crate) color: bool,
|
||||
|
Loading…
Reference in New Issue
Block a user