diff --git a/src/logger.rs b/src/logger.rs index a027304..bd3639a 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -2,6 +2,80 @@ use std::env; use simplelog::{ConfigBuilder, SharedLogger}; +/// 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"; + +#[macro_export] +/// Start a new log group. All logs between this and the next `end_group!` will be grouped together. +/// +/// # Example +/// +/// ```rust +/// start_group!("My group"); +/// info!("This will be grouped"); +/// end_group!(); +/// ``` +macro_rules! start_group { + ($name:expr) => { + log::log!(target: $crate::logger::GROUP_TARGET, log::Level::Info, "{}", $name); + }; +} + +#[macro_export] +/// Start a new opened log group. All logs between this and the next `end_group!` will be grouped together. +/// +/// # Example +/// +/// ```rust +/// start_opened_group!("My group"); +/// info!("This will be grouped"); +/// end_group!(); +/// ``` +macro_rules! start_opened_group { + ($name:expr) => { + log::log!(target: $crate::logger::OPENED_GROUP_TARGET, log::Level::Info, "{}", $name); + }; +} + +#[macro_export] +/// End the current log group. +/// See [`start_group!`] for more information. +macro_rules! end_group { + () => { + log::log!(target: $crate::logger::GROUP_TARGET, log::Level::Info, ""); + }; +} + +pub enum GroupEvent { + Start(String), + StartOpened(String), + End, +} + +/// Returns the group event if the record is a group event, otherwise returns `None`. +pub(super) fn get_group_event(record: &log::Record) -> Option { + match record.target() { + OPENED_GROUP_TARGET => { + let args = record.args().to_string(); + if args.is_empty() { + None + } else { + Some(GroupEvent::StartOpened(args)) + } + } + GROUP_TARGET => { + let args = record.args().to_string(); + if args.is_empty() { + Some(GroupEvent::End) + } else { + Some(GroupEvent::Start(args)) + } + } + _ => None, + } +} + pub fn get_local_logger() -> Box { let log_level = env::var("CODSPEED_LOG") .ok() diff --git a/src/run/ci_provider/buildkite/logger.rs b/src/run/ci_provider/buildkite/logger.rs index 46d3d71..df727c5 100644 --- a/src/run/ci_provider/buildkite/logger.rs +++ b/src/run/ci_provider/buildkite/logger.rs @@ -1,5 +1,6 @@ -use crate::run::ci_provider::logger::{ - get_group_event, should_provider_logger_handle_record, GroupEvent, +use crate::{ + logger::{get_group_event, GroupEvent}, + run::ci_provider::logger::should_provider_logger_handle_record, }; use log::*; use simplelog::SharedLogger; diff --git a/src/run/ci_provider/github_actions/logger.rs b/src/run/ci_provider/github_actions/logger.rs index 407f5db..5a24f87 100644 --- a/src/run/ci_provider/github_actions/logger.rs +++ b/src/run/ci_provider/github_actions/logger.rs @@ -1,5 +1,6 @@ -use crate::run::ci_provider::logger::{ - get_group_event, should_provider_logger_handle_record, GroupEvent, +use crate::{ + logger::{get_group_event, GroupEvent}, + run::ci_provider::logger::should_provider_logger_handle_record, }; use log::*; use simplelog::SharedLogger; diff --git a/src/run/ci_provider/logger.rs b/src/run/ci_provider/logger.rs index 17ab082..85ba733 100644 --- a/src/run/ci_provider/logger.rs +++ b/src/run/ci_provider/logger.rs @@ -1,79 +1,5 @@ use crate::run::runner::VALGRIND_EXECUTION_TARGET; -/// 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"; - -#[macro_export] -/// Start a new log group. All logs between this and the next `end_group!` will be grouped together. -/// -/// # Example -/// -/// ```rust -/// start_group!("My group"); -/// info!("This will be grouped"); -/// end_group!(); -/// ``` -macro_rules! start_group { - ($name:expr) => { - log::log!(target: $crate::run::ci_provider::logger::GROUP_TARGET, log::Level::Info, "{}", $name); - }; -} - -#[macro_export] -/// Start a new opened log group. All logs between this and the next `end_group!` will be grouped together. -/// -/// # Example -/// -/// ```rust -/// start_opened_group!("My group"); -/// info!("This will be grouped"); -/// end_group!(); -/// ``` -macro_rules! start_opened_group { - ($name:expr) => { - log::log!(target: $crate::run::ci_provider::logger::OPENED_GROUP_TARGET, log::Level::Info, "{}", $name); - }; -} - -#[macro_export] -/// End the current log group. -/// See [`start_group!`] for more information. -macro_rules! end_group { - () => { - log::log!(target: $crate::run::ci_provider::logger::GROUP_TARGET, log::Level::Info, ""); - }; -} - -pub enum GroupEvent { - Start(String), - StartOpened(String), - End, -} - -/// Returns the group event if the record is a group event, otherwise returns `None`. -pub(super) fn get_group_event(record: &log::Record) -> Option { - match record.target() { - OPENED_GROUP_TARGET => { - let args = record.args().to_string(); - if args.is_empty() { - None - } else { - Some(GroupEvent::StartOpened(args)) - } - } - GROUP_TARGET => { - let args = record.args().to_string(); - if args.is_empty() { - Some(GroupEvent::End) - } else { - Some(GroupEvent::Start(args)) - } - } - _ => None, - } -} - pub(super) fn should_provider_logger_handle_record(record: &log::Record) -> bool { // Provider logger should handle all records except the ones from the valgrind execution target record.target() != VALGRIND_EXECUTION_TARGET diff --git a/src/run/logger.rs b/src/run/logger.rs index e543474..a048337 100644 --- a/src/run/logger.rs +++ b/src/run/logger.rs @@ -1,9 +1,6 @@ +use crate::logger::{GROUP_TARGET, OPENED_GROUP_TARGET}; use crate::prelude::*; -use crate::run::{ - ci_provider::logger::{GROUP_TARGET, OPENED_GROUP_TARGET}, - ci_provider::CIProvider, - runner::RunData, -}; +use crate::run::{ci_provider::CIProvider, runner::RunData}; use log::LevelFilter; use simplelog::{CombinedLogger, WriteLogger}; use std::fs::copy;