Skip to content

Commit

Permalink
refactor(cli): move local logger to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jul 24, 2024
1 parent 7af9b55 commit 04f0e4d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 170 deletions.
4 changes: 3 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{api_client::CodSpeedAPIClient, auth, logger::CODSPEED_U8_COLOR_CODE, prelude::*, run};
use crate::{
api_client::CodSpeedAPIClient, auth, local_logger::CODSPEED_U8_COLOR_CODE, prelude::*, run,
};
use clap::{
builder::{styling, Styles},
Parser, Subcommand,
Expand Down
2 changes: 1 addition & 1 deletion src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use crate::logger::get_local_logger;
use crate::local_logger::get_local_logger;
use crate::{api_client::CodSpeedAPIClient, config::CodSpeedConfig, prelude::*};
use clap::{Args, Subcommand};
use console::style;
Expand Down
166 changes: 166 additions & 0 deletions src/local_logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use std::{
env,
sync::{Arc, Mutex},
time::Duration,
};

use console::{style, Style};
use indicatif::{ProgressBar, ProgressStyle};
use lazy_static::lazy_static;
use log::Log;
use simplelog::SharedLogger;
use std::io::Write;

use crate::logger::{get_group_event, GroupEvent};

pub const CODSPEED_U8_COLOR_CODE: u8 = 208; // #FF8700
const BLACK_U8_COLOR_CODE: u8 = 16; // #000

lazy_static! {
pub static ref SPINNER: Arc<Mutex<Option<ProgressBar>>> = Arc::new(Mutex::new(None));
pub static ref IS_TTY: bool = std::io::IsTerminal::is_terminal(&std::io::stdout());
}

/// Hide the progress bar temporarily, execute `f`, then redraw the progress bar.
///
/// If the output is not a TTY, `f` will be executed without hiding the progress bar.
pub fn suspend_progress_bar<F: FnOnce() -> R, R>(f: F) -> R {
// If the output is a TTY, and there is a spinner, suspend it
if *IS_TTY {
if let Ok(mut spinner) = SPINNER.lock() {
if let Some(spinner) = spinner.as_mut() {
return spinner.suspend(f);
}
}
}

// Otherwise, just run the function
f()
}

pub struct LocalLogger {
log_level: log::LevelFilter,
}

impl LocalLogger {
pub fn new() -> Self {
let log_level = env::var("CODSPEED_LOG")
.ok()
.and_then(|log_level| log_level.parse::<log::LevelFilter>().ok())
.unwrap_or(log::LevelFilter::Info);

LocalLogger { log_level }
}
}

impl Log for LocalLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= self.log_level
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

if let Some(group_event) = get_group_event(record) {
match group_event {
GroupEvent::Start(name) | GroupEvent::StartOpened(name) => {
println!(
" {}",
style(format!(" {} ", name.to_uppercase()))
.bold()
.color256(BLACK_U8_COLOR_CODE)
.on_color256(CODSPEED_U8_COLOR_CODE)
);
println!();

if *IS_TTY {
let spinner = ProgressBar::new_spinner();
spinner.set_style(
ProgressStyle::with_template(
format!(
" {{spinner:>.{}}} {{wide_msg:.{}.bold}}",
CODSPEED_U8_COLOR_CODE, CODSPEED_U8_COLOR_CODE
)
.as_str(),
)
.unwrap(),
);
spinner.set_message(format!("{}...", name));
spinner.enable_steady_tick(Duration::from_millis(100));
SPINNER.lock().unwrap().replace(spinner);
} else {
println!("{}...", name);
}
}
GroupEvent::End => {
if *IS_TTY {
let mut spinner = SPINNER.lock().unwrap();
if let Some(spinner) = spinner.as_mut() {
spinner.finish_and_clear();
println!();
}
}
println!();
}
}

return;
}

suspend_progress_bar(|| print_record(record));
}

fn flush(&self) {
std::io::stdout().flush().unwrap();
}
}

/// Print a log record to the console with the appropriate style
fn print_record(record: &log::Record) {
let error_style = Style::new().red();
let info_style = Style::new().white();
let warn_style = Style::new().yellow();
let debug_style = Style::new().blue().dim();
let trace_style = Style::new().black().dim();

match record.level() {
log::Level::Error => eprintln!("{}", error_style.apply_to(record.args())),
log::Level::Warn => eprintln!("{}", warn_style.apply_to(record.args())),
log::Level::Info => println!("{}", info_style.apply_to(record.args())),
log::Level::Debug => println!(
"{}",
debug_style.apply_to(format!("[DEBUG::{}] {}", record.target(), record.args())),
),
log::Level::Trace => println!(
"{}",
trace_style.apply_to(format!("[TRACE::{}] {}", record.target(), record.args()))
),
}
}

impl SharedLogger for LocalLogger {
fn level(&self) -> log::LevelFilter {
self.log_level
}

fn config(&self) -> Option<&simplelog::Config> {
None
}

fn as_log(self: Box<Self>) -> Box<dyn Log> {
Box::new(*self)
}
}

pub fn get_local_logger() -> Box<dyn SharedLogger> {
Box::new(LocalLogger::new())
}

pub fn clean_logger() {
let mut spinner = SPINNER.lock().unwrap();
if let Some(spinner) = spinner.as_mut() {
spinner.finish_and_clear();
}
}
165 changes: 0 additions & 165 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
use std::{
env,
sync::{Arc, Mutex},
time::Duration,
};

use console::{style, Style};
use indicatif::{ProgressBar, ProgressStyle};
use lazy_static::lazy_static;
use log::Log;
use simplelog::SharedLogger;
use std::io::Write;

