Skip to content

Commit

Permalink
Move BaseLogger outside of the hurl crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamiel committed Apr 26, 2024
1 parent fbd9ab1 commit dfef693
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 48 deletions.
59 changes: 59 additions & 0 deletions packages/hurl/src/cli/logger.rs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
3 changes: 3 additions & 0 deletions packages/hurl/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 1 addition & 2 deletions packages/hurl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 1 addition & 46 deletions packages/hurl/src/util/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit dfef693

Please sign in to comment.