From dfef6931dd39b09b55e0389fadeccd6d934d67a1 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Amiel Date: Fri, 26 Apr 2024 17:57:28 +0200 Subject: [PATCH] Move BaseLogger outside of the hurl crate. --- packages/hurl/src/cli/logger.rs | 59 ++++++++++++++++++++++++++++++++ packages/hurl/src/cli/mod.rs | 3 ++ packages/hurl/src/main.rs | 3 +- packages/hurl/src/util/logger.rs | 47 +------------------------ 4 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 packages/hurl/src/cli/logger.rs diff --git a/packages/hurl/src/cli/logger.rs b/packages/hurl/src/cli/logger.rs new file mode 100644 index 00000000000..7d6dd7b481c --- /dev/null +++ b/packages/hurl/src/cli/logger.rs @@ -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}"); + } + } +} diff --git a/packages/hurl/src/cli/mod.rs b/packages/hurl/src/cli/mod.rs index 96921a2eb5d..f1c846c8306 100644 --- a/packages/hurl/src/cli/mod.rs +++ b/packages/hurl/src/cli/mod.rs @@ -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; diff --git a/packages/hurl/src/main.rs b/packages/hurl/src/main.rs index e7606465b86..d38cf30689b 100644 --- a/packages/hurl/src/main.rs +++ b/packages/hurl/src/main.rs @@ -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; diff --git a/packages/hurl/src/util/logger.rs b/packages/hurl/src/util/logger.rs index 154a3f188ac..35e46e73afb 100644 --- a/packages/hurl/src/util/logger.rs +++ b/packages/hurl/src/util/logger.rs @@ -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,