pub const CODSPEED_U8_COLOR_CODE: u8 = 208; // #FF8700
const BLACK_U8_COLOR_CODE: u8 = 16; // #000

/// This target is used exclusively to handle group events.
pub const GROUP_TARGET: &str = "codspeed::group";
pub const OPENED_GROUP_TARGET: &str = "codspeed::group::opened";
Expand Down Expand Up @@ -87,152 +71,3 @@ pub(super) fn get_group_event(record: &log::Record) -> Option<GroupEvent> {
_ => None,
}
}

lazy_static! {
pub static ref SPINNER: Arc<Mutex<Option<ProgressBar>>> = Arc::new(Mutex::new(None));
pub static ref IS_TTY: bool = std::io::IsTerminal::is_terminal(&std::io::stdout());
}

/// Hide the progress bar temporarily, execute `f`, then redraw the progress bar.
///
/// If the output is not a TTY, `f` will be executed without hiding the progress bar.
pub fn suspend_progress_bar<F: FnOnce() -> R, R>(f: F) -> R {
// If the output is a TTY, and there is a spinner, suspend it
if *IS_TTY {
if let Ok(mut spinner) = SPINNER.lock() {
if let Some(spinner) = spinner.as_mut() {
return spinner.suspend(f);
}
}
}

// Otherwise, just run the function
f()
}

pub struct LocalLogger {
log_level: log::LevelFilter,
}

impl LocalLogger {
pub fn new() -> Self {
let log_level = env::var("CODSPEED_LOG")
.ok()
.and_then(|log_level| log_level.parse::<log::LevelFilter>().ok())
.unwrap_or(log::LevelFilter::Info);

LocalLogger { log_level }
}
}

impl Log for LocalLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= self.log_level
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

if let Some(group_event) = get_group_event(record) {
match group_event {
GroupEvent::Start(name) | GroupEvent::StartOpened(name) => {
println!(
" {}",
style(format!(" {} ", name.to_uppercase()))
.bold()
.color256(BLACK_U8_COLOR_CODE)
.on_color256(CODSPEED_U8_COLOR_CODE)
);
println!();

if *IS_TTY {
let spinner = ProgressBar::new_spinner();
spinner.set_style(
ProgressStyle::with_template(
format!(
" {{spinner:>.{}}} {{wide_msg:.{}.bold}}",
CODSPEED_U8_COLOR_CODE, CODSPEED_U8_COLOR_CODE
)
.as_str(),
)
.unwrap(),
);
spinner.set_message(format!("{}...", name));
spinner.enable_steady_tick(Duration::from_millis(100));
SPINNER.lock().unwrap().replace(spinner);
} else {
println!("{}...", name);
}
}
GroupEvent::End => {
if *IS_TTY {
let mut spinner = SPINNER.lock().unwrap();
if let Some(spinner) = spinner.as_mut() {
spinner.finish_and_clear();
println!();
}
}
println!();
}
}

return;
}

suspend_progress_bar(|| print_record(record));
}

fn flush(&self) {
std::io::stdout().flush().unwrap();
}
}

/// Print a log record to the console with the appropriate style
fn print_record(record: &log::Record) {
let error_style = Style::new().red();
let info_style = Style::new().white();
let warn_style = Style::new().yellow();
let debug_style = Style::new().blue().dim();
let trace_style = Style::new().black().dim();

match record.level() {
log::Level::Error => eprintln!("{}", error_style.apply_to(record.args())),
log::Level::Warn => eprintln!("{}", warn_style.apply_to(record.args())),
log::Level::Info => println!("{}", info_style.apply_to(record.args())),
log::Level::Debug => println!(
"{}",
debug_style.apply_to(format!("[DEBUG::{}] {}", record.target(), record.args())),
),
log::Level::Trace => println!(
"{}",
trace_style.apply_to(format!("[TRACE::{}] {}", record.target(), record.args()))
),
}
}

impl SharedLogger for LocalLogger {
fn level(&self) -> log::LevelFilter {
self.log_level
}

fn config(&self) -> Option<&simplelog::Config> {
None
}

fn as_log(self: Box<Self>) -> Box<dyn Log> {
Box::new(*self)
}
}

pub fn get_local_logger() -> Box<dyn SharedLogger> {
Box::new(LocalLogger::new())
}

pub fn clean_logger() {
let mut spinner = SPINNER.lock().unwrap();
if let Some(spinner) = spinner.as_mut() {
spinner.finish_and_clear();
}
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mod api_client;
mod app;
mod auth;
mod config;
mod local_logger;
mod logger;
mod prelude;
mod request_client;
mod run;

use console::style;
use local_logger::clean_logger;
use prelude::*;

use log::log_enabled;
Expand All @@ -32,7 +34,7 @@ async fn main() {
debug!("Caused by: {}", e);
}
}
logger::clean_logger();
clean_logger();

std::process::exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/run/ci_provider/local/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use git2::Repository;
use lazy_static::lazy_static;
use simplelog::SharedLogger;

use crate::logger::get_local_logger;
use crate::local_logger::get_local_logger;
use crate::prelude::*;
use crate::run::{
ci_provider::{
Expand Down
2 changes: 1 addition & 1 deletion src/run/runner/valgrind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::logger::suspend_progress_bar;
use crate::local_logger::suspend_progress_bar;
use crate::prelude::*;
use crate::run::{
config::Config, instruments::mongo_tracer::MongoTracer,
Expand Down

0 comments on commit 04f0e4d

Please sign in to comment.