Skip to content

Commit

Permalink
refactor(logger): move logger group logic to root logger
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jun 19, 2024
1 parent d020bc5 commit f7e0fc5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 83 deletions.
74 changes: 74 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupEvent> {
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<dyn SharedLogger> {
let log_level = env::var("CODSPEED_LOG")
.ok()
Expand Down
5 changes: 3 additions & 2 deletions src/run/ci_provider/buildkite/logger.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/run/ci_provider/github_actions/logger.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
74 changes: 0 additions & 74 deletions src/run/ci_provider/logger.rs
Original file line number Diff line number Diff line change
@@ -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<GroupEvent> {
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
Expand Down
7 changes: 2 additions & 5 deletions src/run/logger.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit f7e0fc5

Please sign in to comment.